PHP  PHP Syntax Error: Unexpected '>' Character

  • Thread starter Thread starter sean1234
  • Start date Start date
  • Tags Tags
    Code Php
AI Thread Summary
The discussion centers around troubleshooting PHP code that interacts with a MySQL database. Key issues include a parse error due to a missing end quote in an echo statement, which leads to confusion between HTML and PHP code. Additionally, warnings about invalid MySQL result resources indicate that earlier SQL queries may have failed, likely due to syntax errors. A specific problem identified is the absence of a space in the SQL query after the variable `$row['id']`, causing malformed SQL. Suggestions for debugging include using error logging to capture MySQL errors and printing SQL queries for manual testing. Overall, the conversation emphasizes the importance of proper syntax and debugging techniques in PHP development.
sean1234
Messages
40
Reaction score
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
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.
 
<?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.
 
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.
 
$commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] . "ORDER BY dateposted;";

You might need to add a space after $row['id'].
 
<?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
 
"Order by date posted" is indented if that is what you mean xAXISx. It just pasted on here as if it wasn't.
 
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:
Thanks for the support! It was the quote spacing.
 

Similar threads

Back
Top