简体   繁体   English

如何从PHP中的数据库查询返回数值?

[英]How do I return the numeric value from a database query in PHP?

I am looking to retreive a numerical value from the database 我正在寻找从数据库检索数值

function adminLevel()
{
   $q = "SELECT userlevel FROM ".TBL_USERS." WHERE id = '$_SESSION[id]'";
   return mysql_query($q, $this->connection);
}

This is the SQL. 这就是SQL。

I then wrote the following php/html: 然后,我编写了以下php / html:

    <?php 
    $q = $database->adminLevel();
    if ($q > 7)
    {
        ?>
        <a href="newleague.php">Create a new league</a>
        <?
    }
    ?>

The problem I have is that the userlevel returned isn't affecting the if statement. 我的问题是返回的用户级别不会影响if语句。 It is always displayed. 它总是显示。 How do i get it to test the value of userlevel is greater than 7? 我如何测试用户级别的值大于7?

Thanks 谢谢

mysql_query returns a resource. mysql_query返回资源。 You need to fetch data from that resource by using some of the mysql_fetch_* functions, such as mysql_fetch_row http://php.net/mysql_fetch_row 您需要使用某些mysql_fetch_ *函数从该资源中获取数据,例如mysql_fetch_row http://php.net/mysql_fetch_row

adminlevel() doesn't return an integer here. adminlevel()在此不返回整数。 It returns a mysql resultset object containing a single row and a single column; 它返回一个包含单行和单列的mysql结果集对象; the data point contained within happens to be an integer. 其中包含的数据点恰好是整数。 Presumably, however PHP compares that object to integers happens to alwayus result in its being larger than 7. But it's not comparing the integer you wanted to compare. 大概,但是PHP将对象与整数进行比较总是会导致它大于7。但这并不是在比较您要比较的整数。 Try this: 尝试这个:

function adminLevel()
{
   $q = "SELECT userlevel FROM ".TBL_USERS." WHERE id = '$_SESSION[id]'";
   $r = mysql_query($q, $this->connection);
   if ( $r and mysql_num_rows($r) ) {
       $s = mysql_fetch_assoc($r);
       return $s['userlevel'];
   } else {
       // error handling; your query failed or returned no rows
   }
}

mysql_query returns a resource. mysql_query返回资源。 You then need to fetch the result from this. 然后,您需要从中获取结果。 eg 例如

function adminLevel()
{
  $q = "SELECT userlevel FROM ".TBL_USERS." WHERE id = '$_SESSION[id]'";
  $result = mysql_query($q, $this->connection);
  if ($row = mysql_fetch_assoc($result)) {
    $level = $row['userlevel'];
  } else {
    $level = 0; // some default value
  }

  // free the result resource after using it
  mysql_free_result($result);
  return $level;
}

If you think about it, in the general case a query can return multiple results so you need a way to retrieve each of these in turn (eg a while loop over mysql_fetch_assoc ) but in this specific case as you are selecing the userlevel by id you will retrieve either 0 or 1 rows so we can use an if to check for a matching row. 如果你想想看,在一般情况下的查询,就能让您需要一种方法来检索每个这些反过来又返回多个结果(例如, while遍历mysql_fetch_assoc ),但因为你是selecing此特定情况下, userlevelid你将检索0或1行,因此我们可以使用if检查匹配的行。

See the documentation for mysql_query for more details. 有关更多详细信息,请参见mysql_query文档

You are comparing a mysql resource with an integer, so you can expect strange results. 您正在将mysql资源与整数进行比较,因此可以预期到奇怪的结果。 Use mysql_result() to fetch one cell from this resource. 使用mysql_result()从此资源中获取一个单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM