Page 1 of 1

What's wrong with this code? --Newbie

Posted: Mon Jun 26, 2006 6:51 pm
by Forgotten
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.

Code: Select all

$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result) ) {
// process the row
} else {
echo('done with while statement');
}
the line with else returns an error – can an else ‘statement’ follow a ‘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>
Thank you for your help. All suggestions are welcomed.[/quote]

Re: What's wrong with this code? --Newbie

Posted: Mon Jun 26, 2006 7:46 pm
by bdlang
Forgotten wrote:I am walking through a book from sitepoint I bought a long time ago.
Man, have you got small feet. Heh, funny.
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.
Your text editor (unless you're using something really basic like Notepad) should be able to properly format tabs and spaces in your documents. I have mine set to tab 4 spaces, and actually insert spaces instead of a tab. When you're entering code into the forum here, use actual spaces in your indentation, tabs do not work within this structure.

Code: Select all

$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result) ) {
// process the row
} else {
echo('done with while statement');
}
the line with else returns an error – can an else ‘statement’ follow a ‘while’ statement?
No, and while you're at it, remove the redundant call to mysql_fetch_array(). This should look like

Code: Select all

while ($row = mysql_fetch_array($result) ) {
    // process the row
}
echo 'Done with while() statement';
The below is the same only longer and commented.

Code: Select all

$result = @mysql_query('SELECT JokeText FROM Jokes');
if (!$result) {
die('<p>Error performing query: mysql_error()</p>');
} else {
echo('It worked!');
}
Please note that using mysql_error() within the single quotes will not actually provide any information. You must escape the string and call mysql_error() like so:

Code: Select all

die('<p>Error performing query: ' .mysql_error(). '</p>');

Code: Select all

/* $row = mysql_fetch_array($result); <-- why does this line cause it not to work? */
This is fine in and of itself, but only retrieves a single record from the resultset, the last one. You must use a looping construct such as while() to retrieve all records.

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.
Remove the endif, it is a different format than what you've used in the rest of your script and not required here. The closing brace at the end of the if statement will do it.

Some light reading:
PHP Manual
PHP Manual: Control Structures
PHP Manual: Alternative syntax for control structures

Re: What's wrong with this code? --Newbie

Posted: Mon Jun 26, 2006 9:10 pm
by Jade
bdlang wrote:
Forgotten wrote:I am walking through a book from sitepoint I bought a long time ago.
Man, have you got small feet. Heh, funny.
Good one.

Posted: Tue Jun 27, 2006 1:19 am
by Forgotten
Whoops, was thinking of another word. :) 'working'

Thanks.

No doubt I'll be back.