Php 使用prepare语句的CRUD无效。

Php 使用prepare语句的CRUD无效。,php,database,select,mysqli,Php,Database,Select,Mysqli,数据库连接正在工作。类中的SELECT和UPDATE函数根本不起作用。它甚至没有显示错误来帮助我解决问题。我正在尝试学习如何使用prepare、bind param和execute语句。请有人帮我看一下代码,并告诉我可能有什么问题。只是花了很多时间在这上面,根本不知道问题出在哪里。我是一个新手,正在写我的第一个代码。非常感谢 <?php class connect_dbase{ public $mysqli; public function connectio

数据库连接正在工作。类中的SELECT和UPDATE函数根本不起作用。它甚至没有显示错误来帮助我解决问题。我正在尝试学习如何使用prepare、bind param和execute语句。请有人帮我看一下代码,并告诉我可能有什么问题。只是花了很多时间在这上面,根本不知道问题出在哪里。我是一个新手,正在写我的第一个代码。非常感谢

 <?php class connect_dbase{
       public $mysqli;
       public function connection($host="localhost",$user="root",$password="london",$db_name="users")       
        {
            $this->mysqli=new mysqli($host,$user,$password,$db_name);
            if ($this->mysqli->connect_error) {
               die('Connect Error: ' . $this->mysqli->connect_error);   
            }
            else{
                echo " Database connection successful";
                }           
        }       
        public function display_all($id){
                   if($stmt = $this->mysqli->prepare("SELECT * FROM user WHERE id =?")){
                      /* bind parameters for markers */
                      $stmt->bind_param('i',$id);

                      /* execute query */
                      $stmt->execute();

                      if($stmt->num_row() >0){ 
                         echo 'Total results: ' . $resultrol->num_rows;
                     $result = $stmt->get_result();

                         while ($row = $result->fetch_assoc()) {
                               echo $row['name'];
                               echo $row['email'];
                               echo $row['address'];}  
                  }    
                      else {  echo "no result found";}
       }    
       else
       {
        echo "cant prepare result";     
       }    
    }

    public function update_post($name, $address,$email,$mob,$id)
        {
            $up="UPDATE user SET name=?, address =?,email=?,mobile=? WHERE id =?";

            if($stmt=$mysqli->prepare($up))
            {
                $stmt->bind_param("sssii", $name, $address,$email,$mob,$id);

                            if($stmt->excute()) { 
                              echo " post updated";
                  header('location:index.php'); 
                }
                else
                {
                   echo "post not executed";

                }
            }else{ echo " cannot prepare statement";}

            }

    }
    $connect_dbase=new connect_dbase();     
    $connect_dbase->connection();
    $connect_dbase->display_all(2);
    $connect_dbase-> update_post("john","kkkkk","kkk@yahoo.com",98765,2);

    // These 2 functions- $connect_dbase->display_all(2); and 
    $connect_dbase-> update_post("john","kkkkk","kkk@yahoo.com",98765,2); are not working when called from the class above .

    ?>

我同意@MikeBrant的评论。如果您想在尝试调用之前确保连接成功,那么应该在构造函数中进行连接

这里还有一个提示:

if($stmt->num_row() >0){ 
请注意,在客户端获取行之前,num_rows()不会返回任何有用的内容。因此,在execute()之后立即调用它几乎肯定会使它返回错误的数字


您需要使用将结果集从服务器传输到客户端,然后num_rows()将起作用。但是,如果结果集非常大,请小心,它可能会占用太多内存。

考虑到Mike和Bill的输入,我已修改了您的代码,使其能够正常工作。它可能需要更多的工作,但至少应该给你一个起点。我创建了一个包含三个字段的测试数据库,id、name和email,但是您应该能够插入自己的数据库和字段,并且仍然可以使用

<?php 
class connect_dbase {
    public $mysqli;

    public function connection($host="localhost",$user="root",$password="",$db_name="test")       
    {
        $this->mysqli=new mysqli($host,$user,$password,$db_name);

        if ($this->mysqli->connect_error) {
            die('Connect Error: ' . $this->mysqli->connect_error);   
        } else {
            // return a true value here if successful, that way you can check
            // if your connection was established
            return true;
        }           
    }

    public function display_all($id){
        if($stmt = $this->mysqli->prepare("SELECT * FROM test WHERE id =?")) {
            // some minor changes to the bind and execute statments.  I
            // wrapped them in an if just to make sure there were no errors
            // if i had more time i might make these more elegant rather than just
            // echoing them out
            /* bind parameters for markers */
            if(!($stmt->bind_param('i',$id))) {
                echo $stmt->error;
            }

            /* execute query */
            if(!($stmt->execute())) {
                echo $stmt->error;
            }

            // You could also bind the results to specific variables here and return those
            //$stmt->bind_result($id,$name,$email);
            //$stmt->fetch();
            //$result = $name;
            //assign the results to a variable and then return that variable
            //rather than processing the results here
            $result = $stmt->get_result();

            return $result;
        } else {
            // if an error occurs return the error, once again another place for
            // improvement but at the very least will show you an error
            echo $this->mysqli->error;

        }    
    }

public function update_post($name, $email, $id)
{
    $up="UPDATE test SET name=?, email=? WHERE id =?";
    // originally had $mysqli->prepare($up), fixed syntax
    if($stmt = $this->mysqli->prepare($up))
    {
        //$stmt->bind_param("sssii", $name, $address,$email,$mob,$id);
        $stmt->bind_param("ssi", $name, $email,$id);
        // execute was spelled wrong
        if($stmt->execute()) { 
            return true; 
        } else {
            return $stmt->error;
           //return false;
        }
    } else {
        return false;
    }

 }

}

// set up database connection
$connect_dbase = new connect_dbase();

if($connect_dbase->connection()) {
   // if connection was successful, call display_all
   // and assign the results to $result                
   $result = $connect_dbase->display_all(2);
   // you could do a foreach here also but since there
   // was only one result i just echoed the values
   while($row = $result->fetch_array()) {
       echo $row['id'] . "<br/>";
       echo $row['name'] . "<br/>";
       echo $row['email'] . "<br/>";

   } 

   // then call update_post 

   $update_result = $connect_dbase->update_post("asdf","asdf@yahoo.com",2);
   // show a message if the update_post was successful
   if($update_result) {
        echo "Update successful";
   }

}
?>

我对我切换的区域进行了评论,这样你就知道我做了什么

“不工作”是什么意思?什么不起作用?您做了哪些调试工作?几句忠告。您不应该在特定于DB的类中放置任何逻辑来执行诸如回显消息或执行重定向之类的操作(顺便说一句,在输出完成后,这些操作将不起作用)。在尝试对DB连接进行操作之前,还应确保您的DB连接已实际建立。