Chris
Chris

Reputation: 105

Using mysql_fetch_array to retrieve a field value

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!

Upvotes: 0

Views: 152

Answers (1)

Kermit
Kermit

Reputation: 34055

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.

Upvotes: 4

Related Questions