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!

4
  • 1
    Please do not use mysql* functions in new code, it is deprecated. Try using PDO instead. Commented Mar 29, 2013 at 19:14
  • @zjd Next time, include a link to this post Commented Mar 29, 2013 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 :-) Commented Mar 29, 2013 at 19:23
  • If I do a replace on 'mysql' for 'mysqli' using CTRL+F are there any functions that might stop working? Commented Mar 29, 2013 at 19:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Works as intended. Thanks for the quick response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.