CRUD使用prepare语句不起作用。

时间:2014-01-07 21:35:27

标签: php database select mysqli

数据库连接正在运行。类中的SELECT AND UPDATE FUNCTION根本不起作用。它甚至没有显示错误来帮我解决问题。我正在尝试学习如何使用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 .

    ?>

2 个答案:

答案 0 :(得分:0)

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

这是另一个提示:

if($stmt->num_row() >0){ 

请注意,在客户端获取行之前,num_rows()不会返回任何有用的内容。所以在execute()之后调用它几乎可以保证它返回错误的数字。

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

答案 1 :(得分:0)

考虑到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";
   }

}
?>

我评论了我换过的区域,所以你知道我做了什么。