-3

I'm unable to display a particular record in PHP. This is my code:

<?php

session_start();
$name = $_SESSION['MM_Username'];
mysql_connect("localhost","abc","sar");
mysql_select_db("abc");
$query = mysql_query("select * from employee WHERE username = '$name'");

while($row = mysql_fetch_row($query));
{ 
  echo $row['employee_id'];
}   

?>

This does not display anything. What am I doing wrong?

Mischa
  • 42,876
  • 8
  • 99
  • 111
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Lawrence Cherone Jul 03 '13 at 13:44
  • try `print_r($row);` to see what is in that variable – Jakolcz Jul 03 '13 at 13:45
  • So where is your error handling? [mysql_error()](http://php.net/manual/en/function.mysql-error.php) – Anigel Jul 03 '13 at 13:46
  • 3
    You're mixing up mysql_fetch_row, which gives you a numerically indexed array of results; with mysql_fetch_array, which gives you an associative array. – andrewsi Jul 03 '13 at 13:46

5 Answers5

6

You are using mysql_fetch_row which returns numerical array. You should be using mysql_fetch_assoc, for eg.

while($row = mysql_fetch_assoc($query))
{ 
  echo $row['employee_id'];
} 

Please read through the documentation of both:

http://php.net/manual/en/function.mysql-fetch-row.php

http://www.php.net/manual/en/function.mysql-fetch-assoc.php

Note: Also please don't ignore the warning. Start using either PDO or mysqli.

Mischa
  • 42,876
  • 8
  • 99
  • 111
vee
  • 38,255
  • 7
  • 74
  • 78
  • I did that..even used mysqli..but still no progress..did the error handeling too ut it ain't showing any error – user2546670 Jul 03 '13 at 14:18
  • Is that semicolon in `while($row = mysqli_fetch_assoc($query));` intentional? You won't see syntax errors with that semicolon but the while block does not execute with that semicolon. Please remove it and give it a try. – vee Jul 03 '13 at 14:22
0

Did you even debug your work? Does $_SESSION['MM_Username']; contain anything? Try print_r($_SESSION); to make sure.

You can also echo just your query and test it directly on your Database to make sure there any expected results showing up.

If you did this and it still is not working: please provide more information.

Bjorn 'Bjeaurn' S
  • 3,861
  • 2
  • 18
  • 29
  • I've already checked this...the print_r($_SESSION) is giving the correct username and group – user2546670 Jul 03 '13 at 13:51
  • As I said; add additional info because we can't see what your expected results are (from the Database), what the current information in MM_Username is. It could possibly go wrong on so many levels now: Your MySQL statement might be invalid because you're not escaping a special character, the MySQL field might be case sensitive which you forgot to take into account, I don't know. Give us some data to work with. – Bjorn 'Bjeaurn' S Jul 03 '13 at 13:54
  • @BjornSchijff, no additional info is necessary. See vinodadhikary's answer. – Mischa Jul 03 '13 at 13:55
  • @Mischa Of course, if he had debugged the results he had gotten (or checked the number of results), he could have figured this out himself. Debugging is key. – Bjorn 'Bjeaurn' S Jul 03 '13 at 13:56
0
<?php
session_start();
$name=$_SESSION['MM_Username'];
mysql_connect("localhost","abc","sar");
mysql_select_db("abc");
$query = "select * from employee WHERE username =  '$name';";
$result = mysql_query($query)or die(mysql_error());    
while($row = mysql_fetch_assoc($result));
{ 
echo $row['employee_id'];
}   
?>

or something like that

Strawberry
  • 33,750
  • 13
  • 40
  • 57
0

First enable error reporting :

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

Then follow code below :

<?php
session_start();
$name = $_SESSION['MM_Username'];

mysql_connect("localhost","abc","sar") or die("No Connection"); //Add or die() to record any error if connection is failed
mysql_select_db("abc") or die("can't select");

$query = mysql_query("select * from `employee` WHERE username = '".$name."'") or die("Wrong query"); //makesure table name with backtick eg: `tablename`

while($row = mysql_fetch_array($query));  //also can use : mysql_fetch_assoc($query)
{ 
  echo $row['employee_id']; //print output
}   
?>
  1. Enable error reporting
  2. Add or die() to check if failed
  3. Always use backtick for table name . Why? check here
  4. Try use mysql_fetch_array($query) or mysql_fetch_assoc($query)
  5. And please don't use mysql_* in future. Why ? check here
Community
  • 1
  • 1
rusly
  • 1,504
  • 6
  • 28
  • 62
0

You can also use mysql_fetch_array($query, MYSQL_ASSOC) but please use mysqli_fetch_array($result, MYSQL_ASSOC) because mysql_ is deprecated.

And in future if you have some problem with your code please call funcion var_dump() for debugging reasons, or you can set xdebug in your php.ini file and connected with your environment tool (like NetBeans or something similar).

If you are interested in NetBeans and xdebug you can visit this link.