php - Mysqli 无法获取信息或bind_params

标签 php mysql mysqli null prepared-statement

所以我不确定这里出了什么问题,我尝试了多种方法,但无法让它工作,我按照手册操作,但它似乎没有改变,我有一个简单的登录.php ,我已经检查了我的查询,但它可以在 mysql 上运行,我有错误报告,我怀疑这可能是因为我正在 WAMP 服务器上测试它,但我不确定这是否有什么关系请帮忙。

已编辑

<?php
 session_start();
 ERROR_REPORTING( E_ALL | E_STRICT );
 ini_set('display_errors',1);error_reporting(-1);
 include_once 'UniversalConnect.php';
 class login{

 public function __construct()
 {
 $this->dologin();
 }

 private function dologin()
 {
 $name = $_POST['name'];
 $pass = $_POST['pass'];
 $mysqli = new mysqli("127.0.0.1","root","root","mrt");
 var_dump($mysqli);
 $sql="SELECT * FROM administradores WHERE nombre_administrador= ? AND password= ?";
 $stmt = $mysqli->prepare($sql);
 if ( !$stmt ) {
 printf('errno: %d, error: %s', $mysqli->errno, $mysqli->error);
 die;
 }
 $stmt->bind_param("ss",$name,$pass);
 if ( !$name ) {
 printf('errno: %d, error: %s', $stmt->errno, $stmt->error);
 }
 $stmt->execute();
 $stmt->store_result();
 if($stmt->num_rows==1)
 {
 var_dump($rows);
 header("location: indexSCAF.html");
 else{
 $errmsg_arr[] = 'Username and Password are not found';
 $errflag = true;
 }
 if($errflag) {
 $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
 session_write_close();
 exit();
 }
 }

 }/*close function dologin*/
 }/*close class */
?>

var_dumps 的结果给出

 string 'admin' (length=5)
 string 'test' (length=4)

 object(mysqli)[3]
 public 'affected_rows' => null
 public 'client_info' => null
 public 'client_version' => null
 public 'connect_errno' => null
 public 'connect_error' => null
 public 'errno' => null
 public 'error' => null
 public 'error_list' => null
 public 'field_count' => null
 public 'host_info' => null
 public 'info' => null
 public 'insert_id' => null
 public 'server_info' => null
 public 'server_version' => null
 public 'stat' => null
 public 'sqlstate' => null
 public 'protocol_version' => null
 public 'thread_id' => null
 public 'warning_count' => null

 object(mysqli_stmt)[4]
 public 'affected_rows' => null
 public 'insert_id' => null
 public 'num_rows' => null
 public 'param_count' => null
 public 'field_count' => null
 public 'errno' => null
 public 'error' => null
 public 'error_list' => null
 public 'sqlstate' => null
 public 'id' => null

object(mysqli_stmt)[4]
 public 'affected_rows' => null
  public 'insert_id' => null
  public 'num_rows' => null
 public 'param_count' => null
 public 'field_count' => null
 public 'errno' => null
 public 'error' => null
 public 'error_list' => null
 public 'sqlstate' => null
 public 'id' => null

 null

最佳答案

在获取结果之前,您需要使用bind_result,以便您的准备语句能够正确获取。

$mysqli = new mysqli("127.0.0.1","root","root","mrt");
$sql = " SELECT userId,userName From user WHERE nombre_administrador= ? AND password= ? ";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("ss",$user,$pass);
$stmt->execute();
$stmt->store_result();

if($stmt->num_rows>0)
 {
   $stmt->bind_result($userId,$userName)
  while($stmt->fetch())
   {
     // success handle result 
      header("location: indexSACF.html");
   }
 }
else
{
  $errmsg_arr[] = 'Username and Password are not found';
  $errflag = true;
}

$stmt->free_result()
$stmt->close();

注意:在查询语句中切勿使用 select *,因为它不会绑定(bind)您的结果。而是使用参数

SELECT userId,userName  From user WHERE nombre_administrador= ? AND password= ? ; 

更多信息您可以引用this

关于php - Mysqli 无法获取信息或bind_params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26344654/

相关文章:

php - 通过 AJAX 使用 GET 发送大量文本

php - AWS S3 无法通过 PHP SDK 删除存储桶中的对象

php - 单击下拉菜单中的选项时,如何从数据库获取数据以填充表单中的特定字段?

php - 使用 FILTER_SANITIZE_NUMBER_INT 后在查询中使用 MySQL LIMIT

php - mysqli_connect 会在脚本结束时自动关闭吗?

php - 如何将文档转换为 PDF

php - 使用 HTML/CSS 设置 PHP 数组 POST 输出的样式(使其美观)

c++ - 将 MySQL 日期时间转换为 C++ std::time_t

mysql - uml问题——泛化

php - 当我将数据插入数据库时​​,显示错误列数与第 1 行的值计数不匹配