douzao5487 2014-07-06 06:41
浏览 14
已采纳

如何从数组获取数据到字符串php

Have problem with php array. I'm trying to send get_headers() request with paths written into $variable array, answer I want to write into MySQL database, spent few hours already, but didn't find out how to do that. It returns only 1 result, but if echo results from array - can see for example 3 results. Please, help me somebody :)

    foreach($array_variable as $variable) {
$url = "http://".$site.$variable;
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 200 OK') {

    $test = $url;
     echo $test;  //here it works fine, I can see all available results
      $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)"); //here problem is that result duplicates many-many times

}
 echo $test; //but here I have problems, can see only 1 result (last one)
 $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)"); //here problem is, only 1 result goes to our database
  • 写回答

1条回答 默认 最新

  • dongmu5106 2014-07-06 08:46
    关注

    First things first, don't use MySQL its deprecated.

    Use MySQLi (i is for improved) http://www.php.net/manual/en/book.mysqli.php

    Your using a foreach on an array of variables so for each variable it will INSERT into the database, how many variables are in the array?

    What is the $smthing?

    You say the array only contains 3 values yet you get over 30 INSERTS it could be that your not closing your foreach this is needed } or that you are getting multiple replys from headers.

    If you echo outside the foreach loop you will only see the last value of the array.

    This might help also https://stackoverflow.com/a/12782327/3754261

    Try this:

    foreach($array_variable as $variable) {
        $url = "http://".$site.$variable;
        $file_headers = @get_headers($url);
        if($file_headers[0] == 'HTTP/1.1 200 OK') {
    
        $test = $url;
        echo $test;  //here it works fine, I can see all available results
        $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)");     //here problem is that result duplicates many-many times
        }
    
    }
    

    It would also be worth trying this without the conditional statement

    What are your results?

    foreach($array_variable as $variable) {
        $url = "http://".$site.$variable;
        $file_headers = @get_headers($url);
    
        $test = $url;
        echo $test;  //here it works fine, I can see all available results
        $sql = mysql_query("INSERT INTO table (col_1, col_2) VALUES ($smthing, $test)");     //here problem is that result duplicates many-many times
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度