JBlue
JBlue

Reputation: 23

What is wrong with this "for dummies" PHP code?

I am running PHP5 on a free web server and I am trying to learn PHP reading a "for dummies" book...It gives me some code to run and for some infuriating reason I get errors on every line that echos HTML.

here is the code. specifics have been Xed out but they are accurate:

<?php
/* Program: mysql_up.php
* Desc: Connects to MySQL Server and
* outputs settings.
*/
echo “<html>
<head><title>Test MySQL</title></head>
<body>”;
$host=”XXXX”;
$user=”XXXX”;
$password=”XXXX”;
$cxn = mysqli_connect($host,$user,$password);
$sql=”SHOW STATUS”;
$result = mysqli_query($cxn,$sql);
if($result == false)
{
echo “<h4>Error: “.mysqli_error($cxn).”</h4>”;
}
else
{
/* Table that displays the results */
echo “<table border=’1’>
<tr><th>Variable_name</th>
<th>Value</th></tr>”;
for($i = 0; $i < mysqli_num_rows($result); $i++)
{
echo “<tr>”;
$row_array = mysqli_fetch_row($result);
for($j = 0;$j < mysqli_num_fields($result);$j++)
{
echo “<td>”.$row_array[$j].”</td>\n”;
}
}
echo “</table>”;
}
?>
</body></html>

Whenever it gets to a line that echos HTML I get this error or similar:

Parse error: syntax error, unexpected '>' in /home/a7613610/public_html/mysql_up.php on line 6

I want to learn PHP but when it reports errors in supposedly good code it makes me not want to.

Upvotes: 2

Views: 470

Answers (5)

aletzo
aletzo

Reputation: 2481

Your question has been answered, but since you're a rookie in PHP (and apparently in programming), I would like to add, regarding Mike Caron's answer, that you should avoid coding in windows notepad.

I once had a MAJOR white-space problem, because I edited a PHP file in windows notepad (It took me about 2 hours to figure it out).

Notepad++, is a must for starters.

Upvotes: 0

BoltClock
BoltClock

Reputation: 723668

The code is riddled with fancy quotes ( and ) which PHP cannot cope with. You could use your text editor to do a find and replace for both characters with the generic double quote character ". Although it definitely looks like you're not using a plain text editor (see Mike Caron's answer), I hope you're using one. Word-processing and rich text editing software is not designed for writing programs and scripts.

OK, so you're using Notepad to write your PHP code. That's great, but be aware that some eBook readers and even some books themselves mess with quote characters in code blocks and turn them fancy. This is one reason why copying and pasting code is frowned on ;)

Also, if you want to learn PHP, or any form of programming in general, you should probably not expect books and tutorials to be perfect ;)

Upvotes: 13

Marcelo
Marcelo

Reputation: 9407

In addition to what others have pointed out, these lines

echo “<table border=’1’>
<tr><th>Variable_name</th>
<th>Value</th></tr>”;

Should be just one line

echo "<table border=’1’><tr><th>Variable_name</th><th>Value</th></tr>";

Marcelo.

Upvotes: -1

Mike Caron
Mike Caron

Reputation: 14561

NEVER USE MICROSOFT WORD FOR DEVELOPMENT OF ANY KIND!

I apologize for shouting, but this is really important. Use notepad, or use any other text editor. But, never, ever use Word or any word processor for coding!

Upvotes: 6

dst
dst

Reputation: 1788

The quotes are wrong. Use the normal quotes like " and not the ones you copied (“)!

Upvotes: 2

Related Questions