p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Look Up Values


Message #1 by "Ken Goff" <macessen@a...> on Sat, 8 Feb 2003 23:13:58
I am trying to use PHP to look up a value within a MySQL table.

My problem is that I am not able to extract data from my look up code which
follows:

$link_id = mysql_connect("localhost","phpuser","phppass");
		if(mysql_select_db("EventDb", $link_id));
        $result = mysql_query("SELECT DayType FROM Day WHERE EventDate
='$y-$m-$d'",$link_id);

And to check the value of "$result", I've tried:

print $result;

Instead of the expected field content of "violet" or "orange", I get
"Resource id #2" or "Resource id #3", etc.

I can use Terminal or phpMyAdmin to query the database table but I'm having
difficulty using PHP to produce the look up.

And print "$y-$m-$d"; will show what appears to be valid Unix style dates,
such as "2003-02-8".
Message #2 by Kyle Ketterer <BIGE88FAN@c...> on Sat, 08 Feb 2003 18:22:00 -0800
This is because your variable "$result" only contains the actual query
to the database. It is considered a resource. Before you can expect any
kind of extraction from a query, you need to call a MySQL function such
as mysql_result().

So in your case, to print out this value from your MySQL table, you
would need to do this:

$link_id = mysql_connect("localhost","phpuser","phppass");
		if(mysql_select_db("EventDb", $link_id));
$result = mysql_query("SELECT DayType FROM Day WHERE EventDate ='$y-$m
$d'",$link_id);
$daytype = mysql_result($result,0,'DayType');
print $daytype; // or echo $daytype;

The reason I suggested the function mysql_result() in this case, is
because your only trying to extract one row of data from your table. For
a query with a larger result set, you need to use one of the following:

mysql_fetch_row(), mysql_fetch_array(), or mysql_fetch_object().

You should read up on these functions at http://php.net in their
function list.

-Kyle-


Message #3 by "Ken Goff" <macessen@a...> on Sun, 9 Feb 2003 07:54:00
> This is because your variable "$result" only contains the actual query
to the database. It is considered a resource. Before you can expect any
kind of extraction from a query, you need to call a MySQL function such
as mysql_result().

So in your case, to print out this value from your MySQL table, you
would need to do this:

$link_id = mysql_connect("localhost","phpuser","phppass");
		if(mysql_select_db("EventDb", $link_id));
$result = mysql_query("SELECT DayType FROM Day WHERE EventDate ='$y-$m
$d'",$link_id);
$daytype = mysql_result($result,0,'DayType');
print $daytype; // or echo $daytype;

The reason I suggested the function mysql_result() in this case, is
because your only trying to extract one row of data from your table. For
a query with a larger result set, you need to use one of the following:

mysql_fetch_row(), mysql_fetch_array(), or mysql_fetch_object().

You should read up on these functions at http://php.net in their
function list.

-Kyle-

Thanks but I must have something incorrectly configured. With these
modifications, I'm now getting an error message of:

"Warning: Unable to jump to row 0 on MySQL result index 2 in /Users. . ."

This following line is identified as having the problem.

$daytype = mysql_result($result,0,'DayType');

I had tried the mysql_result() on my own earlier but got this same kind of
error.

On the bright side, I did see the variable values printed that I expected.
Message #4 by "Ken Goff" <macessen@a...> on Sun, 9 Feb 2003 23:43:12
> > This is because your variable "$result" only contains the actual query
t> o the database. It is considered a resource. Before you can expect any
k> ind of extraction from a query, you need to call a MySQL function such
a> s mysql_result().

> So in your case, to print out this value from your MySQL table, you
w> ould need to do this:

> $link_id = mysql_connect("localhost","phpuser","phppass");
	> 	if(mysql_select_db("EventDb", $link_id));
$> result = mysql_query("SELECT DayType FROM Day WHERE EventDate ='$y-$m
$> d'",$link_id);
$> daytype = mysql_result($result,0,'DayType');
p> rint $daytype; // or echo $daytype;


Why am I getting this following error message?

> "Warning: Unable to jump to row 0 on MySQL result index 2 in /Users. . ."

> This following line is identified as having the problem.

> $daytype = mysql_result($result,0,'DayType');

What can I do to fix this?
Message #5 by Kyle Ketterer <BIGE88FAN@c...> on Mon, 10 Feb 2003 21:22:52 -0500
Usually when you get that error, it means the query didn't return a
result. To avoid that error, place the "@" symbol before the function.
The "@" symbol surpresses error messages.



  Return to Index