0

I'm using the following PHP code to retrieve a name from a table matching an ID:

$qry = "SELECT league_name FROM Leagues where league_id = '".$_SESSION['SESS_LEAGUE_ID']."'";
$row = mysql_fetch_array($qry);
$leaguename = $row['league_name'];

I know that SESS_LEAGUE_ID is correct as I can output it to a variable and see it, however when I try and grab the name that matches the ID from the table Leagues I get nothing back.

$leaguename is always blank. I've checked the database and there should definitely be some text returned. The league_name field is a varchar type.

I know it's something simple that I'm not doing but I can't think what!

Dilip Raj Baral
  • 3,060
  • 6
  • 34
  • 62
Chris
  • 105
  • 4
  • 14
  • 1
    Please do not use mysql* functions in new code, it is deprecated. Try using [PDO](http://php.net/manual/en/book.pdo.php) instead. – zajd Mar 29 '13 at 19:14
  • @zjd Next time, include a link to [this post](http://stackoverflow.com/q/12859942/1190388) – hjpotter92 Mar 29 '13 at 19:15
  • Thanks for the advice on mysql. I'll read up on mysqli and PDO and try to implement them on my site as an alternative :-) – Chris Mar 29 '13 at 19:23
  • If I do a replace on 'mysql' for 'mysqli' using CTRL+F are there any functions that might stop working? – Chris Mar 29 '13 at 19:25

1 Answers1

4

You need to execute the statement

$result = mysql_query($qry);
$row = mysql_fetch_array($result);

Stop using mysql_ functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Kermit
  • 33,827
  • 13
  • 85
  • 121