Re: mysql test and error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Feb 24, 2008, at 1135AM, hE wrote:

hi,
I found an e-book in the net about php and mysql and with its sample program I am trying to test whether I have mysql working correctly or not. the following program gives error message. why?

What exactly does the error message say? We can help troubleshoot errors much better with error messages. :)

<html>
<head><title>Test MySQL</title></head>
<body>
<!-- mysql_up.php -->
<?php3

Change <?php3 to <?php - everywhere in your code.

By the fact that it's using <?php3 I'm guessing this is a very old tutorial. I'd strongly suggest using a more recent tutorial. There have been lots of changes since PHP3 and there's no reason to start with such an old version.

Here are some places to start:

http://devzone.zend.com/article/627-PHP-101-PHP-For-the-Absolute-Beginner
http://hudzilla.org/phpwiki/index.php?title=Main_Page

I also strongly suggest taking some time to go through the manual. Browse through the different sections and get an idea for what is available. Obviously I'm not suggesting you memorize the manual, but I've found it very helpful to occasionally browse through a section or two in the manual, I always seem to find functions I wasn't aware of before. This really helps in deciding the best way to solve a problem.

if ($result == 0)
echo “<b>Error “ . mysql_errno() . “: “
. mysql_error() . “</b>”;

Is this all on one line in your code? If not it needs to be inside curly braces, though this could be just a case of long lines be broken up by your email software. For consistency and readability, especially when starting with a new language, I tend to always use curly braces - even if not needed.

if ($result == 0)
{
	echo '<b>Error ' . mysql_errno() . ': '. mysql_error() . '</b>';
}

else
{
?>
<!-- Table that displays the results -->
<table border=”1”>
<tr><td><b>Variable_name</b></td><td><b>Value</b>
</td></tr>
<?php3
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo “<TR>”;
$row_array = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++)
{
echo “<TD>” . $row_array[$j] . “</td>”;
}
echo “</tr>”;
}
?>

One last suggestion, indent your code. For example:

else
{
	?>
	<!-- Table that displays the results -->
	<table border=”1”>
	<tr><td><b>Variable_name</b></td><td><b>Value</b></td></tr>
	<?php
	for ($i = 0; $i < mysql_num_rows($result); $i++)
	{
		echo '<TR>';
		$row_array = mysql_fetch_row($result);
		for ($j = 0; $j < mysql_num_fields($result); $j++)
		{
			echo '<TD>'. $row_array[$j] . '</td>';
		}
		echo '</tr>';
	}
	?>
	</table>
	<?php
}
?>

While it's not necessary, I find it much easier to read and troubleshoot code when indented.

Brady
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux