Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:mysql语句有问题。_Php_Mysql_Class - Fatal编程技术网

PHP:mysql语句有问题。

PHP:mysql语句有问题。,php,mysql,class,Php,Mysql,Class,我有一个公共函数为mysql做查询。在我的本地计算机上,一切都很好,但一旦上传到godaddy服务器,就会出现问题。基本上,当我调用该方法时,$rows是空的 我确信数据库数据是正确的,查询应该返回很少的记录。我花了几个小时想弄明白,但运气不好。我希望你们能帮我解决这个问题。提前谢谢 代码 类测试{ 公共函数getEmployees($username,$password){ $stmt=mysqli\u prepare($this->connection, “选择名称、密码 来自员工 其中na

我有一个公共函数为mysql做查询。在我的本地计算机上,一切都很好,但一旦上传到godaddy服务器,就会出现问题。基本上,当我调用该方法时,
$rows
是空的

我确信数据库数据是正确的,查询应该返回很少的记录。我花了几个小时想弄明白,但运气不好。我希望你们能帮我解决这个问题。提前谢谢

代码

类测试{
公共函数getEmployees($username,$password){
$stmt=mysqli\u prepare($this->connection,
“选择名称、密码
来自员工
其中name='$username'&&password='$password'
");     
$this->throweExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throweExceptionOnError();
$rows=array();
while(mysqli_stmt_fetch($stmt)){
$rows[]=$row;
$row=新的stdClass();
mysqli\u stmt\u bind\u结果($stmt,$row->name,$row->password);
}
如果(mysqli_stmt_num_rows($stmt)==0){
返回false;
}否则{
返回$rows;
}
mysqli_stmt_free_结果($stmt);
mysqli_close($this->connection);
}
}
$test=新测试();
$row=$test->getEmployees('bob','1111');
回声“;
打印(行);
回声“;
//它不在页面上打印任何内容

我认为问题在于代码结构。 尝试:

类测试{
公共函数getEmployees($username,$password){
$stmt=mysqli\u prepare($this->connection,
“选择名称、密码
来自员工
其中name='$username'&&password='$password'
");     
$this->throweExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throweExceptionOnError();
$rows=array();
$row=新的stdClass();
mysqli\u stmt\u bind\u结果($stmt,$row->name,$row->password);
while(mysqli_stmt_fetch($stmt)){
$rows[]=$row;
}
mysqli_stmt_free_结果($stmt);
mysqli_close($this->connection);
$temp=mysqli\u stmt\u num\u行($stmt);
如果($temp==0){
返回false;
}否则{
返回$rows;
}
}
}
$test=新测试();
$row=$test->getEmployees('bob','1111');
回声“;
打印(行);
回声“;

删除此行$row=new stdClass();还有tryThanks的伙计们,我刚想出来。我必须将mysqli_stmt_num_行($stmt)==0更改为mysqli_stmt_num_行($stmt)。不知道为什么,谢谢你。请看我的评论。你的代码也适用于我。我会给你绿色的D
    class test{

     public function getEmployees($username,$password) {



         $stmt = mysqli_prepare($this->connection,
              "SELECT name, password
               FROM employee
               WHERE name='$username' && password='$password'
               ");     

          $this->throwExceptionOnError();

          mysqli_stmt_execute($stmt);
          $this->throwExceptionOnError();

          $rows = array();


          while (mysqli_stmt_fetch($stmt)) {
              $rows[] = $row;
              $row = new stdClass();
              mysqli_stmt_bind_result($stmt,  $row->name, $row->password);
          }
          if(mysqli_stmt_num_rows($stmt)==0){

              return false;
          }else{
              return $rows;
          }



          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
   }

}

$test=new test();
$row=$test->getEmployees('bob','1111');
echo "<pre>";
print_r($row);
echo "</pre>";

//it prints nothing on the page
class test{

     public function getEmployees($username,$password) {



         $stmt = mysqli_prepare($this->connection,
              "SELECT name, password
               FROM employee
               WHERE name='$username' && password='$password'
               ");     

          $this->throwExceptionOnError();

          mysqli_stmt_execute($stmt);
          $this->throwExceptionOnError();

          $rows = array();
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt,  $row->name, $row->password);

          while (mysqli_stmt_fetch($stmt)) {
              $rows[] = $row;

          }

          mysqli_stmt_free_result($stmt);
          mysqli_close($this->connection);
          $temp = mysqli_stmt_num_rows($stmt); 
          if($temp==0){

              return false;
          }else{
              return $rows;
          }



   }

}

$test=new test();
$row=$test->getEmployees('bob','1111');
echo "<pre>";
print_r($row);
echo "</pre>";