0

I have been trying to solve this for hours now and just can't figure out what I am doing wrong here. The loop won't cycle (nothing is being echo'd), but when I run the query in phpmyadmin it works fine. Also the database info is fine. Unfortunately mysqli doesn't output any error at all...

Any help is greatly appreciated!

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
set_time_limit(0);

$db = new mysqli('localhost', 'xxx', 'xxx', 'xxx');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$sql = <<<SQL
    SELECT * FROM 
    `zips`
SQL;


if(!$result = $db->query($sql,MYSQLI_STORE_RESULT)){
    echo  $result->num_rows; // nothing outputted here
    while($row = $result->fetch_assoc()){
        $zip    = $row['zip'];        
        $url = "http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=".$zip;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec($ch);

        curl_close($ch);

        $xmlobj = simplexml_load_string($result);
        $city = $xmlobj->Table->CITY;
        $state = $xmlobj->Table->STATE;
        $timezone = $xmlobj->Table->TIME_ZONE;
        $areacode = $xmlobj->Table->AREA_CODE;

        echo "city:" . $city;die();

        $sql2 = <<<SQL
            INSERT INTO zips
            (zip,city,state,timezone,areacode)
            VALUES('{$zip}','{$city}','{$state}', '{$timezone}', '{$areacode}')
SQL;
        if(!$result2 = $db->query($sql2)){
            die('There was an error running the query [' . $db->error . ']');
        }
    }
} else {
        die('There was an error running the query [' . $db->error . ']');
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Johan
  • 9
  • 1
  • 3

1 Answers1

2

Here is your issue Mate

  echo "city:" . $city;die();  ///script stops.

     .....

 if(!$result2 = $db->query($sql2)){ 

DIE - which will prematurely end your script before running the query inside this block.

Also Technically you can get away with this

    if(!$result2 = $db->query($sql2)){

But it's very hard to read ( a double negative ) I would change it to this way

 if(false === ($result2 = $db->query($sql2))){

As you have it you are saying if(!false == true ) which will pass on the false value and output the error ect.

AS Your Common Sense mentioned you may not be in this block, but without knowing what the result of the first query is, ( it would need to be false to be running in the posted block of code ) it's hard to say. It could be that that first query should be false?

 if(!$result = $db->query($sql,MYSQLI_STORE_RESULT)){
       .....

To explain a bit more, the above line would need to return false, then with the ! not it would be true, for the code in the if block below it to even run. However in that case ( if it was not false ) this output would be generated

   ....
} else {
    die('There was an error running the query [' . $db->error . ']');
}

More likely its a combination of errors.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Not my downvote here, but I can make a real wild guess as to where that comes from. – Funk Forty Niner Oct 13 '16 at 14:51
  • The OP clearly stated that they don't see any output. While this code shoud let out at least the word "city:" – Your Common Sense Oct 13 '16 at 14:51
  • @YourCommonSense so why didn't you respond to [my comment](http://stackoverflow.com/questions/40023929/mysqli-not-showing-any-error#comment67325727_40023929) about your closing the question? which should be reopened. IMHO. If you don't then I will and don't go on meta about it when I do, you will get shot down for it or flag the question. – Funk Forty Niner Oct 13 '16 at 14:52
  • That's because the die preceeds the sql query, in other words the script is ending without executing the sql because of the misplaced die. Ergo no sql is being executed and therefor no error or success is possible. – ArtisticPhoenix Oct 13 '16 at 14:52
  • @ArtisticPhoenix Seems like I've fallen onto deaf ears with that guy. But pretty sure he will comment once I reopen the question if he doesn't. – Funk Forty Niner Oct 13 '16 at 14:55
  • @ArtisticPhoenix did you notice *another* SQL query, near the top of the script, by chance? – Your Common Sense Oct 13 '16 at 14:57
  • Nope didn't notice that at all. Just being honest, but do you honestly think that die belongs there? – ArtisticPhoenix Oct 13 '16 at 14:57
  • then you may reconsider your conclusions now. taking into account echo `$result->num_rows;` line as well – Your Common Sense Oct 13 '16 at 14:58
  • 1
    Not likely, I feel I am still correct. None of the other code would even execute if that was the case. Sense that is the only code there one must assume that some out put is generated by that block, given the echo city its a safe bet. Either they get the `city` as output or this error `die('There was an error running the query [' . $db->error . ']');` From the first queries else block. as they said no errors, I am safe in my assumption. – ArtisticPhoenix Oct 13 '16 at 15:00
  • So you *insists* that die will be executed *prior* `$result->num_rows;`? Nuff said. – Your Common Sense Oct 13 '16 at 15:01
  • The OP said no errors are output, the else for the first query outputs an error, so if the first query failed they would have an error output. Therefore its a very safe bet that the issue lies within the code block that was posted. My conclusion is irrefutable. I'm not always right but I'm never wrong.... – ArtisticPhoenix Oct 13 '16 at 15:02
  • @YourCommonSense - don't get me wrong I see what you are saying but there is no way to know if that first query should return no results. It's possible it shouldn't. Anyway I updated my answer to reflect that. – ArtisticPhoenix Oct 13 '16 at 15:13