0

I have a list of IDs. For test this is about 20 items.

Now I want to make a loop and get extra data from another server. On this server I prepared a script - when you send it an item ID it will send back more details in JSON format.

while($row = mysql_fetch_array($result)){   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://test.mysite.abc/call/itemdetail/id/".$row['id_item']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $ret = curl_exec ($ch);
    curl_close ($ch);

    $result = json_decode($ret, true);

    print_r($result);
    vecho "<br>";
}

When I go on mysite.abc I see result when I give an item id. But when I run script sometimes I have 1 result or 3 results and the message:

Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:\wamp64\www\testfile.php on line 65

How should I do this?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Wraith
  • 504
  • 6
  • 28
  • Why are you using the long-deprecated `mysql_` code library? It was discontinued many years ago and removed entirely in PHP7. No new code should be written using this library. It leaves you vulnerable to SQL injection attacks (due to the lack of parameterised query support) and potentially other unpatched vulnerabilities. Switch to using `mysqli` or `PDO` as soon as possible, and then learn how to write parameterised queries to protect your data from malicious input. See http://bobby-tables.com for a simple explanation of the risks and some sample PHP code to write queries safely. – ADyson Oct 24 '17 at 09:56
  • as to the error itself, it means $result was not a valid result set. Presumably, your query failed for some reason, and presumably you aren't checking for mysql errors (this should be done after you run the query, but before starting the loop) – ADyson Oct 24 '17 at 09:58
  • it is legacy from 5 years agou wrtitten by someone. Now i replaced mysql on mysqli all functions and switched on php7 but now we testing old system if test will be ok we start runing on php7. i will check this erros from mysql – Wraith Oct 24 '17 at 09:59

1 Answers1

1

Check mysql_query returned a valid resource type.

$result = mysql_query('SELECT column1, column2 FROM table WHERE 1=1'); // Change the SQL query with your

if ($result) {
    while($row = mysql_fetch_array($result)){   
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://test.mysite.abc/call/itemdetail/id/".$row['id_item']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_exec ($ch);
        curl_close ($ch);

        $result = json_decode($ret, true);

        print_r($result);
        echo "<br>";
    }
}

Best Practices:, Avoid mysql_* extension. This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, use the MySQLi or PDO_MySQL extension.

Consider using multi-curl for your case. Look this answer for more details.

Cheerse!!

Arjun Kariyadan
  • 489
  • 2
  • 12