PDA

View Full Version : Resource ID#4?


yo_da84
01-13-2005, 07:30 PM
Hi Guys,
im new to PHP and MySQL. im trying to get a very very very simple query from a MySQL db using the following code, but it keeps giving me
"Resource ID#4"

Any ideas?
###########################################
<?php
//Call the MySQL location, user and password from the mysql_db.php file//
require("mysql_db.php")
?>
<?php
$query="SELECT * FROM products";
$results=mysql_query($query)
or die(mysql_error());
echo $results;
?>
###########################################

cheers
Yo_da84

raf
01-13-2005, 07:44 PM
welcome here!

mysql_query() returns a resource-id, which is a handle to get the actual data from the recordset. But you need to us a function like mysql_fetch_assoc() to get the actual date. The traditional code to do this is:

<?php
//Call the MySQL location, user and password from the mysql_db.php file//
require("mysql_db.php")
$query="SELECT var1, var2, var3 FROM products";
// where var1 etc are the names of your columns. Never use * inside embedded sql
$result=mysql_query($query) or die('Queryproblem: ' . mysql_error() . '<br />Executed query: ' . $query);
if (mysql_num_rows($result) >= '1'){
while ($row = mysql_fetch_assoc($result)){
echo $row['var1']; //to print out the value of column 'var1' for each record
}
}else{
echo 'No records found.';
}
?>