Can someone please help with below query. The code mentioned below is not returning FirstName and LastName. Why?
Cheers, Akhil G
<?php
$username = mysql_real_escape_string($_GET['user']);
$firstname ="" ;
$lastname ="";
if (ctype_alnum($username))
{
//check user exist
$check = mysql_query("SELECT username, first_name, last_name FROM syn where username = '$username'");
if (mysql_num_rows($check) === 1)
{
$get = mysql_fetch_array($check);
$username = $get['username'];
$firstname = $get['first_name'];
$lastname = $get['last_name'];
} else
echo "<h2> User Does Not Exist ! </h2>";
exit();
}
?>
you are using the wrong fetch function
$get = mysql_fetch_array($check);
stored data in numbered positions in the array, so the returned data is stored in $get[0], $get[1] and $get[2].
you can see the contents by using print_r($get);
Instead use
$get = mysql_fetch_assoc($check);
this stores the data in the associated array you require.
try this..
<?php
$username = mysql_real_escape_string($_GET['user']);
$firstname ="" ;
$lastname ="";
if (ctype_alnum($username))
{
//check user exist
$check = mysql_query("SELECT username, first_name, last_name FROM syn where username = '$username'");
if (!$check)
{
$get = mysql_fetch_row($check);
$username = $get[0];
$firstname = $get[1];
$lastname = $get[2];
} else
echo "<h2> User Does Not Exist ! </h2>";
exit();
}
?>
Thanks Guys .. but seems I am out of luck .. nothing seems working .. tried all the options suggested .. below is the modified version which again not printiing firstname lastname or username
<?php
include("./inc/header.inc.php");
?>
<?php
if (isset($_GET['user']))
{
$username = mysql_real_escape_string($_GET['user']);
$firstname ="" ;
$lastname ="";
if (ctype_alnum($username))
{
//check user exist
$check = mysql_query("SELECT username, first_name, last_name FROM syn where username = '$username'");
if (mysql_num_rows($check) === 1)
{
$get = mysql_fetch_assoc($check);
$username = $get['username'];
$firstname = $get['first_name'];
$lastname = $get['last_name'];
} else
echo "<meta http-equiv =\"refresh\" content=\"0; url = http://localhost:8080/FirstTry/Tests/index.php\">";
exit();
}
}
?>
<div class="postForm" > Post form will go in here .. </div>
<div class="postPosts" > Your Post will go in here ..</div>
<img src ="" height="250" width="200" alt ="<?php echo $firstname; ?>'s profile" title="<? echo $firstname; ?>'s profile"/>