【问题标题】:How to bind an arbitrary number of values to a prepared statement in mysqli? [duplicate]如何将任意数量的值绑定到 mysqli 中的准备语句? [复制]
【发布时间】:2013-04-10 16:28:13
【问题描述】:

我真的希望有人花一点时间来查看我的代码。我正在解析一些新闻内容,我可以将初始解析插入到包含新闻 URL 和标题的数据库中。我想进一步扩展它,传递每个文章链接并解析文章的内容并将其包含在我的数据库中。初始解析完美地像这样工作:

<?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))."");
?>

如您所见,我正在使用PHP Simple HTML DOM Parser. 进行扩展,我正在尝试使用 mysqli 语句来绑定参数,以便将所有 html 标记插入到我的数据库中。我以前用 XML 解析做过这个。问题是我不知道如何绑定数组,看看我的代码是否正确,如果它会这样工作......这是整个代码:

<?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("\n", (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();
?>

所以逻辑是针对找到的文章的每个href,我将其传递以解析内容,并尝试将其添加到数组中。我可能有很多错误,但我还不能测试它,因为我不知道如何绑定它以查看它是否有效。而且我也不确定我是否可以在 $text_content div 中执行 if 语句...意思是如果存在“播放视频”则显示它们。所以,如果有人能花时间和我一起工作,我将不胜感激。

更新:将 if 语句更改为 $text_content div 中的比较运算符。

【问题讨论】:

  • 格式太差了..我没看到foreach ($main已关闭..请看看PSR-2-coding-style
  • 我关闭了它...你能看看代码吗?将所有内容输入 $items 数组后,foreach 将关闭。

标签: php mysqli


【解决方案1】:

这正是mysqli真正尴尬的场景。要绑定多个参数,您必须将它们作为可变长度参数列表传递给 mysql->bind_param(),但棘手的部分是您必须通过引用来绑定它们。 PHP 中的引用可能会很混乱。

这是一个粗略的例子(虽然我没有测试过这个确切的代码):

$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);

当我想编写一个通用函数来将参数绑定到 SQL 时,我发现使用 PDO 要容易得多。不需要绑定,只需将一组值传递给 PDOStatement::execute() 方法即可。

$stmt = $pdo->prepare("INSERT IGNORE INTO test_news 
    (article, link, 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 
    (article, link, text_cont) VALUES (?,?,?)");
foreach ($items as $tuple) {
    $stmt->execute($tuple);
}

我根本不知道你为什么使用 array_reverse(),我也不知道你为什么使用 INSERT IGNORE,所以我把它们省略了。

【讨论】:

  • 嗨,比尔,感谢您回复我。现在我正在从我的代码中修复一大堆错误。我摆脱了所有查询,只输出内容,只是为了确保代码运行。我想我或多或少地工作了。所以我会在一切就绪后尽快尝试你的建议。如您所知,我对 mysqli 尤其是 PDO 很陌生。 PDO 会将数组分成三部分并按照您编写的方式执行???这么简单?
  • 这个行得通吗:$stmt->bind_param('sss', $reverse[0], $reverse[1], $reverse[2]);
  • 这里没有提到的是VALUES 具有可变长度,如(?,?,?), (?,?,?), (?,?,?) 等,所以上面的工作不会那么容易
  • 感谢@redreggae 指出这一点,我在回答中添加了更多内容。
  • 谢谢@BillKarwin,今天终于搞定了,让它开始工作。不得不涉足未知领域(PDO),但找到了例子并让它发挥作用。效果很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 2010-10-04
  • 2017-04-09
  • 2020-12-11
相关资源
最近更新 更多