Kevin Yank : Php and Mysql query

Hi,
I wonder if you can help me please.
I’ve purchased a copy if Kevin’s “Build your own database driven web site using php and mysql” (an excellent book) but I do seem to be having some trouble with running a “While” clause.

On page 143 of the book, Kevin describes the following while clause :-

while ($row = mysqli_fetch_array($result))
{
$jokes = array(‘id’ => $row[‘id’], ‘text’ => $row[‘joketext’]);

}

Whenever I try to execute this my browser just “hangs”. I can comment out the while loop and everything runs as expected.
If I comment out the $jokes assignment and substitue in a basic assignment ( $count=0; ) the browser still “hangs” which is leading me to think that the

$row = mysqli_fetch_array($result)
statement is causing the issue.

All other scripting examples up until this point have worked flawlessly.

I am currently running :
mysqladmin Ver 8.42 Distrib 5.1.43, for Win32 on ia32
php-5.2.12-Win32
IE8: version 8.0.7600.16385

and I have 5 rows in my joke database (inserted using the insertjoke routine).

I have checked the book’s Errata web page - no joy.

Any advice would be greatly appreciated.

Thanks,

Chris.

I can’t remember the fix here off hand. Do a search for joke database, something with the title, this question is asked like once a month :wink:

The fix is to check that you have enabled the mysqli drivers.

Secondly, it helps to check that the database connection is valid before making use of it.

Thanks for the tips.
If I had problems with the mysql drivers or database connection would the other scripts described earlier in Kevin’s book have worked successfully ?

Apologies if these seem daft questions - hopefully I’ll get up to speed with php and mysql soon !

Define “hangs”.
If it really hangs, like any other application, so you could not press any button? Or it just shows blank page and continue loading something?
First one is not PHP issue and latter one usually means infinite loop in your script.
But this code doesn’t seem to be infinite.
To detect it, add an incrementing counter variable and a condition, like this:
if ($counter++ > 10) die($counter);
inside your loop

To check if something wrong with mysqli_fetch_array function, substitute whole loop with just one call
$row = mysqli_fetch_array($result);

mingkwong, check your Apache error log, what shows up around the time that your trying the script?

Apologies for the slow reply…

rguy84:

Thanks you. I found a thread that suggested upgrading the version of php would be the answer. Unfortunately that didn’t work for me - so have reverted back to my original.

Shrapnel_N5:

You’re right - “hang” is incorrect. The page attempts to load and after maybe 10-15 seconds then displays the standard page “internet explorer cannot display the web page”
I replaced the while loop with your suggested $row = mysqli_fetch_array($result); but the result was the same - “internet explorer cannot display the web page”

It does seem strange as the statement in Kevin’s addjoke works perfectly.
while ($row = mysqli_fetch_array($result))
{
$jokes = $row[‘joketext’];
}

As for your count suggestion within the while loop - again “internet explorer cannot display the web page”

One thing I did find in my search thru the forum was that it was noted that this issue only seemed to happen when there was more than one column being selected from the database - i.e. when using
$result = mysqli_query($link, ‘SELECT id, joketext FROM joke’);
the page won’t load - the while clause appears to be the issue (deletejoke)

when using $result = mysqli_query($link, ‘SELECT joketext FROM joke’);
the page loads (addjoke)

SpacePhoenix:

My log file shows the following :-
[Fri Feb 26 17:11:46 2010] [notice] Parent: child process exited with status 255 – Restarting.
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
[Fri Feb 26 17:11:46 2010] [notice] Apache/2.2.14 (Win32) PHP/5.2.12 configured – resuming normal operations
[Fri Feb 26 17:11:46 2010] [notice] Server built: Sep 28 2009 22:41:08
[Fri Feb 26 17:11:46 2010] [notice] Parent: Created child process 4092
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
[Fri Feb 26 17:11:46 2010] [notice] Child 4092: Child process is running
[Fri Feb 26 17:11:46 2010] [notice] Child 4092: Acquired the start mutex.
[Fri Feb 26 17:11:46 2010] [notice] Child 4092: Starting 64 worker threads.
[Fri Feb 26 17:11:46 2010] [notice] Child 4092: Starting thread to listen on port 80.
[Fri Feb 26 17:11:47 2010] [notice] Parent: child process exited with status 255 – Restarting.
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
[Fri Feb 26 17:11:47 2010] [notice] Apache/2.2.14 (Win32) PHP/5.2.12 configured – resuming normal operations
[Fri Feb 26 17:11:47 2010] [notice] Server built: Sep 28 2009 22:41:08
[Fri Feb 26 17:11:47 2010] [notice] Parent: Created child process 4016
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
[Fri Feb 26 17:11:47 2010] [notice] Child 4016: Child process is running
[Fri Feb 26 17:11:47 2010] [notice] Child 4016: Acquired the start mutex.
[Fri Feb 26 17:11:47 2010] [notice] Child 4016: Starting 64 worker threads.
[Fri Feb 26 17:11:47 2010] [notice] Child 4016: Starting thread to listen on port 80.

