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
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php