douzhong5902 2013-03-29 19:12
浏览 41
已采纳

使用mysql_fetch_array检索字段值

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!

  • 写回答

1条回答 默认 最新

  • duanchoupo1104 2013-03-29 19:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?