【问题标题】:Getting "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given" [duplicate]获取“警告:mysql_num_rows() 期望参数 1 是资源,给定的布尔值”[重复]
【发布时间】:2013-10-20 16:31:58
【问题描述】:

根据第 14 行的标题,我收到一个错误。我对 php 很陌生,我登录了,这段代码在登录到索引页面时抛出了一个错误。不过我可以登录(不太确定如何登录)。

<?php
include 'functions.php';
include_once("config.php");
session_start();
if(!isset($_SESSION["email"])){
  header("location: login.php");
  exit();
}
$id = preg_replace('#[^0-9]#i', '',$_SESSION["id"]);
$email = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["email"]);
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]);
include "db_connect.php";
$sql=mysql_query("SELECT * FROM 'members' WHERE id - ? LIMIT 1"); // query the person
$existCount=mysql_num_rows($sql); // count the nums
if ($existCount==1){//evaluate the count
  echo "Your login session data is not on record in the database";
  exit();
}
?>

【问题讨论】:

  • 如果没有看到您的代码,我们将无法提供帮助。
  • 谷歌你的错误信息,然后咨询mysql_error()
  • 我已经编辑过了,抱歉
  • 请参阅this answer 了解如何解决此问题。
  • 你不应该使用 mysql_* 函数。

标签: php mysql


【解决方案1】:

您的 SQL 语句不完整:

而不是

$sql=mysql_query("SELECT * FROM 'members' WHERE id - ? LIMIT 1"); // 查询人

使用这个

$sql=mysql_query("SELECT * FROM members WHERE id ="."'".$id."'"." and password ="."'".$password."' LIMIT 1"); // 查询人

这当然是假设您按原样使用密码(这不是一个好主意)。您应该在将其存储到数据库之前对其进行编码。

【讨论】:

    【解决方案2】:

    只需像这样更正您的查询语句:

    <?php
     $sql = "SELECT * FROM `members` WHERE id = '{$id}' LIMIT 1";
     // `members` can also be simply members only but don't use quotes like ('', or "")
     ?>
    

    查询此语句,您将摆脱问题。
    一点建议 mysql_* 已正式弃用,因此请使用 mysqliPDO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2016-12-02
      • 1970-01-01
      • 2011-03-26
      • 2011-12-07
      相关资源
      最近更新 更多