PHP Syntax Error: Unexpected '>' Character

  • PHP
  • Thread starter sean1234
  • Start date
  • Tags
    Code Php
In summary: Never mind.)In summary, the conversation is about an error message that appears when trying to use a tutorial on PHP. The error is due to a missing end quote on line 14 and a missing space before the "ORDER BY" statement. The conversation also discusses ways to debug the code and suggests using the fwrite function to print error messages to a log file.
  • #1
sean1234
40
0
<?php

require("header.php");

$sql = "select entries. * , categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

echo "<h2><a href='viewentry.php?id=" . $row['id']
. "'>" . $row['subject'] .
"</a></h2><br />;
echo "<i>In<a href='viewcat.php?id=" . $row['cat_id']
."'>" . $row['cat'] .
"</a> - Posted on " . date("D jS F Y g.iA",
strtotime($row['dateposted'])) .
"</i>";
echo "<p>";
echo n12br($row['body']);
echo "</p>";


The error message is:

Parse error: syntax error, unexpected '>' in C:\xampp\htdocs\index.php on line 15

I'm not sure why the '>' is wrong.
 
Technology news on Phys.org
  • #2
This is why it's good to use a syntax highlighting text editor.

Look carefully at line 14/15:

"</a></h2><br />;
echo "
<i>In<a href='viewcat.php?id=" . $row['cat_id']

You're missing an end quote on line 14, so after that it thinks your HTML is code and your code is HTML.
 
  • #3
<?php

require("header.php");

$sql = "SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER by dateposted DESC
LIMIT 1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "<h2><a href='viewentry.php?id" . $row['id']
. "'>" . $row['subject'] .
"</a></h2><br />";
echo "<i>In <a href='viewcat.php?id=" . $row['cat_id']
."'>" . $row['cat'] .
"</a> - Posted on ".date("D j F Y g.iA",
strtotime($row['dateposted'])) .
"</i>";
echo "<p>";
echo nl2br($row['body']);
echo "</p>";

echo "<p>";

$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] .
"ORDER BY dateposted;";
$commresult = mysql_query($commsql);
$numrows_comm = mysql_num_rows($commresult);
if($numrows_comm == 0) {
echo "<p>No Comments.</p>";
}
else {
echo "(<strong>" . $numrows_comm . " </strong>) comments : ";
$i = 1;
while($commrow = mysql_fetch_assoc($commresult)) {
echo "<a href='viewentry.php?id=" . $row['id'] . "#comment" . $i .
"'>" . $commrow['name'] . "</a> ";
$i++;
}
}
echo "</p>";
$prevsql = "SELECT entries.*, categories.cat FROM entrie, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1, 5;";
$prevresult = mysql_query($prevsql);
$numrows_prev = mysql_num_rows($prevresult);

if($numrows_prev == 0) {
echo "<p>No previous entries.</p>";
}
else {
echo "<ul>";

while($prevrow = mysql_fetch_assoc($prevresult)) {
echo "<li><a href='viewentry.php?id="
. $prevrow['id'] . "'>" . $prevrow ['subject']
. "</a></li>";
}
}
echo "</ul>";



require("footer.php");

?>

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\index.php on line 28

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\index.php on line 47

I have been working on this tutorial for awhile and still cannot get this to work right.
 
  • #4
sean1234 said:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\index.php on line 28

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\index.php on line 47

I have been working on this tutorial for awhile and still cannot get this to work right.

I am pretty sure what this means is that for some reason your mysql_query on the earlier lines failed. Either something's incorrect about your SQL, or you're not actually connected to the database (although this doesn't seem likely since your mysql_fetch_assoc seemed to work?). One of the unfortunate things about the mysql support in PHP is that sometimes errors can appear to occur sometime after the point where they actually occurred.

It's a bit brute-force-y, but I'd suggest just pasting in something like
fwrite(STDERR,__LINE__.": ".mysql_error()."\n");
After every line where you call a "mysql_" method, and seeing when the error appears and what it is.
 
  • #5
$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] . "ORDER BY dateposted;";

You might need to add a space after $row['id'].
 
  • #6
<?php

require("header.php");

$sql = "SELECT entries.*, categories.cat FROM entries, categories
WHERE entries.cat_id = categories.id
ORDER by dateposted DESC
LIMIT 1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
fwrite(STDERR,__LINE__.": ".mysql_error()."\n");
echo "<h2><a href='viewentry.php?id" . $row['id']
. "'>" . $row['subject'] .
"</a></h2><br />";
echo "<i>In <a href='viewcat.php?id=" . $row['cat_id']
."'>" . $row['cat'] .
"</a> - Posted on ".date("D j F Y g.iA",
strtotime($row['dateposted'])) .
"</i>";
echo "<p>";
echo nl2br($row['body']);
echo "</p>";

