PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
So, I'm not sure what I'm doing wrong. I'm trying to have a php script search the database and extract the information it finds, and if no entry is found, list some declared values. It currently returns
[text]Resource id #4$[/text] When it should actually return some values from the database
Here's what I've got so far.
There's a syntax error on your first mysql_query() because you're missing a single quote at the end - just look at the syntax highlighting to see what I mean. I can't offer any insight into the "Tester Resource id #4$" message though - maybe someone else can!
function lookup($wanted) //Part number of the item this form is for.
{
$result = mysql_query('SELECT * from test WHERE id = "$wanted"');
echo $result; //Lets see what $result is
if (!result)
{
$price = 'Inquire';
echo $price;
}
else
{
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$price = '$ '.$row['price'];
echo $price;
}
}
}
I'm not familiar with what the issue might be, so in the meantime you could try searching for "Resource id #4" on the forum or search engines. It looks like it's to do with MySQL in any case. I don't see where in the script the DB is that you're trying to connect to, plus I noticed that you're not checking to see whether the MySQL query completed successfully, as this would tell you if there was a problem with the query. Where you've got this:
The point of using die() is that it will stop the script from executing. You can also replace it with your own custom error handler if necessary. I forgot to mention that you can also echo out mysql_error() to get the actual message.
You might want to have a look at the mysql_query page in the PHP manual:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.