0

I'm trying to display a "Welcome back, <name>." to my blog when I log in back. I'm using php to access the database, get the name and last name of the username currently in $_SESSION['username'], and then print it back in the index.

So the function to query the database is:

 function get_full_name($username){

    $real = array();

    $query = mysql_query("SELECT `name`, `last` FROM `users` WHERE `user` = `{$username}`");

    $row = mysql_num_rows($query);

    foreach($row as $k => $v)
    {
        $real[$k] = $v;
    }

       return $real;
  }

Then the part of the html where it calls the function above:

<div id="menu">
    <?php 
            $temp = $_SESSION['username'];
            $real[] = get_full_name($temp);
            if(isset($_SESSION['username']))
            {
                echo '<br />'.'Welcome back, '. $real['name'] . '.';
            }
    ?>
    </div>

The output of the above codes is:

 Welcome back, .

var_dump($real) gives:

array(1) { [0]=> array(0) { } } 

var_dump($real) after changing to mysql_fectch_assoc:

array(0) { }
  • Fixed: The error was not using the single quotes ' {$username} '

By changing them, it worked like a charm, cheers to all!

7
  • try var_dump($real) to see what indexes it has. I seem to remember mysql_num_rows being the wrong function if you want an associative array back. Commented Jun 12, 2012 at 10:55
  • Go ahead and accept incarnate's answer--it's the right one. Commented Jun 12, 2012 at 11:01
  • I've done what the answer says but, still its not working. Commented Jun 12, 2012 at 11:11
  • Have you done var_dump after using mysql_fetch_assoc? Good way to confirm it's working and to see what you're actually getting back from your indexes. Commented Jun 12, 2012 at 11:13
  • Just added the new var_dump, cheers! Commented Jun 12, 2012 at 11:17

5 Answers 5

2

Curiously you are using mysql_num_rows to get the results of your query. This will only return the number of rows. You'll be better off using mysql_fetch_assoc to get the associative array of results.

You also need to change your backticks to single quotes around {$username} in the query.

Something like:

function get_full_name($username){

    $real = array();

    $query = mysql_query("SELECT `name`, `last` FROM `users` WHERE `user` = '{$username}'");

    $row = mysql_fetch_assoc($query);

    foreach($row as $k => $v)
    {
        $real[$k] = $v;
    }

    return $real;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Somehow this is not working, still showing me no printed name, but i didnt understand why before it wouldnt work at all, this seems logical but still its not working for me =/, must be doing something else wrng?
See also Nick's solution, you need to remove the [] from $real[] = get_full_name($temp); bsdnoobz has an even more comprehensive writeup!
Yes, it worked!! So the error was just the ` {$username} ` quotes instead of '{$username}' ! Thanks a lot!
1

Your get_full_name() function should be like this:

function get_full_name($username) {
   $real  = array();
   $query = mysql_query("SELECT `name`, `last` 
                         FROM `users` 
                         WHERE `user` = '{$username}'");
   if (mysql_num_rows($query)) {
     $real = mysql_fetch_assoc($query);
   }

   return $real;
}

Notes:

  • Use single quotes for the variable: '{$username}' instead of `{$username}`
  • Use mysql_num_rows() for checking if the result is not empty.
  • Use mysql_fetch_assoc() for retrieving the row.

Calling the function:

$temp = $_SESSION['username'];
$real = get_full_name($temp);
if (isset($temp) && count($real)) {
   echo '<br />Welcome back, '. $real['name'] . '.';
}

Notes:

  • Use $real instead of $real[].
  • Check if $real empty with count().

Comments

0

Change this line $row = mysql_fetch_array($query);

Comments

0

Two issues I see.

1) You are using mysql_num_rows, not fetching the array of data from the database.

function get_full_name($username){

    $real = array();

    $query = mysql_query("SELECT `name`, `last` FROM `users` WHERE `user` = `{$username}`");

    $row = mysql_fetch_assoc($query);

    foreach($row as $k => $v)
    {
        $real[$k] = $v;
    }

    return $real;
}

2) Looks like $real is a multi-dimention array in your code. Try this instead:

<div id="menu">
    <?php 
            $temp = $_SESSION['username'];
            $real = get_full_name($temp);
            if(isset($_SESSION['username']))
            {
                echo '<br />'.'Welcome back, '. $real['name'] . '.';
            }
    ?>
    </div>

Notice I've removed the [] at the end of $real.

Comments

0

Use mysql_query() to get the result of running the query, then use mysql_fetch_assoc() to get individual rows.

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
{
   var_dump($row);
}

Note that mysql_num_rows($result) will return the number of rows in a given result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.