乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      php – 添加prepare語句結(jié)果警告

       印度阿三17 2019-08-30

      我正在嘗試使用prepare語句創(chuàng)建最佳的全局選擇查詢,除了我收到警告之外,一切正常:

      警告:mysqli_stmt_bind_result():綁定變量數(shù)與預(yù)準備語句中的字段數(shù)不匹配

      獨特的全局選擇功能

      function querysp($selectquery, $type_bind, $colval) {
          global $db;
          $stmt = $db->prepare($selectquery);
          $stmt->bind_param($type_bind,$colval);
          $stmt->execute();
          mysqli_stmt_bind_result($stmt, $colval);
          $arr = array();
          if ($stmt) {
              while ($result = mysqli_stmt_fetch($stmt)) {
                  array_push($arr, $result);
              }
          }
          return $arr;    
      }
      

      示例使用:

      $select = "SELECT * from advertising WHERE status = ?";
      $status = 1;
      $advertising = querysp($select, 'i', $status);
      foreach ($advertising as $ad) {
          $banner[] = $ad['banner'];
          $bannercode[] = $ad['bannercode'];
          $location[] = $ad['location'];
          $status = $ad['status'];
      }
      

      我錯過了什么?很抱歉沒有完全準備好staments,也在SO和谷歌檢查,但無法解決這個問題.

      編輯2

      我已經(jīng)更改了選擇查詢,因為我將綁定參數(shù)的類型從b(我認為這意味著布爾值)更改為i,但仍然收到錯誤.

      編輯3 – 當前版本 – 仍然得到相同的錯誤:

      $select = "SELECT banner, bannercode, location, status from advertising WHERE status = ?";
      $status = 1;
      $advertising = querysp($select, 'i', $status);
      foreach ($advertising as $ad) {
          $banner[] = $ad['banner'];
          $bannercode[] = $ad['bannercode'];
          $location[] = $ad['location'];
          $status = $ad['status'];
      }
      

      和功能

      function querysp($selectquery, $type_bind, $colval) {
          global $db;
          $stmt = $db->prepare($selectquery);
          $stmt->bind_param($type_bind,$colval);
          $stmt->execute();
          $stmt->bind_result($result);
          $arr = array();
          while($stmt->fetch()) {
              $arr[] = $result;
          }
          $stmt->close();
          return $arr;
      }
      

      解決方法:

      花了一些時間,但這里是我準備好的陳述的版本,它是一個相當?shù)膲?但它幾乎捕獲了可能產(chǎn)生的大多數(shù)錯誤.我試著在這里和那里添加一些文檔來解釋會發(fā)生什么.只需逐步閱讀,您就可以理解會發(fā)生什么.如果有任何不清楚的地方提問.

      要使用下面發(fā)布的類,請執(zhí)行此操作.

      $query  = "SELECT ? FROM ?"; // can be any query
      $params = array($param1, $param2); //must equal to ammount of "?" in query.
      //an error will occur if $param1 or 2 is not of the type string, int, 
      //bool or double, it can however be a double like this 2,1 instead of 2.1
      $db = new databaseHandler();
      $result = $db->runQuery($query, $params);
      //or for short runQuery("SELECT * FROM *" , array());
      if($result !== false){
          while($row = mysqli_fetch_array($result){
              $column1 = $row['columnname'];
              //just do with the result what you want.
          }
      }else{
          echo "error occured";
      }
      

      這是能夠處理數(shù)據(jù)庫交互的類.不要以你想要的方式設(shè)置連接.您可以對此運行所有類型的查詢.

      class databaseHandler{
      
      private $x = ""; //required
      
      //$query = the query, $params = array with params can be empty.
      public function runQuery($query, Array $params){
          if($this->checkparams($params, $query)){
              //starts and returns the database connection
              $con = startConnection();    //<--- CHANGE THIS SO IT WORKS FOR YOU
              if($stmt = $con->prepare($query)){
                  //obtains type of params if there are any.
                  if(count($params) < 0){
                      $type = "";
                      $i=0;
                      foreach($params as $par){
                          $par = $this->checktype($par);
                          $params[$i] = $par;
                          $type = $this->setType($type);
                          if($type === false){
                              echo "type error occured"
                              return false;
                          }
                          $i  ;
                      }
                      //sets $type on the first spot of the array.
                      array_unshift($params, $type)
                      //binds the params
                      call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
                  }
                  $stmt->execute();
                  $result - $stmt->get_result();
                  stmt->close();
                  return $result; // return the result of the query.
              }else{
                  echo "error occured, bad connection";
                  return false;
          }else{
              echo "params dont match prepared statement";
              return false;
          }
      }
      
      //checks if params are equal to prepared statement requirements.
      checkparams($params, $query){
          //counts occurences of ? in query.
          $count = substr_count($q, "?");
          $i = count($params);
          if($count == $i){
              return true;
          }else{
              return false;
          }
      }
      
      //Checks the type of a param
      public function checktype($par){
          $this->x = gettype($par);
          if($this->x == "string"){
              $npar = str_replace(",", ".", $par);
              if(is_numeric($npar)){
                  $this->x = "integer";
                  if(strpos($npar, ".")){
                      $this->x="double";
                      return $npar;
                  }
              }
          }
          return $par;
      }
      
      //sets/adds to the type.
      public function setType($type){
          //$this->x from checktype;
          switch($this->x){
              case "string":
              $type = $type."s";
              break;
              case "integer":
              $type = $type."i";
              break;
              case "double":
              $type = $type."d";
              break;
              case "boolean":
              $type = $type."b";
              break;
              case "NULL":
              $type = $type."s";
              default:
                  return false;
          }
          return $type; 
      }
      
      //This function exist to resolve a few version errors in php.
      //not sure what it does exactly, but it resolved some errors I had in the past.               
      function refValues($arr){
          if(strnatcmp(phpversion(),'5.3') >= 0){
              $refs = array();
              foreach($arr as $key => $value)
                  $refs[$key] = &$arr[$key];
              return $refs;
          }
          return $arr;
      }
      }
      }
      

      所以這里發(fā)生的是一組執(zhí)行查詢的檢查,如果出現(xiàn)任何問題,如果沒有出錯則返回false,即使結(jié)果為空,也會返回查詢結(jié)果.也有可能不在課堂上完成所有這些,盡管這會使$x全局化.我認為最好修改某些東西,使它們最適合您的應(yīng)用程序.像錯誤處理/消息變量名稱等

      此代碼唯一不能保護您的是查詢中的錯誤.

      編輯—-我發(fā)現(xiàn)這個代碼沒有防止的東西,插入NULL值.我更改了此代碼以防止插入NULL值,它們將作為類型字符串插入.數(shù)據(jù)庫將看到其NULL并將其插入為NULL值.

      只是不要嘗試插入對象或空值,因為這無論如何都是無用的.

      來源:https://www./content-1-426651.html

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多