pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

        <bdo id='03fRw'></bdo><ul id='03fRw'></ul>

        <small id='03fRw'></small><noframes id='03fRw'>

      1. <i id='03fRw'><tr id='03fRw'><dt id='03fRw'><q id='03fRw'><span id='03fRw'><b id='03fRw'><form id='03fRw'><ins id='03fRw'></ins><ul id='03fRw'></ul><sub id='03fRw'></sub></form><legend id='03fRw'></legend><bdo id='03fRw'><pre id='03fRw'><center id='03fRw'></center></pre></bdo></b><th id='03fRw'></th></span></q></dt></tr></i><div class="ss20eeq" id='03fRw'><tfoot id='03fRw'></tfoot><dl id='03fRw'><fieldset id='03fRw'></fieldset></dl></div>

      2. <legend id='03fRw'><style id='03fRw'><dir id='03fRw'><q id='03fRw'></q></dir></style></legend>

        <tfoot id='03fRw'></tfoot>

        如何確保 MySQL 中的值在 PHP 中保持其類型?

        How do I make sure that values from MySQL keep their type in PHP?(如何確保 MySQL 中的值在 PHP 中保持其類型?)
          <bdo id='CklFr'></bdo><ul id='CklFr'></ul>

          <small id='CklFr'></small><noframes id='CklFr'>

              <tbody id='CklFr'></tbody>
            • <i id='CklFr'><tr id='CklFr'><dt id='CklFr'><q id='CklFr'><span id='CklFr'><b id='CklFr'><form id='CklFr'><ins id='CklFr'></ins><ul id='CklFr'></ul><sub id='CklFr'></sub></form><legend id='CklFr'></legend><bdo id='CklFr'><pre id='CklFr'><center id='CklFr'></center></pre></bdo></b><th id='CklFr'></th></span></q></dt></tr></i><div class="cq0so2u" id='CklFr'><tfoot id='CklFr'></tfoot><dl id='CklFr'><fieldset id='CklFr'></fieldset></dl></div>
                <tfoot id='CklFr'></tfoot>

                <legend id='CklFr'><style id='CklFr'><dir id='CklFr'><q id='CklFr'></q></dir></style></legend>

                1. 本文介紹了如何確保 MySQL 中的值在 PHP 中保持其類型?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我的 PHP 腳本出現問題,盡管在數據庫中標記為 inttinyint,但從 MySQL 調用的值仍以字符串形式返回.

                  這是一個問題,因為在將基于 MySQL 日期的數組轉換為 JSON 數據時,應該是整數的值被放在雙引號中,這會在使用該 JSON 數據的 Javascript 和 iPhone 應用程序中引起問題.我得到的 JSON 值看起來像 "key" : "1",而我想要的是 "key" : 1.

                  經過一些研究,似乎應該可以將值作為其原生類型獲取,只要一個有 PHP 5.3,并安裝了 mysqlnd 模塊.我有 5.3.3 并且 phpinfo() 似乎表明我已經安裝并運行了 mysqlnd 模塊:

                  mysqlnd 已啟用版本 mysqlnd 5.0.10 - 20111026

                  但是,我的值仍然作為字符串返回.

                  我已經查看了 mysqlnd 的 PHP 手冊條目,它是總是有可能我錯過了顯而易見的事情,但我沒有看到任何表明我需要在代碼中執行任何特定操作才能獲得本機值的內容.

                  我究竟該怎么做才能在 PHP 中獲取 MySQL 函數以提供本機類型的 MySQL 結果?

                  <小時>

                  為了方便下面的回答,這是我用來連接數據庫的命令:

                  私有函數databaseConnect(){$this->mysqli = new mysqli(Database::$DB_SERVER, Database::$DB_USERNAME, Database::$DB_PASSWORD);$this->mysqli->set_charset("utf8");返回真;}私有函數 dbConnect(){數據庫::$USE_MYSQLI = extension_loaded('mysqli');if (!$this->databaseConnect()){echo "無法連接到數據庫服務器";拋出新的異常();}if (!$this->databaseSelectDB()){echo "數據庫服務器已連接,但系統找不到正確的數據庫";拋出新的異常();}}私有函數databaseQuery($query){返回 $this->mysqli->query($query);}公共函數 doQuery($query){$result = $this->databaseQuery($query);如果($result == FALSE){//ErrorHandler::backtrace();die("此查詢無效:$query");}返回 $result;}私有函數 getRows($table, $matches, $orderBy = array(), $limit = array()){$calcFoundRows = '';如果(計數($限制)> 0){$calcFoundRows = 'SQL_CALC_FOUND_ROWS';}$query = '選擇' .$calcFoundRows .' * 從 ' .$table;如果(計數($匹配)> 0){$query .= 'WHERE';$keys = array_keys($matches);$first = 真;foreach ($keys 作為 $key){如果 (!$first){$query .= ' AND ';}$first = 假;//現在他可以安全地添加到查詢中//這是一個數組的唯一時間是當它被 getSelectedUsers 或 getSelectedArticles 調用時//在這種情況下,它是一個數組的數組,因為鍵(即列名)可能有多個//一個條件如果 (is_array($matches[$key])){$firstForColumn = 真;foreach ($matches[$key] 作為 $conditions){如果 (!$firstForColumn){$query .= ' AND ';}$firstForColumn = 假;//如果值是一個數組,我們會生成一個 OR 選擇如果 (is_array($conditions[1])){$firstOr = true;$query .= '(';foreach ($conditions[1] 作為 $value){如果 (!$firstOr){$query .= '或';}$firstOr = false;//在將他放入查詢之前清理這個人$this->cleanMySQLData($value);if ($conditions[0] == 選擇::$CONTAINS){//$query .= 'MATCH (' . $key . ') AGAINST (' . $value . ') ';$value = trim($value, "'");$value = "'%" .$價值."%'";$query .= $key .' 喜歡 ' .$值;}別的{$query .= $key .' ' .$條件[0].' ' .$值;}}$query .= ')';}別的{//在將他放入查詢之前清理這個人$var = $conditions[1];$this->cleanMySQLData($var);if ($conditions[0] == 選擇::$CONTAINS){//$query .= 'MATCH (' . $key . ') AGAINST (' . $var . ') ';$var = trim($var, "'");$var = "'%" .$var ."%'";$query .= $key .' 喜歡 ' .$var;}別的{$query .= $key .' ' .$條件[0].' ' .$var;}}}}別的{//在將他放入查詢之前清理這個人$this->cleanMySQLData($matches[$key]);$query .= $key .=".$matches[$key];}}}如果(計數($orderBy)> 0){$query .= " ORDER BY ";$first = 真;foreach ($orderBy as $orderCol){如果 (!$first){$query .= ',';}$query .= $orderCol;$first = 假;}}如果(計數($限制)> 0){$query .= ' LIMIT ' .$limit[0];如果(計數($限制)> 1){$query .= ',' .$限制[1];}}$result = $this->doQuery($query);$data = 數組();while ($row = $this->databaseFetchAssoc($result)){$data[] = $row;}如果 (strlen($calcFoundRows) > 0){$numRows = $this->databaseCountFoundRows();$key = '^^' .$表.'_selectionCount';Session::getSession()->putUserSubstitution($key, $numRows);}返回 $data;}

                  解決方案

                  我究竟該怎么做才能在 PHP 中獲取 MySQL 函數以提供本機類型的 MySQL 結果?

                  您連接到數據庫,然后準備查詢,執行它,綁定結果,然后獲取它.

                  讓我們逐行執行這些步驟:

                  $conn = new Mysqli('localhost', 'testuser', 'test', 'test');$stmt = $conn->prepare("SELECT id FROM config LIMIT 1");$stmt->execute();$stmt->bind_result($id);$stmt->fetch();var_dump($id);# 這是一個整數!

                  這對我有用.由于您編寫的代碼更加復雜,因此您需要找到查詢數據庫的位置.檢查您是否正在使用 Mysqli::prepare(),如果沒有,介紹一下.

                  您還需要使用 Mysqli_Stmt::execute() 和然后 Mysqli_Stmt::bind_result() 否則(這里是整數)類型不是為該結果列保留.

                  I am having a problem in my PHP script where values called from MySQL are being returned as strings, despite being marked in the database as int and tinyint.

                  This is a problem because when converting an array based on MySQL date into JSON data, values that should be integers are placed in double quotes, which is causing trouble in both Javascript and iPhone apps that use that JSON data. I am getting JSON values that look like "key" : "1", when what I want is "key" : 1.

                  After doing some research, it seems that it should be possible to get the values as their native type so long as one has PHP 5.3, and the mysqlnd module installed. I have 5.3.3 and phpinfo() seems to indicate I have the mysqlnd module installed and running:

                  mysqlnd enabled
                  Version mysqlnd 5.0.10 - 20111026
                  

                  However, my values are still being returned as strings.

                  I have looked at the PHP manual entry for mysqlnd, and it's always possible I'm missing the obvious, but I don't see anything that indicates I need to do anything specific in my code to get the native values.

                  What exactly do I do to get my MySQL functions in PHP to give me the MySQL results in their native type?


                  In order to fascillitate an answer below, this is the command I use to connect to the database:

                  private function databaseConnect()
                  {
                      $this->mysqli = new mysqli(Database::$DB_SERVER, Database::$DB_USERNAME, Database::$DB_PASSWORD);
                      $this->mysqli->set_charset("utf8");
                      return true;
                  }
                  
                  private function dbConnect()
                  {
                      Database::$USE_MYSQLI = extension_loaded('mysqli');
                      if (!$this->databaseConnect())
                      {
                          echo "Cannot Connect To The Database Server";
                          throw new Exception();
                      }
                      if (!$this->databaseSelectDB())
                      {
                          echo "The database server connected, but the system could not find the right database";
                          throw new Exception();
                      }
                  }
                  
                  private function databaseQuery($query)
                  {
                      return $this->mysqli->query($query);
                  }
                  
                  public function doQuery($query)
                  {
                      $result = $this->databaseQuery($query);
                      if ($result == FALSE)
                      {
                          //ErrorHandler::backtrace();
                          die("This query did not work: $query");
                      }
                      return $result;
                  }
                  
                  private function getRows($table, $matches, $orderBy = array(), $limit = array())
                  {
                      $calcFoundRows = '';
                      if (count($limit) > 0)
                      {
                          $calcFoundRows = ' SQL_CALC_FOUND_ROWS';
                      }
                      $query = 'SELECT ' . $calcFoundRows . ' * FROM ' . $table;
                      if (count($matches) > 0)
                      {
                          $query .= ' WHERE ';
                          $keys = array_keys($matches);
                          $first = true;
                          foreach ($keys as $key)
                          {
                              if (!$first)
                              {
                                  $query .= ' AND ';
                              }
                              $first = false;
                  
                              // now he is safe to add to the query
                              // the only time this is an array is when this is called by getSelectedUsers or getSelectedArticles
                              // in that case it is an array of array's as the key (which is the column name) may have more than
                              // one condition
                              if (is_array($matches[$key]))
                              {
                                  $firstForColumn = true;
                                  foreach ($matches[$key] as $conditions)
                                  {
                                      if (!$firstForColumn)
                                      {
                                          $query .= ' AND ';
                                      }
                                      $firstForColumn = false;
                  
                                      // if the value is an array we generate an OR selection
                                      if (is_array($conditions[1]))
                                      {
                                          $firstOr = true;
                                          $query .= '(';
                  
                                          foreach ($conditions[1] as $value)
                                          {
                                              if (!$firstOr)
                                              {
                                                  $query .= ' OR ';
                                              }
                                              $firstOr = false;
                                              // clean this guy before putting him into the query
                                              $this->cleanMySQLData($value);
                                              if ($conditions[0] == Selection::$CONTAINS)
                                              {
                                                  //$query .= 'MATCH (' . $key . ') AGAINST (' . $value . ') ';
                                                  $value = trim($value, "'");
                                                  $value = "'%" . $value . "%'";
                                                  $query .= $key . ' LIKE ' . $value;
                                              }
                                              else
                                              {
                                                  $query .= $key . ' ' . $conditions[0] . ' ' . $value;
                                              }
                                          }
                  
                                          $query .= ')';
                                      }
                                      else
                                      {
                                          // clean this guy before putting him into the query
                                          $var = $conditions[1];
                                          $this->cleanMySQLData($var);
                                          if ($conditions[0] == Selection::$CONTAINS)
                                          {
                                              //$query .= 'MATCH (' . $key . ') AGAINST (' . $var . ') ';
                                              $var = trim($var, "'");
                                              $var = "'%" . $var . "%'";
                                              $query .= $key . ' LIKE ' . $var;
                                          }
                                          else
                                          {
                                              $query .= $key . ' ' . $conditions[0] . ' ' . $var;
                                          }
                                      }
                                  }
                              }
                              else
                              {
                                  // clean this guy before putting him into the query
                                  $this->cleanMySQLData($matches[$key]);
                                  $query .= $key . " = " . $matches[$key];
                              }
                          }
                      }
                      if (count($orderBy) > 0)
                      {
                          $query .= " ORDER BY ";
                          $first = true;
                          foreach ($orderBy as $orderCol)
                          {
                              if (!$first)
                              {
                                  $query .= ',';
                              }
                              $query .= $orderCol;
                              $first = false;
                          }
                      }
                  
                      if (count($limit) > 0)
                      {
                          $query .= ' LIMIT ' . $limit[0];
                          if (count($limit) > 1)
                          {
                              $query .= ',' . $limit[1];
                          }
                      }
                  
                  
                      $result = $this->doQuery($query);
                      $data = array();
                      while ($row = $this->databaseFetchAssoc($result))
                      {
                          $data[] = $row;
                      }
                      if (strlen($calcFoundRows) > 0)
                      {
                          $numRows = $this->databaseCountFoundRows();
                          $key = '^^' . $table . '_selectionCount';
                          Session::getSession()->putUserSubstitution($key, $numRows);
                      }
                  
                      return $data;
                  }
                  

                  解決方案

                  What exactly do I do to get my MySQL functions in PHP to give me the MySQL results in their native type?

                  You connect to the database, then you prepare your query, execute it, bind the result and then you fetch it.

                  Let's do these steps line-by-line:

                  $conn = new Mysqli('localhost', 'testuser', 'test', 'test');
                  $stmt = $conn->prepare("SELECT id FROM config LIMIT 1");
                  $stmt->execute();
                  $stmt->bind_result($id);
                  $stmt->fetch();
                  var_dump($id); # it's an int!
                  

                  This works for me. As you wrote your code is more complex, you will need to locate the place where you query the database. Check that you're using Mysqli::prepare() and if not, introduce it.

                  You will also need to use Mysqli_Stmt::execute() and then Mysqli_Stmt::bind_result() otherwise the (here integer) type is not preserved for that result column.

                  這篇關于如何確保 MySQL 中的值在 PHP 中保持其類型?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  store_result() and get_result() in mysql returns false(mysql 中的 store_result() 和 get_result() 返回 false)
                  Call to undefined function mysqli_result::num_rows()(調用未定義的函數 mysqli_result::num_rows())
                  PHP Prepared Statement Problems(PHP 準備好的語句問題)
                  mysqli_fetch_array returning only one result(mysqli_fetch_array 只返回一個結果)
                  PHP MySQLi Multiple Inserts(PHP MySQLi 多次插入)
                  Fatal error: Call to undefined method mysqli::error()(致命錯誤:調用未定義的方法 mysqli::error())
                          <tbody id='JjgZl'></tbody>

                      • <small id='JjgZl'></small><noframes id='JjgZl'>

                          <i id='JjgZl'><tr id='JjgZl'><dt id='JjgZl'><q id='JjgZl'><span id='JjgZl'><b id='JjgZl'><form id='JjgZl'><ins id='JjgZl'></ins><ul id='JjgZl'></ul><sub id='JjgZl'></sub></form><legend id='JjgZl'></legend><bdo id='JjgZl'><pre id='JjgZl'><center id='JjgZl'></center></pre></bdo></b><th id='JjgZl'></th></span></q></dt></tr></i><div class="cg0iiwi" id='JjgZl'><tfoot id='JjgZl'></tfoot><dl id='JjgZl'><fieldset id='JjgZl'></fieldset></dl></div>
                          <tfoot id='JjgZl'></tfoot>

                          <legend id='JjgZl'><style id='JjgZl'><dir id='JjgZl'><q id='JjgZl'></q></dir></style></legend>

                            <bdo id='JjgZl'></bdo><ul id='JjgZl'></ul>
                            主站蜘蛛池模板: 「阿尔法设计官网」工业设计_产品设计_产品外观设计 深圳工业设计公司 | 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 达利园物流科技集团- | 强效碱性清洗剂-实验室中性清洗剂-食品级高纯氮气发生器-上海润榕科学器材有限公司 | 大型冰雕-景区冰雕展制作公司,3D创意设计源头厂家-[赛北冰雕] | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | 东莞螺丝|东莞螺丝厂|东莞不锈钢螺丝|东莞组合螺丝|东莞精密螺丝厂家-东莞利浩五金专业紧固件厂家 | 离子色谱自动进样器-青岛艾力析实验科技有限公司 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司 | 云南标线|昆明划线|道路标线|交通标线-就选云南云路施工公司-云南云路科技有限公司 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 广东西屋电气有限公司-广东西屋电气有限公司 | 精密五金加工厂-CNC数控车床加工_冲压件|蜗杆|螺杆加工「新锦泰」 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 中矗模型-深圳中矗模型设计有限公司 | 深圳诚暄fpc首页-柔性线路板,fpc柔性线路板打样生产厂家 | 实战IT培训机构_IT培训班选大学生IT技术培训中心_中公优就业 | 电力测功机,电涡流测功机,磁粉制动器,南通远辰曳引机测试台 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 硫化罐-胶管硫化罐-山东鑫泰鑫智能装备有限公司 | 地磅-地秤-江阴/无锡地磅-江阴天亿计量设备有限公司_ | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 土壤养分检测仪_肥料养分检测仪_土壤水分检测仪-山东莱恩德仪器 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 罗茨真空机组,立式无油往复真空泵,2BV水环真空泵-力侨真空科技 | 陕西华春网络科技股份有限公司 | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 无痕胶_可移胶_无痕双面胶带_可移无痕胶厂家-东莞凯峰 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 成都APP开发-成都App定制-成都app开发公司-【未来久】 | 耐高温电缆厂家-远洋高温电缆| 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 房间温控器|LonWorks|海思 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 武汉不干胶印刷_标签设计印刷_不干胶标签印刷厂 - 武汉不干胶标签印刷厂家 |