For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > March 2006 > Return values from mysql_query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Return values from mysql_query
David Killen

2006-03-23, 7:58 am

I'm just beginning with PHP and came across something which is
frustrating me no end. I have a really simple script which given a
username will query the database and if it doesn't find a match will
then insert into the database. The problem I find is that even when
there is no data in the database to match the return from mysql_query
evaluates to true even though all the documentation and tutorials that I
have read say that mysql_query should return false when using a SELECT
query if there is no match. Am I missing something really simple here or
is there another explaination as to why this might be the case? The
following code snippet exhibits this behaviour. I am running the
following: apache 2.0.54, PHP 5.0.5-2, MySQL 4.1.12.

<?php
$username = "david";
$conn = mysql_connect('localhost','user','pass')
;
mysql_select_db('test',$conn);
$sql = "SELECT * FROM users WHERE username='$username'";
echo $sql."<br/>";
$result = mysql_query($sql,$conn);
echo $result."<br/.>";
if (!$result)
echo "No Match!<br/>";
else
{
$row = mysql_fetch_array($result);
while ($row)
{
echo $row['username'].' -- '.$row['email'].'<br/>';
$row = mysql_fetch_array($result);
}
}
mysql_close();
?>

Cheers,

David
João Cândido de Souza Neto

2006-03-23, 7:58 am

David Killen wrote:

> I'm just beginning with PHP and came across something which is
> frustrating me no end. I have a really simple script which given a
> username will query the database and if it doesn't find a match will
> then insert into the database. The problem I find is that even when
> there is no data in the database to match the return from mysql_query
> evaluates to true even though all the documentation and tutorials that I
> have read say that mysql_query should return false when using a SELECT
> query if there is no match. Am I missing something really simple here or
> is there another explaination as to why this might be the case? The
> following code snippet exhibits this behaviour. I am running the
> following: apache 2.0.54, PHP 5.0.5-2, MySQL 4.1.12.
>
> <?php
> $username = "david";
> $conn = mysql_connect('localhost','user','pass')
;
> mysql_select_db('test',$conn);
> $sql = "SELECT * FROM users WHERE username='$username'";
> echo $sql."<br/>";
> $result = mysql_query($sql,$conn);
> echo $result."<br/.>";
> if (!$result)
> echo "No Match!<br/>";
> else
> {
> $row = mysql_fetch_array($result);
> while ($row)
> {
> echo $row['username'].' -- '.$row['email'].'<br/>';
> $row = mysql_fetch_array($result);
> }
> }
> mysql_close();
> ?>
>
> Cheers,
>
> David


The function mysql_query just returns false when it get an error in that
query. When do you wanto to know if is there some match, you ought to use
mysql_num_rows($result). It'll return the number of lines was found by your
query.

Hope helping.

--
---------------------------------------------------
João Cândido de Souza Neto
Web Developer
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com