tags:

views:

127

answers:

6

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";
    }
}
A: 

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

BastiBense
no it connected perfectly it returned everythign but the username of who sent the pm
H4cKL0rD
+4  A: 

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.

Tatu Ulmanen
+4  A: 

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) {
wallyk
+1  A: 

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

fauxtrot
That shouldn't matter in MySQL
Louis
Thanks... Like I said, It's been a while since I used PHP/MySQL
fauxtrot
+3  A: 

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'.

dar7yl
+1 for bringing up a valid alternative. But it seems almost like cheating since it doesn't answer the primary question.
wallyk
No actually it does anwser and works perfectly +1 and Thank you very much.
H4cKL0rD
you're very welcome.
dar7yl
A: 

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?

amir beygi