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

<tfoot id='UfafF'></tfoot>

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

      • <bdo id='UfafF'></bdo><ul id='UfafF'></ul>
    1. <legend id='UfafF'><style id='UfafF'><dir id='UfafF'><q id='UfafF'></q></dir></style></legend>

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

        如何將任意數量的值綁定到 mysqli 中的準備好的語

        How to bind an arbitrary number of values to a prepared statement in mysqli?(如何將任意數量的值綁定到 mysqli 中的準備好的語句?)

      1. <legend id='aqfoA'><style id='aqfoA'><dir id='aqfoA'><q id='aqfoA'></q></dir></style></legend>

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

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

                  本文介紹了如何將任意數量的值綁定到 mysqli 中的準備好的語句?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我真的希望有人花一點時間查看我的代碼.我正在解析一些新聞內容,我可以將初始解析插入到包含新聞 URL 和標題的數據庫中.我想進一步擴展它,傳遞每個文章鏈接并解析文章的內容并將其包含在我的數據庫中.初始解析的工作原理如下:

                  find('div[class=mainBlock]', 0);$items = array();foreach ($main->find('a') as $m){$items[] = '("'.mysql_real_escape_string($m->純文本).'","'.mysql_real_escape_string($m->href).'")';}$reverse = array_reverse($items);mysql_query ("INSERT IGNORE INTO bag_news (article, link) VALUES".(implode(',', $reverse))."");?>

                  如您所見,我使用的是 PHP 簡單 HTML DOM 解析器. 為了擴展,我是嘗試使用 mysqli 語句,我可以在其中綁定參數,以便將所有 html 標記插入到我的數據庫中.我以前用 XML 解析做過這個.問題是我不知道如何綁定數組,看看我的代碼是否正確,是否可以這樣工作......這是完整的代碼:

                  query("SET NAMES 'utf8'");include_once ('simple_html_dom.php');$html = file_get_html('http://basket-planet.com/ru/');//查找主要新聞$main = $html->find('div[class=mainBlock]', 0);$items = array();foreach ($main->find('a') as $m){$h = file_get_html('http://www.basket-planet.com'.$m->href.'');$article = $h->find('div[class=newsItem]');//轉成字符串可以修改內容$a = str_get_html(implode("
                  ", (array)$article));if(isset($a->find('img'))){foreach ($a->find('img') as $img){$img->outertext = '';}}//去掉圖片if(isset($a->find('a'))){foreach ($a->find('a') as $link){$link->href = 'javascript:;';$link->target = '';}}//去掉任何javascriptif(isset($a->find('iframe'))){foreach ($a->find ('iframe') as $frame){$frame->outertext = '';}}//去掉 iframe@$a->find('object', 0)->outertext = '';@$a->find('object', 1)->outertext = '';//修改更多內容以僅檢索文本內容//將整個內容放入一個 div 中(如果語句在這里有效???)$text_content = '

                  '.$a.'
                  '.($a->find('object', 0)->data > 0 ?'find('object', 0)->data.'">播放視頻</a>&nbsp;&nbsp;')($a->find('object', 1)->data > 0 ?'find('object', 1)->data.'">播放視頻</a>&nbsp;&nbsp;')($a->find('iframe[src*=youtube]', 0)->src > 0 ?'<a target="_blank" href="'.$a->find('iframe', 0)->src.'">播放視頻</a>&nbsp;&nbsp;')//進行更多檢查以查看是否存在視頻鏈接.'</div>';$items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")';}//反轉數組,使最新的項目具有最后一個ID$reverse = array_reverse($items);$stmt = $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)");$stmt->bind_param ???;//(內爆(',', $reverse));$stmt->execute();$stmt->close();?>

                  所以邏輯是針對找到的文章的每個 href,我傳遞它來解析內容,并嘗試將其添加到數組中.我可能有很多錯誤,但我還不能測試它,因為我不知道如何綁定它以查看它是否有效.而且我也不確定是否可以在 $text_content div 中執行 if 語句...意思是顯示播放視頻"(如果存在).所以,如果有人能花時間和我一起解決這個問題,我將不勝感激.

                  更新:將 $text_content div 中的 if 語句更改為比較運算符.

                  解決方案

                  這正是 mysqli 真正尷尬的場景.要綁定多個參數,您必須將它們全部作為可變長度參數列表傳遞給 mysql->bind_param(),但棘手的部分是您必須通過引用綁定它們.PHP 中的引用可能非常混亂.

                  這是一個粗略的例子(雖然我沒有測試過這個確切的代碼):

                  $stmt = $mysqli->prepare("INSERT IGNORE INTO test_news(文章, 鏈接, text_cont) VALUES (?,?,?)");foreach ($reverse as &$value) {$params[] = &$value;}array_unshift(str_repeat('s', count($params)));call_user_func_array(array($stmt, 'bind_param'), $params);

                  當我想編寫一個通用函數來將參數綁定到 SQL 時,我發現使用 PDO 要容易得多.不需要綁定,只需將一組值傳遞給 PDOStatement::execute() 方法即可.

                  $stmt = $pdo->prepare("INSERT IGNORE INTO test_news(文章, 鏈接, text_cont) VALUES (?,?,?)");$stmt->execute($reverse);

                  <小時>

                  更新:如果您需要 $items 來包含多行數據,我會這樣做:

                  首先,在構建 $items 時,將其設為數組數組,而不是將值連接在一起:

                  foreach ($main->find('a') as $m){$items[] = array($m->plaintext, $m->href, $text_content);}

                  然后準備一個插入一行的 INSERT 語句,并在 $items 上為每個元組循環執行一次準備好的語句:

                  $stmt = $pdo->prepare("INSERT INTO test_news(文章, 鏈接, text_cont) VALUES (?,?,?)");foreach ($items as $tuple) {$stmt->execute($tuple);}

                  我完全不知道你為什么使用 array_reverse(),我也不知道你為什么使用 INSERT IGNORE,所以我忽略了這些.

                  I would really like for someone to take a little time and look over my code. I'm parsing some news content and I can insert the initial parse into my database which contains the news URL and the title. I'd like to expand it farther, to pass along each article link and parse the content of the article and include it in my database. The initial parsing works perfectly like this:

                  <?php
                  include_once ('connect_to_mysql.php');
                  include_once ('simple_html_dom.php');
                  $html = file_get_html('http://basket-planet.com/ru/');
                  $main = $html->find('div[class=mainBlock]', 0);
                    $items = array();
                    foreach ($main->find('a') as $m){
                      $items[] = '("'.mysql_real_escape_string($m->plaintext).'",
                                  "'.mysql_real_escape_string($m->href).'")';
                    }
                  $reverse = array_reverse($items);
                  mysql_query ("INSERT IGNORE INTO basket_news (article, link) VALUES 
                               ".(implode(',', $reverse))."");
                  ?>
                  

                  As you can see, I'm using PHP Simple HTML DOM Parser. To expand, I'm trying to use mysqli statement where I can bind the parameters so all the html tags get inserted into my database. I've done this before with XML parsing. Problem is I don't know how to bind the array, and see whether my code is correct, if it will work this way... Here's the entire code:

                  <?php
                  $mysqli = new mysqli("localhost", "root", "", "test");
                  $mysqli->query("SET NAMES 'utf8'");
                  include_once ('simple_html_dom.php');
                  $html = file_get_html('http://basket-planet.com/ru/');
                  //find main news
                  $main = $html->find('div[class=mainBlock]', 0);
                  $items = array();
                    foreach ($main->find('a') as $m){
                      $h = file_get_html('http://www.basket-planet.com'.$m->href.'');
                      $article = $h->find('div[class=newsItem]');
                      //convert to string to be able to modify content
                      $a = str_get_html(implode("
                  ", (array)$article));
                        if(isset($a->find('img'))){
                          foreach ($a->find('img') as $img){
                            $img->outertext = '';}} //get rid of images
                        if(isset($a->find('a'))){
                          foreach ($a->find('a') as $link){
                            $link->href = 'javascript:;';
                            $link->target = '';}} //get rid of any javascript
                        if(isset($a->find('iframe'))){
                          foreach ($a->find ('iframe') as $frame){
                            $frame->outertext = '';}} //get rid of iframes
                       @$a->find('object', 0)->outertext = '';
                       @$a->find('object', 1)->outertext = '';
                       //modify some more to retrieve only text content
                       //put entire content into a div (will if statements work here???)
                       $text_content = '<div>'.$a.'<br>'.
                         ($a->find('object', 0)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 0)->data.'">Play Video</a>&nbsp;&nbsp;')
                         ($a->find('object', 1)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 1)->data.'">Play Video</a>&nbsp;&nbsp;')
                         ($a->find('iframe[src*=youtube]', 0)->src > 0 ? '<a target="_blank" href="'.$a->find('iframe', 0)->src.'">Play Video</a>&nbsp;&nbsp;')
                         //couple more checks to see if video links are present
                      .'</div>';
                  $items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")';
                  }
                  //reverse the array so the latest items have the last id
                  $reverse = array_reverse($items);
                  $stmt = $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)");
                  $stmt->bind_param ???; //(implode(',', $reverse));
                  $stmt->execute();
                  $stmt->close();
                  ?>
                  

                  So the logic is for every href of an article found, I'm passing it to parse the content and I'm trying to add it to the array. I probably have a ton of errors but I can't test it yet because I don't know how to bind it to see if it works. And I'm also not sure if I can do the if statements inside $text_content div...meaning to display "Play Video" if they exist. So please, if someone can take time to work on this with me I would really appreciate it.

                  UPDATE: changed the if statements to comparison operators in $text_content div.

                  解決方案

                  This is exactly the scenario where mysqli is really awkward. To bind multiple params, you have to pass them all as a variable-length argument list to mysql->bind_param(), but the tricky part is that you have to bind them by reference. References in PHP can be pretty confusing.

                  Here's an rough example (though I have not tested this exact code):

                  $stmt = $mysqli->prepare("INSERT IGNORE INTO test_news 
                      (article, link, text_cont) VALUES (?,?,?)");
                  foreach ($reverse as &$value) {
                    $params[] = &$value;
                  }
                  array_unshift(str_repeat('s', count($params)));
                  call_user_func_array(array($stmt, 'bind_param'), $params);
                  

                  I find it much easier to use PDO when I want to write a general-purpose function to bind parameters to SQL. No binding is necessary, just pass an array of values to the PDOStatement::execute() method.

                  $stmt = $pdo->prepare("INSERT IGNORE INTO test_news 
                      (article, link, text_cont) VALUES (?,?,?)");
                  $stmt->execute($reverse);
                  


                  Update: if you need $items to contain multiple rows of data, I'd do it this way:

                  First, when building $items, make it an array of arrays, instead of concatenating the values together:

                  foreach ($main->find('a') as $m){
                      $items[] = array($m->plaintext, $m->href, $text_content);
                  }
                  

                  Then prepare an INSERT statement that inserts one row, and loop over $items executing the prepared statement once for each tuple:

                  $stmt = $pdo->prepare("INSERT INTO test_news 
                      (article, link, text_cont) VALUES (?,?,?)");
                  foreach ($items as $tuple) {
                      $stmt->execute($tuple);
                  }
                  

                  I don't know why you were using array_reverse() at all, and I don't know why you were using INSERT IGNORE, so I left those out.

                  這篇關于如何將任意數量的值綁定到 mysqli 中的準備好的語句?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 多次插入)
                  How do I make sure that values from MySQL keep their type in PHP?(如何確保 MySQL 中的值在 PHP 中保持其類型?)
                1. <tfoot id='lA3Vm'></tfoot>

                      <tbody id='lA3Vm'></tbody>
                        <legend id='lA3Vm'><style id='lA3Vm'><dir id='lA3Vm'><q id='lA3Vm'></q></dir></style></legend>

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

                            <bdo id='lA3Vm'></bdo><ul id='lA3Vm'></ul>

                            <i id='lA3Vm'><tr id='lA3Vm'><dt id='lA3Vm'><q id='lA3Vm'><span id='lA3Vm'><b id='lA3Vm'><form id='lA3Vm'><ins id='lA3Vm'></ins><ul id='lA3Vm'></ul><sub id='lA3Vm'></sub></form><legend id='lA3Vm'></legend><bdo id='lA3Vm'><pre id='lA3Vm'><center id='lA3Vm'></center></pre></bdo></b><th id='lA3Vm'></th></span></q></dt></tr></i><div class="80sonev" id='lA3Vm'><tfoot id='lA3Vm'></tfoot><dl id='lA3Vm'><fieldset id='lA3Vm'></fieldset></dl></div>
                            主站蜘蛛池模板: 济南网站建设_济南网站制作_济南网站设计_济南网站建设公司_富库网络旗下模易宝_模板建站 | 路面机械厂家| 搜活动房网—活动房_集装箱活动房_集成房屋_活动房屋 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 温室大棚建设|水肥一体化|物联网系统| 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 耐磨焊丝,堆焊焊丝,耐磨药芯焊丝,碳化钨焊丝-北京耐默公司 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 工业PH计|工业ph酸度计|在线PH计价格-合肥卓尔仪器仪表有限公司 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 非甲烷总烃分析仪|环控百科 | 哔咔漫画网页版在线_下载入口访问指引| 华中线缆有限公司-电缆厂|电缆厂家|电线电缆厂家 | 润滑油加盟_润滑油厂家_润滑油品牌-深圳市沃丹润滑科技有限公司 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 快速门厂家-快速卷帘门-工业快速门-硬质快速门-西朗门业 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | CTAB,表面活性剂1631溴型(十六烷基三甲基溴化铵)-上海升纬化工原料有限公司 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 加热制冷恒温循环器-加热制冷循环油浴-杭州庚雨仪器有限公司 | 济南菜鸟驿站广告|青岛快递车车体|社区媒体-抖音|墙体广告-山东揽胜广告传媒有限公司 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 361°官方网站| Honsberg流量计-Greisinger真空表-气压计-上海欧臻机电设备有限公司 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | 震动筛选机|震动分筛机|筛粉机|振筛机|振荡筛-振动筛分设备专业生产厂家高服机械 | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 越南专线物流_东莞国际物流_东南亚专线物流_行通物流 | ALC墙板_ALC轻质隔墙板_隔音防火墙板_轻质隔墙材料-湖北博悦佳 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 新密高铝耐火砖,轻质保温砖价格,浇注料厂家直销-郑州荣盛窑炉耐火材料有限公司 | 注浆压力变送器-高温熔体传感器-矿用压力传感器|ZHYQ朝辉 | 浙江自考_浙江自学考试网| 长城人品牌官网|