Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

PHP code question

  1. Nov 17, 2007 #1
    <?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.
     
  2. jcsd
  3. Nov 17, 2007 #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.
     
  4. Nov 21, 2007 #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.
     
  5. Nov 21, 2007 #4
    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.
     
  6. Nov 22, 2007 #5
    $commsql = "SELECT name FROM comments WHERE blog_id = " . $row['id'] . "ORDER BY dateposted;";

    You might need to add a space after $row['id'].
     
  7. Nov 24, 2007 #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
     
  8. Nov 24, 2007 #7
    "Order by date posted" is indented if that is what you mean xAXISx. It just pasted on here as if it wasn't.
     
  9. Nov 24, 2007 #8
    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 :|

    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: Nov 24, 2007
  10. Nov 25, 2007 #9
    Thanks for the support! It was the quote spacing.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?