0

When I run the following php code the while loop only runs once even if there is more than one row in practice_calendar_times.

$sql = "
    SELECT * FROM practice_calendar_times;
    ";
$result = mysql_query($sql, $con);
if($result){
    while($practice = mysql_fetch_array($result)){
        //... Prints data from the row + more ...
    }
}

On the web page I get a printed warning message: "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /rmounts/vol0-nas/yorkweb/webs/fencing/MyWebSite/homecontent.php on line 113".

Line 113 is the while loop. I looked it up and apparently this is generally caused by a syntax error with the mysql query. However this is not the case here; the mysql is correctly pulling the first row from the database and the while loop is running once. If I replace the while with an if it does not complain and prints out one row. I also tried deleting all but one row in practice_calendar_times, but still get the warning message. However when I delete all of the rows it does not print a warning.

Any ideas on how to get the while loop to iterate through the table and stop complaining?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
JSideris
  • 5,101
  • 3
  • 32
  • 50
  • Can you add or die ( mysql_error () ) to the line with mysql_query (before the semi-colon) and see if it is returning an error. If it is, what is it? – judda Sep 09 '11 at 03:14
  • Meagar, yes you are right!! The loop was quite large and I had an inner-query that I didn't even consider, which was re-assigning $result. Please post your suggestion as an answer so that I can accept. – JSideris Sep 09 '11 at 03:18
  • If you are going to give a down arrow, please at least post the reason. – JSideris Sep 10 '11 at 05:59

3 Answers3

2

Are you altering the value of $result in the body of the while loop? Why not change it to an if and var_dump($result) after it to make sure it's still a resource?

user229044
  • 232,980
  • 40
  • 330
  • 338
0

you have a wild ; in the definition of $sql

Do you get the first result correctly?

derp
  • 2,940
  • 17
  • 16
  • 1
    Yea I get one result from the table. I am 100% sure that it's not a fluke. Semicolon should be a valid character in an sql query. – JSideris Sep 09 '11 at 03:15
  • okay, in that case could you add: echo mysql_num_rows($result); before the while to see if the number of rows is actually bigger than 1, if so then probably it's something inside the loop that's affecting the outcome. – derp Sep 09 '11 at 03:17
  • The semi-colon is unrelated. This is one mighty suspicious upvote. – user229044 Sep 09 '11 at 03:31
  • @meagar the semicolon was just a comment, what I wanted was to ask for info in order to give a response, at that time I still was unable to comment on questions – derp Sep 17 '11 at 03:23
-1

try removing the semicolon in your query.. so just

$sql = "SELECT * FROM practice_calendar_times";
user229044
  • 232,980
  • 40
  • 330
  • 338
gerl
  • 1,161
  • 2
  • 17
  • 53