I will continue to search the forum as if this is a regular problem - there must be a solution - but as always - any advise would be gratefully received !

Thanks guys…

Run this code please
<?php wrongg() ?>
I am sure you won’t see an error message, and that’s the problem

There is problem somewhere but without PHP error messages it cannot be found.
I’d suggest to put these lines at the top of your script

ini_set('display_errors',1);
error_reporting(E_ALL);

and run it without hanging part(mysqli_fetch_array)

var_dump($result);
and echo mysqli_error();

also valuable

Beside this, i remember some problem with this code from the book.
What’s the structure of jokes table?

ok…

&lt;?php wrongg() ?&gt;
==============
Fatal error: Call to undefined function wrongg() in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\Test\\index.php on line 1



 var_dump($result);
echo mysqli_error();
===============

object(mysqli_result)#2 (0) { } 
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\index.php on line 94

Add your own joke

Here are all the jokes in the database:

Notice: Undefined variable: jokes in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\jokes.html.php on line 12

Warning: Invalid argument supplied for foreach() in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\jokes.html.php on line 12

I am guessing that the undefined variable is now because I have commented out the (mysqli_fetch_array)


The table structure is defined as:-
==========================
# Code to create a simple joke table

CREATE TABLE joke (
	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	joketext TEXT,
	jokedate DATE NOT NULL
) DEFAULT CHARACTER SET utf8;

seems very strange to me.
from the object(mysqli_result)#2 we can see then query was executed properly
please, add this line to the code, uncomment mysqli_fetch_array and take a look into webserver error_log again:

ini_set('log_errors',1);

ok - the log file now shows :

[Fri Feb 26 18:06:56 2010] [notice] Parent: child process exited with status 255 – Restarting.
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
[Fri Feb 26 18:06:56 2010] [notice] Apache/2.2.14 (Win32) PHP/5.2.12 configured – resuming normal operations
[Fri Feb 26 18:06:56 2010] [notice] Server built: Sep 28 2009 22:41:08
[Fri Feb 26 18:06:56 2010] [notice] Parent: Created child process 4040
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
httpd.exe: Could not reliably determine the server’s fully qualified domain name, using 192.168.1.2 for ServerName
[Fri Feb 26 18:06:56 2010] [notice] Child 4040: Child process is running
[Fri Feb 26 18:06:56 2010] [notice] Child 4040: Acquired the start mutex.
[Fri Feb 26 18:06:56 2010] [notice] Child 4040: Starting 64 worker threads.
[Fri Feb 26 18:06:56 2010] [notice] Child 4040: Starting thread to listen on port 80

umm. seems strange.
there must be all the errors you see in the browser.
though I whas in hope to see an error from the mysqli_fetch_array

even more strange thing I just noticed: as you said the same query goes fine just with fewer number of fields.
Can you try it on this very script?
SELECT joketext FROM joke
instead of
SELECT id, joketext FROM joke

if i just select the joketext and leave the while statement as it currently is
( $jokes = array(‘id’ => $row[‘id’], ‘text’ => $row[‘joketext’]); )

object(mysqli_result)#2 (0) { }
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 94

Notice: Undefined index: id in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 100

Notice: Undefined index: id in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 100

Notice: Undefined index: id in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 100

Notice: Undefined index: id in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 100

Notice: Undefined index: id in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 100

Notice: Undefined index: id in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 100

Add your own joke

Here are all the jokes in the database:

why did the chicken cross the road ? to get to the other side !

got the database working on the server at last.

here’s another naff joke

can’t think of another one !

if I remove the reference to id in the array assignment then I get :

object(mysqli_result)#2 (0) { }
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 94

Add your own joke

Here are all the jokes in the database:

