php - 如何将任意数量的值绑定(bind)到 mysqli 中的准备语句?

标签 php mysqli

<分区>

我真的很希望有人花一点时间来检查我的代码。我正在解析一些新闻内容,我可以将初始解析插入到我的数据库中,其中包含新闻 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 语句,我可以在其中绑定(bind)参数,以便将所有 html 标记插入到我的数据库中。我以前用 XML 解析做过这件事。问题是我不知道如何绑定(bind)数组,看看我的代码是否正确,如果它会这样工作......这是完整的代码:

<?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,我正在传递它来解析内容,并试图将它添加到数组中。我可能有很多错误,但我还不能测试它,因为我不知道如何绑定(bind)它以查看它是否有效。而且我也不确定我是否可以在 $text_content div 中执行 if 语句...意思是显示“播放视频”(如果它们存在)。所以,如果有人能花时间和我一起解决这个问题,我将不胜感激。

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

最佳答案

这正是mysqli真正牛逼的场景。要绑定(bind)多个参数,您必须将它们全部作为可变长度参数列表传递给 mysql->bind_param(),但棘手的部分是您必须通过引用 来绑定(bind)它们。 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);

当我想编写一个通用函数来将参数绑定(bind)到 SQL 时,我发现使用 PDO 更容易。不需要绑定(bind),只需将一组值传递给 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,所以我将它们省略了。

关于php - 如何将任意数量的值绑定(bind)到 mysqli 中的准备语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931394/

相关文章:

php - mysql ,删除后,最后插入的行正好位于已删除行的位置

php - Datatables postgres重复行分页

php - 警告 : in_array() expects parameter 2 to be array - ERROR

php - 使用 mysqli 的 Bind_param 非对象错误

php - MySQLi 安装 PHP 5.5.8 Windows

php - 如何在 Laravel 中添加类别名

php - 对 Paypal SDK 集成到 Laravel 的步骤感到困惑?

php - mysqli_fetch_assoc 会停止下一个 mysqli_prepare 工作吗?

php - in_array 函数的一些问题

php - Mysql查询按月存储员工考勤