【问题标题】:Retrieving num of rows in MySQLI检索 MySQLI 中的行数
【发布时间】:2012-06-25 16:01:38
【问题描述】:

我正在尝试获取查询中的行数,但它一直给我这个错误

尝试在第 15 行获取非对象的属性。

<?php require_once("./includes/Utilities.php") ;?>
<?php require_once("./includes/db_connection.php");?>
<?php 
    if(isset($_POST['submit'])){
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        $conf_pass = trim($_POST['conf_password']);
        $email = trim($_POST['email']);
        require_once('./includes/CheckPassword.php');
        $check_pwd = new Db_CheckPassword($password);
        $check_pwd =$check_pwd->check();
        require_once('./includes/CheckUsername.php');
        $sql = "SELECT * FROM accounts WHERE username = {$username}";
        $result = $conn->query($sql);
        $numRows = $result->num_rows;
    }
?>

这是第 15 行

$numRows = $result->num_rows;

为什么会返回错误?

【问题讨论】:

  • 当你使用 MySQLi 时,你没有使用准备好的语句!这导致了与旧数据库驱动程序引入的相同类型的 SQL 注入漏洞!
  • mysqli存储过程和执行查询有什么区别?
  • 我不是说存储过程,我是说prepared statements
  • 抱歉,打错了,所以这就是他们的不同之处?强烈建议使用准备好的语句吗?为什么?
  • extermely建议使用准备好的语句。在批量执行多个查询时,它可以提高性能,并且它还会自动转义您输入的任何变量。

标签: php object mysqli


【解决方案1】:

您没有检查您的查询是否成功,因此 $result 实际上可能包含 FALSE。

【讨论】:

    【解决方案2】:

    因为没有引用用户名。

    试试这个:

    $sql = "SELECT * FROM accounts WHERE username = '{$username}'";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      相关资源
      最近更新 更多