tags:

views:

186

answers:

5

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.

+13  A: 

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 ;)

BoltClock
This worked out the problems. Guess I have a while to learn.
JBlue
@JBlue: it's OK though, as long as you're *learning*. This fancy quote issue is also pretty obscure at first to beginners. I once read the same book that your code comes from and I must say it's a pretty decent read.
BoltClock
also, there's a difference between double and single quotes " and ' in php. stuff in single quotes is skipped over while stuff in double quotes is processed. so echo "$stuff" will echo the contents of $stuff, while echo '$stuff' will echo exactly $stuff.
pxl
+2  A: 

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

alopix
+6  A: 

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!

Mike Caron
but, I used wordstar 25+ years ago when I started programming (it had a 7 bit mode that occasionally failed, so we wrote another program that read our source code, turning the 8th bit off)
KevinDTimm
A: 

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.

Marcelo
The newlines will be printed out as is. I see nothing wrong with that.
BoltClock
BoltClock, OMG! I just made a test and you're absolutely right! It is my editor, (EditPlus 2.12), that seems to have the wrong syntax file and marks it as invalid.... Oh, well, something new everyday :-) Cheers, Marcelo.
Marcelo
A: 

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.

aletzo