我正在使用公共函数来查询mysql。一切都在我的本地计算机上运行良好,但一旦上传到godaddy服务器就有问题。基本上,当我调用方法时,$rows
为null。
我确信数据库数据是正确的,查询应返回少量记录。我花了好几个小时试图解决它,但没有运气。我希望你们能帮我解决这个问题。提前谢谢。
代码
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
答案 0 :(得分:1)
我认为问题是代码结构。 尝试:
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>";