Hi. I'm running into one of the strangest problems I've ever had with PHP and MySQL... I have a query on a page that I'm building that selects data from a database. It is supposed to select one column from one row of data. I have tested the select query with MySQL, and it works perfectly everytime, as it does with mysql_query("the query").

The problem seems to be with mysql_fetch_array($result), which I'm calling afterwards. Around 1/5 times, it returns what I am expecting and the other 4 times it returns nothing. The data in the table is not changing at all, and neither is the query.

I go to the page, and the data doesn't show up. I reload a few times, and all of a sudden there it is. I reload again and it's gone. Reload a few more times, and it's there again. This makes no sense to me whatsoever.

Any ideas?

    since you failed to display any relevant code with this issue I am forced to ask.....

    have you added debugging code to your script so you can see what the hell its doing each time?

      Yes, I have. Here's the code....

      database looks like:

      table:
      
      first   second   third
      ----------------------
      one   | two    | three
      

      code is:

      $sql = "select `second` from `table` where `first` = 'one';
      
      $result = mysql_query($sql);
      $blah = mysql_fetch_assoc($result);
      
      echo $blah["second"];
      

      Most of the time nothing shows up, sometimes something does. I've tried echoing $result and echoing $blah. Everytime $result yields "Resource id #X". When it works, $blah echos as an array. When it doesn't, nothing shows up. Like I said, it doesn't make sense to me.

        Of course, this may seem like I'm just over simplifying it and removing what may be relevant stuff, but this in fact is what I have. I created a test script for this, and this is what it is, minus the database connection, etc.

          $sql = "select `second` from `table` where `first` = 'one';
          
          $result = mysql_query($sql) or die('Error - '. mysql_error());
          $blah = mysql_fetch_assoc($result);
          
          echo $blah["second"];
          echo '<pre>';
          print_r($blah);
          echo '</pre>';
          

          see if that yields anything differently...

            Hm... Same thing. When it works, it prints out the array. When it doesn't, nothing shows up.

            This is the strangest thing I've ever seen. Like I said, it works everytime if I run the query through the console.

              no sql error when it doesnt work?

                Right, nothing at all. I think I'm gonna start messing around with apache and sql to see if something is configured wrong. It doesn't seem likely, as I haven't had problems w/ this config in about 4-5 months..

                  Write a Reply...