0

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();
            }   
?>
Happy Coding
  • 2,517
  • 1
  • 13
  • 24
  • what is your output? – Syed mohamed aladeen Aug 27 '15 at 13:07
  • 1
    You are sending data 'user' via GET or POST? – bicho Aug 27 '15 at 13:07
  • Your code may syntax errors. Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. – Funk Forty Niner Aug 27 '15 at 13:07
  • Your code doesn't `print` anything in case a result is fetched. So how you know it doesn't return first/last name? (And for directed advise, include debug information, such as variable states, database content sample, etc.) – mario Aug 27 '15 at 13:08
  • Hii.. once check ur query in the database whether its returning any records / not... – phpfresher Aug 27 '15 at 13:24
  • 1
    `mysql_` functions are deprecated in PHP and you should not write anything new using them: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Jamesking56 Aug 27 '15 at 13:32
  • what's with the `if (mysql_num_rows($check) === 1)`? Shouldn't that be `if (mysql_num_rows($check) == 1)` instead (remove an equals sign)? – Richard Chambers Aug 27 '15 at 15:33
  • Richard, this is the identity operator. Since mysql_num_rows returns an integer, this is the prefered way to check it's value. http://php.net/manual/en/language.operators.comparison.php – Peter Aug 27 '15 at 15:36

3 Answers3

0

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.

0

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();
            }   
?>
Fake Face
  • 116
  • 4
0

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"/>


  • How about you start by doing some debugging put a die clause on your mysql_query and print out mysql_error(). generate your sql statement separately and echo it out and check its valid sql, run it direct against your db to ensure it returns something. Once you've ruled out sql errors then look at your php. are you sure the user variable is received via a $_GET? not a $_POST? help yourself and help us to help you debug it properly first! – Dave Aug 28 '15 at 11:50
  • also in code above your if statement has bad syntax. – Dave Aug 28 '15 at 11:50
  • Hi Dave, thanks pointing it out .. I made a small tweak in the code and it worked ... I had to comment out the second 'if' condition to make it work .... not sure this is the best way to code this but for now i will got with it //if (ctype_alnum($username)) – Akhil Prasad Godiyal Aug 28 '15 at 14:47
  • your php install may not include `ctype_alnum()` by default eg: if gentoo. you can replace that bit with a small regex should do the same thing `preg_match('/^[a-zA-Z0-9]+$/', $var).` – Dave Aug 28 '15 at 16:02