why did the chicken cross the road ? to get to the other side !

got the database working on the server at last.

here’s another naff joke

can’t think of another one !

ok - so not really jokes as I don’t know any ! but it kinda proves the application works/fails.

I am giving up.
It seems impossible to me.
Nothing of general debugging techniques can help here, if error really depends on the requested field’s list - it shouldn’t! It’s just nonsense.

i’ve spent a week a “novice beginner” level trying to sort it !
i’ll keep looking thru the forum and surf around and see if i find anything.
it worries me too that others seem to have fixed the issue - but by having to upgrade to a newer version of php - which for me hasn’t worked. i would have hoped that php would have been stable at this level across releases.
I need to leave this for now - but will look at it again tomorrow.

thank you for your time and effort.

They would beat me for this but I’d say anyway.
I think it’s a mysqli library problem
if you switch to mysql (everything the same but just without trailing i) - everything will be fine, I am sure.
At least you can try it.

I will try to obtain Kevin Yanks book and try things out on a local wamp server some time tomorrow.

The “mysqli_error() expects exactly 1 parameter” error message suggests you are losing the resource. The $link from mysqli_connect() or mysqli_init() http://php.net/manual/en/mysqli.error.php

Although I don’t know what would cause the loss in some instances and not others. Can you look at the table’s contents? (got phpMyAdmin ??)

It works when you only SELECT ‘joketext’ but not for SELECT ‘id’, ‘joketext’
How about only SELECT ‘id’ ??

Shrapnel_N5

it didn’t work…

I tried a number of combinations…

I retained the original setting in the php.ini file keeping the I extension.
extension=php_mysqli.dll

and then ran the query without the I
$result = mysql_query($link, ‘SELECT id, joketext FROM joke’);

Fatal error: Call to undefined function mysql_query() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 84

I then amended the php.ini file to set the extension to mysql
extension=php_mysql.dll

I retained the I on the mysql query:
and got the following message:
Fatal error: Call to undefined function mysqli_connect() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\deletejoke\index.php on line 30

I finally set the php.ini setting to
extension=php_mysql.dll
and ran the query without the I - changing all references in the script from mysqli_ to mysql_

web page showed the “Internet Explorer cannot display the webpage”

am starting to get disheartened now…

Mittineague

i can go into mysql and select from the table without problems

mysql&gt; select * from joke;
+----+-----------------------------------------------------------------+--------
----+
| id | joketext                                                        | jokedat
e   |
+----+-----------------------------------------------------------------+--------
----+
|  1 | why did the chicken cross the road ? to get to the other side ! | 2009-04
-01 |
|  2 | got the database working on the server at last.                 | 2010-02
-20 |
|  3 | here's another naff joke
                                      | 2010-02-20 |
|  8 | can't think of another one !                                    | 2010-02
-21 |

+----+-----------------------------------------------------------------+--------
----+
4 rows in set (0.00 sec)

mysql&gt;
mysql&gt; select id, joketext from joke where joketext like '%naff%';
+----+----------------------------+
| id | joketext                   |
+----+----------------------------+
|  3 | here's another naff joke
 |
+----+----------------------------+
1 row in set (0.03 sec)

mysql&gt;

I have tried the same setup on two different machines now - 1 laptop and 1 pc - exactly the same problems.

Would it be possible to post the version numbers of Apache / Mysql / PHP that you guys have working correctly and I will try installing those combinations.

Thanks,

Chris.

The first couple of errors make sense, they should have happened
If the ini has the two dll’s mysql and mysqli
and you enable one and disable the other (remember to restart the server)
then try to use the one you disabled
you will get the Fatal Error

Off Topic:

You may need to take a short break and get away from this for a while. Being frazzled doesn’t help, I know from my own experience. Sometimes I get so into trying to solve a problem and get so wound up I become inefficient. I stop, get a cup of tea, come back to it, and 9 times out of 10 there the answer is staring me in the face.

Your asking about AMP versions triggered a memory. So I searched for old threads. No joy finding the one I was looking for, but I think it was something about a particular version of PHP not playing nicely with mysqli

I think what would be easiest is to:
enable both the mysql and mysqli dll’s
restart the server
try using only the mysql functions
try using only the mysqli functions

If the code using only the mysql functions works, and the code using only the mysqli functions doesn’t, then you can hopefully find out what versions you need, or change the code used by the book to use mysql functions.