Kevin Yank : Php and Mysql query

I’ve got an idea!
mingkwong, can you post here exact copy of the script you’re running?
Run it, watch browser freeze, and copy it as is?

Shrapnel_N5

I have now reset my php.ini file to set
extension=php_mysqli.dll

I have rebooted and restarted.
I have replaced all references to mysql_ to mysqli_

The php script I am running is based on Kevin’s example but now includes all the hints and tips you have given me. Excuse the size - but here it is in full…

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



if (get_magic_quotes_gpc())
{
	function stripslashes_deep($value)
	{
		$value = is_array($value) ?
				array_map('stripslashes_deep', $value) :
				stripslashes($value);

		return $value;
	}

	$_POST = array_map('stripslashes_deep', $_POST);
	$_GET = array_map('stripslashes_deep', $_GET);
	$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
	$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

if (isset($_GET['addjoke']))
{
	include 'form.html.php';
	exit();
}

$link = mysqli_connect('localhost', 'root', 'caste1sardo');
if (!$link)
{
	$error = 'Unable to connect to the database server.';
	include 'error.html.php';
	exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'output.html.php';
	exit();
}

if (!mysqli_select_db($link, 'joke_db'))
{
	$error = 'Unable to locate the joke database.';
	include 'error.html.php';
	exit();
}

if (isset($_POST['joketext']))
{
	$joketext = mysqli_real_escape_string($link, $_POST['joketext']);
	$sql = 'INSERT INTO joke SET
			joketext="' . $joketext . '",
			jokedate=CURDATE()';
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error adding submitted joke: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
}

if (isset($_GET['deletejoke']))
{
	$id = mysqli_real_escape_string($link, $_POST['id']);
	$sql = "DELETE FROM joke WHERE id='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting joke: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}

	header('Location: .');
	exit();
}

$result = mysqli_query($link, 'SELECT id, joketext FROM joke');
if (!$result)
{
	$error = 'Error fetching jokes: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
}


var_dump($result);
echo mysqli_error(); 
ini_set('log_errors',1);

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


include 'jokes.html.php';

?>

The included jokes.html.php file is as follows :-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>List of Jokes</title>
		<meta http-equiv="content-type"
				content="text/html; charset=utf-8"/>
	</head>
	<body>
		<p><a href="?addjoke">Add your own joke</a></p>
		<p>Here are all the jokes in the database:</p>
		<?php foreach ($jokes as $joke): ?>
			<form action="?deletejoke" method="post">
				<blockquote>
					<p>
						<?php echo htmlspecialchars($joke['text'], ENT_QUOTES,
								'UTF-8'); ?>
						<input type="hidden" name="id" value="<?php
								echo $joke['id']; ?>"/>
						<input type="submit" value="Delete"/>
					</p>
				</blockquote>
			</form>
		<?php endforeach; ?>
	</body>
</html>

The lastest attempt still fails and the error log is reporting :-

at Feb 27 21:06:17 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
[Sat Feb 27 21:06:17 2010] [notice] Apache/2.2.14 (Win32) PHP/5.2.12 configured – resuming normal operations
[Sat Feb 27 21:06:17 2010] [notice] Server built: Sep 28 2009 22:41:08
[Sat Feb 27 21:06:17 2010] [notice] Parent: Created child process 3464
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
[Sat Feb 27 21:06:17 2010] [notice] Child 3464: Child process is running
[Sat Feb 27 21:06:17 2010] [notice] Child 3464: Acquired the start mutex.
[Sat Feb 27 21:06:17 2010] [notice] Child 3464: Starting 64 worker threads.
[Sat Feb 27 21:06:17 2010] [notice] Child 3464: Starting thread to listen on port 80.

Mittineague

Thank you for the suggestion.
I did try that earlier. I know that too failed - but I don’t recall the exact problems now. I will repeat and let you know.

Once again - thanks to all for your help…

Well, I tried it and no luck - everything goes fine… As it should

Well - looking at the SQL it’s spot on - so I’m not surprised. It has to be something to do with the setup here.
It seems really strange that I can select a single column from the table - but not multiple columns. It just doesn’t make sense.

the fact that I’ve got the problem on more than one machine does seems to suggest it’s not “installation specific”.

Can you tell me what versions of PHP and MYSQL you are using please ?

As it seems that some types of selects work while other don’t, I would like to see some details about your table structure.

From the mysql prompt


mysql -u root -p

can you connect to the ijdb database


mysql> use ijdb

and show us the result when you ask to describe the joke table?


mysql> describe joke;

Here ya go…my table structure is -

mysql> describe joke;
±---------±--------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±---------±--------±-----±----±--------±---------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| joketext | text | YES | | NULL | |
| jokedate | date | NO | | NULL | |
±---------±--------±-----±----±--------±---------------+
3 rows in set (0.02 sec)

What happens when from the mysql command line, you attempt to perform the troublesome query?


mysql> SELECT id, joketext FROM joke;

it works a treat…

mysql> select id, joketext from joke;
±—±----------------------------------------------------------------+
| id | joketext |
±—±----------------------------------------------------------------+
| 1 | why did the chicken cross the road ? to get to the other side ! |
| 2 | got the database working on the server at last. |
| 3 | here’s another naff joke
|
| 8 | can’t think of another one ! |
±—±----------------------------------------------------------------+
4 rows in set (0.02 sec)

That’s good, so we can rule out the database as being involved in the trouble.

Here’s a crazy idea. We know that using “SELECT id, joketext FROM joke” from PHP is causing trouble. What happens when you use “SELECT * FROM joke” from PHP?

Also, what does this show, when used after different types of selects?


$result = mysqli_query('SELECT id, joketext FROM joke');
var_dump('id, joketext:', mysql_num_rows($result));

$result = mysqli_query('SELECT * FROM joke');
var_dump('*:', mysql_num_rows($result));

when i select * from joke - once again - i get the standard “cannot display page”…

I tried your queries but got error messages which i believe is down to me running mysqli and not mysql.

when i change the queries to mysqli then...
$result = mysqli_query('SELECT id, joketext FROM joke');
var_dump('id, joketext:', mysqli_num_rows($result));

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\index.php on line 84

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\index.php on line 85
string(13) "id, joketext:" NULL 
Error fetching jokes: 

when i add $link to the mysqli_query ...
$result = mysqli_query($link,'SELECT id, joketext FROM joke');
i get the now infamous page..... "Internet Explorer cannot display the webpage"

===============================
$result = mysqli_query('SELECT * FROM joke');
var_dump('*:', mysqli_num_rows($result));

result is :-
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\index.php on line 84

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\\Program Files (x86)\\Apache Software Foundation\\Apache2.2\\htdocs\\deletejoke\\index.php on line 85
string(2) "*:" NULL 
Error fetching jokes: 

if I add the link in - 
$result = mysqli_query($link,'SELECT * FROM joke');
var_dump('*:', mysqli_num_rows($result));

"Internet Explorer cannot display the webpage"

And that cannot be displayed is just from a test page itself, with no while conditions anywhere around?

i’ve got a while further down…

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

but if i comment that while out then i get :-

string(2) “*:” int(4) 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 95

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’m wanting to reduce your problem to only the bare minimum, in order to help narrow down the cause.

Start with a completely fresh test file, and only put in the bare minimum to create the connection and perform the query.

ok - can see where ya going…

the cut down script is now :

=======================================================
<?php
ini_set(‘display_errors’,1);
error_reporting(E_ALL);

$link = mysqli_connect(‘localhost’, ‘root’, ‘my_password’);
if (!mysqli_set_charset($link, ‘utf8’))
{
$output = ‘Unable to set database connection encoding.’;
include ‘output.html.php’;
exit();
}

if (!mysqli_select_db($link, ‘joke_db’))
{
$error = ‘Unable to locate the joke database.’;
include ‘error.html.php’;
exit();
}

$result = mysqli_query($link,‘SELECT id, joketext FROM joke’);

var_dump($result);
echo mysqli_error();
ini_set(‘log_errors’,1);

?>

======================================================
The result is :-

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 23

so that worked ok - right ?

What is the error that is given? You need to provide $link to mysqli_error as well, for that to show you.

I’m sorry - I have to admit I’m new to this and struggling a little…please bare with me…

I’ve amended the line

echo mysqli_error();

to
echo mysqli_error($link);

and now get the following

object(mysqli_result)#2 (0) { }

God, just remember that all procedural mysqli_error commands need to have $link passed to them.

Good, that’s showing that the mysqli_result statement from before generated no errors.

What do you get when you echo mysqli_num_rows($result);

oh - this is looking good - i believe this is telling me i got my four rows…

object(mysqli_result)#2 (0) { } 4

mysql> select count() from joke;
±---------+
| count(
) |
±---------+
| 4 |
±---------+
1 row in set (0.01 sec)

So what happens now when you do


while ($row = mysqli_fetch_array($result)) {
    var_dump($row);
}

it sickens me to tell ya…

“Internet Explorer cannot display the webpage”

code as it stands at the moment…

<?php
ini_set(‘display_errors’,1);
error_reporting(E_ALL);

$link = mysqli_connect(‘localhost’, ‘root’, ‘my_password’);
if (!mysqli_set_charset($link, ‘utf8’))
{
$output = ‘Unable to set database connection encoding.’;
include ‘output.html.php’;
exit();
}

if (!mysqli_select_db($link, ‘joke_db’))
{
$error = ‘Unable to locate the joke database.’;
include ‘error.html.php’;
exit();
}

$result = mysqli_query($link,‘SELECT id, joketext FROM joke’);

var_dump($result);
echo mysqli_error($link);
echo mysqli_num_rows($result);

while ($row = mysqli_fetch_array($result)) {
var_dump($row);
}

ini_set(‘log_errors’,1);

?>