p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Accessing a simple database


Message #1 by "Francois Desautels" <fdesautels@r...> on Mon, 28 Oct 2002 20:39:41
Hi, 

I'm just starting out, and I'm trying to get at some information I 
entered in a database.  Yet I keep getting error messages, and I don't 
know what I'm doing wrong.

Warning: mysql_result(): supplied argument is not a valid MySQL result 
resource in /usr/local/apache/htdocs/database.php on line 8
 First Name: 

 Warning: mysql_result(): supplied argument is not a valid MySQL result 
resource in /usr/local/apache/htdocs/database.php on line 10
 Last Name: 

 Warning: mysql_result(): supplied argument is not a valid MySQL result 
resource in /usr/local/apache/htdocs/database.php on line 12
 Address: 

 Warning: mysql_result(): supplied argument is not a valid MySQL result 
resource in /usr/local/apache/htdocs/database.php on line 14
 Position: 

and this is the script...

<html>
	<body>
	<?
		$db = mysql_connect("localhost", "mysql");
		mysql_select_db("mydb", $db);
		$result = mysql_query("SELECT * FROM employees", $db);

		printf ("First Name: %s<br>\n", mysql_result($result, 
0, "first"));

		printf ("Last Name: %s<br>\n", mysql_result($result, 
0, "last"));

		printf ("Address: %s<br>\n", mysql_result($result, 
0, "address"));

		printf ("Position: %s<br>\n", mysql_result($result, 
0, "position"));

	?>

	</body>
</html>

Any help would be greatly appreciated...
Message #2 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Mon, 28 Oct 2002 12:56:05 -0800
Francois,

These error messages suggest that this line:

> 		$result =3D mysql_query("SELECT * FROM employees", $db);

...is failing you, although the actual problem could be earlier.

You're not doing any testing in your code to see if each of the lines 
interacting with the database are returning valid results.

After each of these lines:

> 		$db =3D mysql_connect("localhost", "mysql");
> 		mysql_select_db("mydb", $db);
> 		$result =3D mysql_query("SELECT * FROM employees", $db);

... test the result to make sure it's not NULL. If it is NULL, 
immediately look at mysql_error() (say, with echo()), which will tell 
you what's up (e.g., invalid username and password).

dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************


> -----Original Message-----
> From: Francois Desautels [mailto:fdesautels@r...]
> Sent: Monday, October 28, 2002 12:40 PM
> To: beginning php
> Subject: [beginning_php] Accessing a simple database
>
>
> Hi,
>
> I'm just starting out, and I'm trying to get at some information I
> entered in a database.  Yet I keep getting error messages,
> and I don't
> know what I'm doing wrong.
>
> Warning: mysql_result(): supplied argument is not a valid
> MySQL result
> resource in /usr/local/apache/htdocs/database.php on line 8
>  First Name:
>
>  Warning: mysql_result(): supplied argument is not a valid
> MySQL result
> resource in /usr/local/apache/htdocs/database.php on line 10
>  Last Name:
>
>  Warning: mysql_result(): supplied argument is not a valid
> MySQL result
> resource in /usr/local/apache/htdocs/database.php on line 12
>  Address:
>
>  Warning: mysql_result(): supplied argument is not a valid
> MySQL result
> resource in /usr/local/apache/htdocs/database.php on line 14
>  Position:
>
> and this is the script...
>
> <html>
> 	<body>
> 	<?
> 		$db =3D mysql_connect("localhost", "mysql");
> 		mysql_select_db("mydb", $db);
> 		$result =3D mysql_query("SELECT * FROM employees", $db);
>
> 		printf ("First Name: %s<br>\n", mysql_result($result,
> 0, "first"));
>
> 		printf ("Last Name: %s<br>\n", mysql_result($result,
> 0, "last"));
>
> 		printf ("Address: %s<br>\n", mysql_result($result,
> 0, "address"));
>
> 		printf ("Position: %s<br>\n", mysql_result($result,
> 0, "position"));
>
> 	?>
>
> 	</body>
> </html>
>
> Any help would be greatly appreciated...
>
Message #3 by "Nikolai Devereaux" <yomama@u...> on Mon, 28 Oct 2002 13:19:23 -0800
To put david's advice (and my own minor changes) into code:


<html>
	<body>
	<?php
		$db = mysql_connect("localhost", "mysql");

		if(! $db)
            {
			die "mysql_connect failed: " . mysql_error();
            }

		if(!mysql_select_db("mydb", $db))
            {
			die "mysql_select_db failed: " . mysql_error();
            }

		$result = mysql_query("SELECT * FROM employees", $db);

            if(! $result)
            {
			die "mysql_query failed: " . mysql_error();
            }

		$row = mysql_fetch_array($result, DB_ASSOC);

		echo "First Name: $row[first]<br>\n";
		echo "Last Name:  $row[last]<br>\n";
		echo "Address:    $row[address]<br>\n";
		echo "Position:   $row[position]<br>\n";

	?>
	</body>
</html>


take care,

nik


  Return to Index