simple mysql query not returning corretcly

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

simple mysql query not returning corretcly

Post 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".
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post 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()); 
}
?>
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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()); 
?>
Image Image
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply