【问题标题】:PHP Curl and ArrayPHP 卷曲和数组
【发布时间】:2016-07-25 06:44:32
【问题描述】:

我的代码有问题。我正在为我的项目使用 curl。所有需要的数据都正确获取,但保存过程开始时出现问题。

$SizeParag = sizeof($dr_match[0]);
for($CountParag=1;$CountParag<$SizeParag;$CountParag++) {
  $description = ($dr_match[0][$CountParag]);
}

我正在使用此代码获取所有段落,但问题是当我保存字符串 $description 时,它只保存最后一段。

在保存之前我应该​​如何保存所有 $description 的值。提前感谢您对我的帮助。

【问题讨论】:

  • $description = array();$SizeParag = sizeof($dr_match[0]); for($CountParag=1;$CountParag&lt;$SizeParag;$CountParag++){ $description[] = ($dr_match[$CountParag][$CountParag]); }
  • 感谢您的回复。但是当我尝试代码时,它只能保存单词 Array.
  • 抱歉 $dr_match[$CountParag] 不是 $dr_match[$CountParag][$CountParag] 。也可以显示echo "&lt;pre/&gt;";print_r($dr_match);
  • 请使用echo "&lt;pre/&gt;";print_r($dr_match); 显示$dr_match 中有什么值
  • 注意:Undefined offset: 8 in C:\xampp\htdocs\politicscurl.php 上线(其中$description[] =($dr_match[$CountParag++); 写成只能保存而已描述的单词数组。

标签: php curl xampp


【解决方案1】:
$description='';
$SizeParag = sizeof($dr_match[0]);
for($CountParag=1;$CountParag<$SizeParag;$CountParag++){
$description .= ($dr_match[0][$CountParag]) . ' ';
}

.= 附加字符串而不是覆盖。如果您希望每个段落都在新行上,请将 ' ' 更改为“\n”;

【讨论】:

  • 仍然无法保存。它只能保存$description的单词数组。
  • //检查是否存在记录 include("/connection.php");$query = mysqli_query($conn, "SELECT * FROM tblpolitics WHERE title='".$title."'") ; if(mysqli_num_rows($query) > 0){ echo "链接已经存在"; }else{ // 保存链接的所有内容 $sql = "INSERT INTO tblpolitics (id, title, description) VALUES ('', '$title', '$description')"; if (mysqli_query($conn, $sql)) { //echo "新记录创建成功"; } else { 回声“错误:”。 $sql 。 “
    ”。 mysqli_error($conn);}}mysqli_close($conn);
  • user3378734...谢谢你的好。有用!。我只是放错了代码。感谢您的知识和时间。:-)
  • 如果你喜欢我的答案,请选择它作为最佳答案:)
猜你喜欢
  • 2018-05-02
  • 2011-05-21
  • 1970-01-01
  • 2017-09-16
  • 2012-06-28
  • 1970-01-01
相关资源
最近更新 更多