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".
simple mysql query not returning corretcly
Moderator: General Moderators
"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:
Hope this helps?
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
{
while ($row = mysql_fetch_array($query))
{
print $rowї0]; // Since your query will return only one field
}
}
else
{
die('No rows were returned.');
}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());
}
?>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());
?>I've only just noticed the @'s with the mysql functions. Can someone elaborate on what this does, or direct me to a website?phice wrote:Code: Select all
<?php @mysql_connect("host","user","pass") or die("Connect Error: " . mysql_error()); // The @ supresses errors
Thanks in advance.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
