Reputation: 804
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) { }
By changing them, it worked like a charm, cheers to all!
Upvotes: 0
Views: 218
Reputation: 8415
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;
}
Upvotes: 2
Reputation: 16462
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:
'{$username}'
instead of `{$username}`
mysql_num_rows()
for checking if the result is not empty.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:
$real
instead of $real[]
.$real
empty with count()
.Upvotes: 1
Reputation: 542
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.
Upvotes: 0
Reputation: 6346
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
.
Upvotes: 0