[Solved] When I run this PHP code nothing prints to the screen [closed]


There are multiple problems with your latest revision:

Problem #1:
You should use some variant of mysql_fetch_ in order to fetch the rows from the resource returned by mysql_query(). Using mysql_result can be inefficient. Usually, people use mysql_fetch_assoc(). This will return an array, which you can then access using its key, which depends on which mysql_fetch_ function you use. For example, if you are trying to get a column named 'id' for the current row, you want to retrieve that by using 'id' as the key for your array.

Problem #2:
Your loop is wrong.
for ($j = 0; $j < $rows; ++$i) should be for ($j = 0; $j < $rows; ++$j). Note the ++$i was changed to ++$j. Otherwise, you have an infinite loop (until the timeout is reached for PHP).

Problem #3:
mysql_num_rows() can return FALSE.
According to the documentation, the function will return the number of rows or FALSE on failure. Although FALSE can be coerced into integer 0, I do not like to depend on this.

Also, if you purchased this book I hope you did not pay full price for it. These mysql_ methods have been deprecated. Thus, it is more useful to use the mysqli or PDO extensions instead.


Based on your latest edit, you’ve fixed Problem #2. Add a var_dump($rows); line *before you enter your loop to double-check you have more than 0 lines.

Also, have you checked your error log? Locate your error log and see if it’s displaying errors on there instead of your browser. That would explain why you would be seeing a white screen.

1

solved When I run this PHP code nothing prints to the screen [closed]