What's wrong with this code? --Newbie
Posted: Mon Jun 26, 2006 6:51 pm
I am walking through a book from sitepoint I bought a long time ago. I am just learning programming and PHP.
Anyway, I have some code that I can' seem to get to work and I dont know why it doesn't work.
Please let me know.
Also, offtopic I am wondering if there is a program that auto-formats your code. Tabbing a lot is new for me and I cant seem to line things up properly. Sorry about the formatting.
the line with else returns an error – can an else ‘statement’ follow a ‘while’ statement?
The below is the same only longer and commented.
And here's a really long version, which gives me a "Parse error: parse error, unexpected T_ENDIF in C:\garn\sample query.php on line 54" I don't know how to use an end if statement, and just copied it, so I am not sure what is wrong.
Thank you for your help. All suggestions are welcomed.[/quote]
Anyway, I have some code that I can' seem to get to work and I dont know why it doesn't work.
Please let me know.
Also, offtopic I am wondering if there is a program that auto-formats your code. Tabbing a lot is new for me and I cant seem to line things up properly. Sorry about the formatting.
Code: Select all
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result) ) {
// process the row
} else {
echo('done with while statement');
}The below is the same only longer and commented.
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<?php
$dbcnx = @mysql_connect('localhost', 'root', '');
mysql_select_db('jokes', $dbcnx);
if (! @mysql_select_db('jokes') ) {
die( '<p>Unable to locate the joke ' .
'database at this time.</p>' );
}
$result = @mysql_query('SELECT JokeText FROM Jokes');
if (!$result) {
die('<p>Error performing query: mysql_error()</p>');
} else {
echo('It worked!');
}
/* $row = mysql_fetch_array($result); <-- why does this line cause it not to work? */
while ($row = mysql_fetch_array($result) ) {
echo('<p>' . $row['JokeText'] . '</p>');
}And here's a really long version, which gives me a "Parse error: parse error, unexpected T_ENDIF in C:\garn\sample query.php on line 54" I don't know how to use an end if statement, and just copied it, so I am not sure what is wrong.
Code: Select all
<body>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" name="phpform">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap>
</textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" />
</p>
</form>
<?php
//connect to the datbase server
$dbcnx = @mysql_connect('localhost', 'root', '');
mysql_select_db('jokes', $dbcnx);
if (! @mysql_select_db('jokes') ) {
die( '<p>Unable to locate the joke ' .
'database at this time.</p>' );
}
$result = @mysql_query('SELECT JokeText FROM Jokes');
if (!$result) {
die('<p>Error performing query: mysql_error()</p>');
} else {
echo('It worked!');
}
/* $row = mysql_fetch_array($result); <-- why does this line cause it not to work? */
while ($row = mysql_fetch_array($result) ) {
echo('<p>' . $row['JokeText'] . '</p>');
}
if (isset ($_POST['submitjoke'])) {
$joketext = $_post['joketext'];
$sql = "INSERT INTO Jokes SET
JokeText = '$joketext';
JokeDate = CURDATE()";
if (@mysql_query($sql)) {
echo('<p>Your joke has been added</p>');
} else {
echo('Error submitting joke');
}
}
echo('<p><a href="' . $_SERVER['PHP_SELF'] .
'?addjoke=1">Add a joke!</a></p>');
endif;
?>
</body>