H4cKL0rD
H4cKL0rD

Reputation: 5508

What is wrong with mysql_num_rows()?

I have this code to get the user name that a friend has messaged to someone like user to from message. mysql_num_rows is not working and my user names come up as unknown. Does this have something to do with mysql_num_rows()?

function getusername($userid) {
    $sql = "SELECT username FROM user WHERE `id` = '".$userid."' LIMIT 1";
    $result = mysql_query($sql);
    // Check if there is someone with this id
    if(mysql_num_rows($result)) {
        // if yes get his username
        $row = mysql_fetch_row($result);
        return $row[0];
    } else {
        // if not, name him Unknown
        return "Unknown";
    }
}

Upvotes: 0

Views: 1389

Answers (6)

amir beygi
amir beygi

Reputation: 1272

Global solution to problems like this

try :

$sql = "SELECT username FROM user WHERE `id` = '".$userid."' LIMIT 1"; 
echo "Query=$sql";

and run displayed value in phpmyadmin->SQL, check does it works?

Upvotes: 0

wallyk
wallyk

Reputation: 57774

Before checking the number of rows, check that the query succeeded:

$result = mysql_query($sql);
if (!$result)
{
     error();  // whatever needs to be done
     return;
}

// Check the number of rows
if (mysql_num_rows($result) > 0) {

Upvotes: 4

dar7yl
dar7yl

Reputation: 3747

You don't have to use mysql_num_rows() at all. vis:

   function getusername($userid) 
   {
        $sql = "SELECT username FROM user WHERE `id` = '$userid' LIMIT 1";
        $result = mysql_query($sql);
        // Check if there is someone with this id
        if ( $result && ($row = mysql_fetch_row($result)) )
            return $row[0];
        else 
            // if not, name him Unknown
            return "Unknown";
    }

edit: also, you don't have to escape out of the string: ".$userid." ... that's why you used the "str" form, instead of 'str'.

Upvotes: 3

Todd Richardson
Todd Richardson

Reputation: 1129

I haven't worked with mysql in a long time, so forgive me if I'm worng.

It seems to me that your constraint is checking to see if the column id is equal to a string

'".$userid."'

If id is a integer of some sort, then this check might fail. (again, I may be wrong, but whats the harm in trying?)

You might try modifying the query to to read

$sql = "SELECT username FROM user WHERE `id` = ".$userid." LIMIT 1";

unless id is truly a string.

I hope this helps.

http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

Upvotes: 1

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

Try echoing the query and see if the ID is placed in properly, then make sure that such an ID actually exists, then, after executing the query run echo mysql_error() to see if there were any errors. That should cover all the bases and give you the answer you need.

Upvotes: 4

BastiBen
BastiBen

Reputation: 19870

You could check if there was an mySQL error for the query. Possibly there was a problem connecting to the database.

In the past I used to forget to select a database after connecting.

http://www.php.net/manual/en/function.mysql-error.php

Upvotes: 0

Related Questions