Page 1 of 1

simple mysql query not returning corretcly

Posted: Tue Sep 30, 2003 10:06 pm
by SBro
I'm fairly new to php but have done asp and a bit of database work in the past...I have this simple query/code that testing out atm...

<?php

$con = mysql_connect("server", "user", "pass") or die("Connect Error: ".mysql_error());

echo 'connection successful<p>';

$db="test";
mysql_select_db($db, $con);

$sql = "SELECT author FROM t_news";

$result = mysql_query($sql);

echo $result;


// close the connection
mysql_close($con);

?>


Now the problem is when I open up the php page with this code in I get the following:

connection successful
Resource id #2

?? What is "Resource id #2" meant to mean? that is what it's returning as my query result and that is definately not what should be returned, any help would be appreciated thanks.

NB. There is only one record in the database at the moment which is why I haven't added anything else to the query, as I'm just "testing".

Posted: Tue Sep 30, 2003 10:33 pm
by nigma
"mysql_query() returns a resource identifier or FALSE if the query was not executed correctly"
http://us2.php.net/manual/en/function.mysql-query.php

If you want to print all the rows that the query returned do something like:

Code: Select all

@mysql_connect('host','user','pass') or die("Connect Error: mysql_error()"); // The @ supresses errors
mysql_select_db('DBNAME');
$query = mysql_query("select author from t_news");
$rowsReturned = @mysql_num_rows($query);
if ($rowsReturned > 0) // Make sure your query returns some rows
&#123;
  while ($row = mysql_fetch_array($query))
  &#123;
    print $row&#1111;0]; // Since your query will return only one field
  &#125;
&#125;
else
&#123;
  die('No rows were returned.');
&#125;
Hope this helps?

Posted: Tue Sep 30, 2003 11:06 pm
by AnsonM
do it in php..

Code: Select all

<?php
@mysql_connect('host','user','pass') or die("Connect Error: mysql_error()"); // The @ supresses errors 
mysql_select_db('DBNAME'); 
$query = mysql_query("select author from t_news"); 
$rowsReturned = @mysql_num_rows($query); 
if ($rowsReturned > 0) // Make sure your query returns some rows 
{ 
  while ($row = mysql_fetch_array($query)) 
  { 
    print $row[0]; // Since your query will return only one field 
  } 
} 
else 
{ 
  die('No rows were returned.'mysql_error()); 
}
?>

Posted: Wed Oct 01, 2003 12:09 am
by phice
Phiceified code ;)

Code: Select all

<?php 
@mysql_connect("host","user","pass") or die("Connect Error: " . mysql_error()); // The @ supresses errors 
@mysql_select_db("DBNAME");

$query = mysql_query("SELECT `author` FROM `t_news`"); 
$rowsReturned = @mysql_num_rows($query); 

if ($rowsReturned > 0)
  while ($row = mysql_fetch_array($query)) 
    print $row[0];
else 
  die("No rows were returned." . mysql_error()); 
?>

Posted: Wed Oct 01, 2003 7:07 am
by zenabi
phice wrote:

Code: Select all

<?php 
@mysql_connect("host","user","pass") or die("Connect Error: " . mysql_error()); // The @ supresses errors
I've only just noticed the @'s with the mysql functions. Can someone elaborate on what this does, or direct me to a website?

Thanks in advance.

Posted: Wed Oct 01, 2003 7:53 am
by twigletmac
The @ sign suppresses errors - it's often used with database functions because you don't want the autogenerated errors being presented to users if the db is down for some reason.

Mac