Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP MySQL (mysql_fetch_array) Help! 2

Status
Not open for further replies.
Jun 5, 2005
103
US
Hi,

I am unable to pull data from the MySQL data bause using the following code:

<html>
<head>
<title>Contacts</title>
<style>
A:link {text-decoration:none; color:#3152A5;}
A:visited {text-decoration:none; color:#3152A5;}
A:hover {text-decoration:none; color:#6699FF}
</style>
<?
$dbconnect = mysql_connect ("localhost", "alasfdu_public", "password")
or die ('Cannot connect to the database because: ' . mysql_error());
mysql_select_db("alasfdu_alasmem");
?>
</head>

<body bgcolor="#FFFFFF" text="#3152A5">

<table border="0" align="center" cellpadding="1" cellspacing="0" width="75%">
<tr>
<th align="center" colspan="5" bgcolor="#E0E0E0">Contactos</th>
</tr>
<tr bgcolor="#F8F8FF">
<th align=center>Position</th>
<th align=center>Name</th>
<th align=center>Country</th>
</tr>

<?
$num=1;
include ("config.php");
$result = mysql_query ("select * from board_mem", $dbconnect);

while ($row = mysql_fetch_array($result))
{
extract($row);
($num == 1)?$bgcolor="#F5F5F5":$bgcolor="#F8F8FF";
?>
<tr bgcolor="<?=$bgcolor?>">
<td align=center><b><a href="#"><?=$row[1]?></a></b></td>
<td align=center><b><a href="#"><?=$row[2]?></a></b></td>
<td align=center><b><a href="#"><?=$row[3]?></a></b></td>
</tr>
<?
if($num == 1)
{
$num++;
}
else
{
$num--;
}
}
?>
</table>
</body>
</html>

I've spent hours and still can't get this to work. Could some one please HELP!
 
This is the error that I keep getting:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/site/public_html/New/contacts.php on line 42
 
The warning you are seeing is because mysql is returning an error not a result resource, from the mysql_query() call.

Use mysql_errno() and mysql_error() to find out what the problem is.
 
Guys,

Thanks for your help!! I got it to work.

This is in the config.php file:
<?
$dbconnect = mysql_connect ("localhost", "alasfdu_public", "********")
or die ('Cannot connect to the database because: ' . mysql_error());
mysql_select_db("alasfdu_alasmem");
?>

This is in the board_members.php file:
<?
$num=1;
include ("config.php");
$result = mysql_query ("select * from board_mem", $dbconnect);

while ($row = mysql_fetch_array($result))
{
extract($row);
($num == 1)?$bgcolor="#FFFFFF":$bgcolor="#B0C4DE";
?>
<tr bgcolor="<?=$bgcolor?>" onMouseOver="this.bgColor='#EEE8AA';" onMouseOut="this.bgColor='<?=$bgcolor?>';" style="cursor:default;">
<td align=center><font color="#000000"><?=$row[1]?></font></td>
<td align=center><font color="#000000"><?=$row[2]?></font></td>
<td align=center><font color="#000000"><?=$row[3]?></font></td>
</tr>
<?
if($num == 1)
{
$num++;
}
else
{
$num--;
}
}
?>
 
You may have got it to work, but you're still not employing the best programming practices.

Part of your script board_members.php reads:

[tt]$result = mysql_query ("select * from board_mem", $dbconnect);

while ($row = mysql_fetch_array($result))[/tt]

Your script attempts a SELECT query then assumes the query will work correctly and that the return of mysql_query() is a resource handle.

By not having your script explicitly check that it got back a resource handle from mysql_query(), you lay yourself open for further error outputs. If this is a one-off script, I wouldn't worry about it. But if this is part of a website you intend to put into production, you need better error-handling.

Typically, my scripts do something like:

[tt]$result = mysql_query ("select * from board_mem", $dbconnect);
if ($result !== FALSE)
{
while ($row = mysql_fetch_array($result))
.
.
.
}
else
{
//output some kind of user-friendly error message,
//log the error to a file for my debugging purposes,
//and gracefully fail
}[/tt]



Want the best answers? Ask the best questions! TANSTAAFL!
 
sleipnir214,

Yes this is for a website (production). So you recommend that I set the script as listed below.

<?
$num=1;
include ("config.php");
$result = mysql_query ("select * from board_mem", $dbconnect);
if ($result !== FALSE)
{
while ($row = mysql_fetch_array($result))
{
extract($row);
($num == 1)?$bgcolor="#FFFFFF":$bgcolor="#B0C4DE";
?>
<tr bgcolor="<?=$bgcolor?>" onMouseOver="this.bgColor='#EEE8AA';" onMouseOut="this.bgColor='<?=$bgcolor?>';" style="cursor:default;">
<td align=center><?=$row[1]?></td>
<td align=center><?=$row[2]?></td>
<td align=center><?=$row[3]?></td>
</tr>
<?
if($num == 1)
{
$num++;
}
else
{
$num--;
}
}
}
else
{
//output some kind of user-friendly error message,
//log the error to a file for my debugging purposes,
//and gracefully fail
}
?>
 
Did the site remove your indenting, or is that you indentation style? If what is shown is an accurate rendition of your indentation style, I strongly recommend that you change your indentation style.

Taking your code and reformatting for better indentation and replaceing a context-switch with a heredoc printstatement gets:

Code:
<?
$num=1;
include ("config.php");
$result = mysql_query ("select * from board_mem", $dbconnect);
if ($result !== FALSE)
{
	while ($row = mysql_fetch_array($result))
	{
		extract($row);
		($num == 1)?$bgcolor="#FFFFFF":$bgcolor="#B0C4DE";
		
		print <<< EOP
<tr bgcolor="$bgcolor" onMouseOver="this.bgColor='#EEE8AA';" onMouseOut="this.bgColor='$bgcolor';" style="cursor:default;">
  <td align=center>$row[1]</td>
  <td align=center>$row[2]</td>
  <td align=center>$row[3]</td>
</tr>
EOP;

		if($num == 1)
		{
			$num++;
		}
		else
		{
			$num--;
		}
	}
}
else
{
    //output some kind of user-friendly error message,
    //log the error to a file for my debugging purposes,
    //and gracefully fail
}
?>

That, in general overall form, is nearly what I would do.

Except for a couple of things:[ul][li]I never use mysql_fetch_array() but rather always mysql_fetch_assoc(). This allows me to reference array elements by column names (which improves the readability of my code) and reduces the chances that a change to the structure of my table will cause a program fault.[/li][li]I don't understand why you are using extract($row) in one line and then referring to the elements of $row later on[/li][/ul]



Want the best answers? Ask the best questions! TANSTAAFL!
 
Yes, it's the site meesing up my code. How do I create that box that you put the new code in?

I'll try to use the mysql_fetch_assoc() code.

About extract($row) and then calling it again later.
<td align=center><?=$row[1]?></td>
<td align=center><?=$row[2]?></td>
<td align=center><?=$row[3]?></td>
My teacher was the one who told me to use it like that. If you have a better way please inform me.

Now I would like to use arsort() to sort the database in reverse order. How can I use it with my code? Where can I place it?

Thanks!!!
 
To put the box around code, simply put [ignore]
Code:
...
[/ignore] tags around your code.


If you're going to reference $row[1], there is absolutely no need to for extract(). What extract does is that if you have an array:

$the_array = array ('blue' => 3, 'red' => 2);

Then

extract($the_array);

will automatically create variables $blue and $red. What you are doing in your code is creating variables you never use. My suggestion of a better way is to simply delete from your script the line:

extracr($row);

because your code doesn't need it. Generally speaking, I don't like extract(). I've seen far too many abuses of extract() decrease the readability of code.


You can't use arsort() in this application. Okay, you could, but I strongly recommend against it. It's always a good idea when writing a database app to let the database server to as much work as possible, so tell the database server to give you the data in a specific order. If you have columns called "lastname" and "firstname" and you want the data returned in reverse order, ask for it. Instead of:

$result = mysql_query ("select * from board_mem", $dbconnect);

Do:

$result = mysql_query ("select * from board_mem ORDER BY lastname DESC, firstname DESC", $dbconnect);



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for all your help!!!

Can I do this?

Code:
$result = mysql_query ("select * from board_mem ORDER BY id", $dbconnect);

Thanks!
 
I can't answer for certain, not having seen your database schema. But if your table "board_mem" has a column named "id", then that query is probably well-formed.

Whether it will return data in a way that is useful to you, I cannot say.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Dude,

It works!!! I used the below code.

Code:
$result = mysql_query ("select * from mem_reg ORDER BY id DESC", $dbconnect);

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top