echo "<p>";

$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] .
"ORDER BY dateposted;";
$commresult = mysql_query($commsql);
$numrows_comm = mysql_num_rows($commresult);
if($numrows_comm == 0) {
echo "<p>No Comments.</p>";
}
else {
echo "(<strong>" . $numrows_comm . " </strong>) comments : ";
$i = 1;
while($commrow = mysql_fetch_assoc($commresult)) {
echo "<a href='viewentry.php?id=" . $row['id'] . "#comment" . $i .
"'>" . $commrow['name'] . "</a> ";
$i++;
}
}
echo "</p>";
$prevsql = "SELECT entries.*, categories.cat FROM entrie, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1, 5;";
$prevresult = mysql_query($prevsql);
$numrows_prev = mysql_num_rows($prevresult);

if($numrows_prev == 0) {
echo "<p>No previous entries.</p>";
}
else {
echo "<ul>";

while($prevrow = mysql_fetch_assoc($prevresult)) {
echo "<li><a href='viewentry.php?id="
. $prevrow['id'] . "'>" . $prevrow ['subject']
. "</a></li>";
}
}
echo "</ul>";



require("footer.php");


That gives me the error: Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\index.php on line 11
 
  • #7
"Order by date posted" is indented if that is what you mean xAXISx. It just pasted on here as if it wasn't.
 
  • #8
sean1234 said:
That gives me the error: Warning: fwrite(): supplied argument is not a valid stream resource in C:\xampp\htdocs\index.php on line 11
I'm sorry, looking I think "STDERR" is something that only exists in newer versions of PHP? Well, you can at least do this. Put this line at the top of the file:
$stderr = fopen("php://stderr","w");
and then whenever you want to print an error put:
fwrite($stderr,__LINE__.": ".mysql_error()."\n");
The errors will then show up in your webserver's error log file.

...there may be a better way than this to print error logging in PHP, I don't know :|

sean1234 said:
"Order by date posted" is indented if that is what you mean xAXISx. It just pasted on here as if it wasn't.

I don't think xAXISx meant that it should be indented, I think he meant that when you say
"ORDER BY"
you OUGHT to be saying
" ORDER BY "
with a space between the quotation mark and the word ORDER. As it is your SQL will be constructed into something like:

"SELECT name FROM comments WHERE blog_id = 23ORDER BY dateposted"

Which of course won't work.

(Another good debugging trick sometimes is to print out your SQL requests rather than passing them to the SQL engine, and then feed them into the SQL prompt manually to see what error you get!)
 
Last edited:
  • #9
Thanks for the support! It was the quote spacing.
 

1. What does the error "PHP Syntax Error: Unexpected '>' Character" mean?

The error "PHP Syntax Error: Unexpected '>' Character" means that there is an extra or misplaced ">" symbol in the code, causing the syntax to be incorrect and the code to fail.

2. How do I fix a "PHP Syntax Error: Unexpected '>' Character"?

To fix a "PHP Syntax Error: Unexpected '>' Character", you will need to carefully review your code and check for any misplaced or extra ">" symbols. Once you have identified the issue, you can remove or correct the symbol to eliminate the error.

3. Why am I getting a "PHP Syntax Error: Unexpected '>' Character"?

You are getting a "PHP Syntax Error: Unexpected '>' Character" because there is an error in your code, specifically an extra or misplaced ">" symbol that is causing the syntax to be incorrect. This error can also occur if you are not using the correct syntax for a particular function or statement.

4. Can I ignore a "PHP Syntax Error: Unexpected '>' Character" and still run my code?

No, you cannot ignore a "PHP Syntax Error: Unexpected '>' Character" and run your code. This error indicates a syntax issue that needs to be fixed in order for the code to run properly. Ignoring the error can lead to further issues and incorrect functionality of your code.

5. How can I prevent "PHP Syntax Error: Unexpected '>' Character" in my code?

The best way to prevent a "PHP Syntax Error: Unexpected '>' Character" is to carefully review your code for any syntax errors before running it. You can also use a code editor or IDE that has syntax highlighting and error checking features to catch any issues before running the code.

Similar threads

  • Computing and Technology
Replies
1
Views
3K
Back
Top