-1

I'm relatively new to PHP so bear with me if I sound like I have no idea what i'm doing..because that's not far from the truth. Also i'm aware my code may look ridiculous or could be simplified or there is an easier way whatever else, i'm not interested in that yet. I want it to work, and i'll simplify it from there. Anyways..

I'm building a page that accesses a table called "poems" in a database. One side of the page is the actual place the poems will display once called from the database, the other side (we'll call that "nav") is a list of poems from the database. The table has three specific fields: order, Title, and Poem.

The nav side pulls all the info properly; it reads order and Title and formats them into a link that reads by the Title and directs to poems.php/?p=#, in which # will be whatever 'order' is for the particular row in the table.

The actual content side is not so lucky. It's supposed to use $_GET to pull the # from the URL, which it does, then it gets fishy. I'm trying to use that bit of info to pull the Title and Poem to display the contents of the two. Sounds simple enough, but for a very very long time all I was getting was "mysql_fetch_assoc() expects parameter 1 to be resource, boolean given ". I finally resolved that by putting single quotes around the word order in my SELECT/FROM/WHERE, as you'll see, but now it's returning nothing.. or at least, when I try to print or echo, nothing displays. I have tried so many different things, and nothing makes sense to me. Even what other similar questions have suggested and people are saying works for them WILL NOT work for me, i'm always getting "expected this, got this" errors. So this is where I am at:

`

        $page = $_GET["p"];
        echo $page;

        $qry = mysql_query("SELECT * FROM poems WHERE 'order'='$page'");
        while ($row = mysql_fetch_array($qry)) {
            $title = $row['Title'];
            $poem = $row['Poem'];
        }
        print $title;
        print $poem;
        ?>
    </td>





    <td id="poemssidebar">
        <?PHP
        $url = "SELECT * FROM poems";
        $result = mysql_query($url);

        while ($db_field = mysql_fetch_assoc($result) ) {

        print 
            "<a href=poems.php?p=" . 
            $db_field['order'] . 
            ">" . 
            $db_field['Title'] . 
            "</a><BR><BR>";
        }
        ?>
    </td>`

The 'echo $page' is just there so I'm seeing some sort of results until it works properly. When I click between the two available entries in the table I get a 1 or a 2, but that's it. print $title and print $poem give me nothing. Help!

2 Answers2

1

When you added the single quotes around order you ended up turning it into a string, instead of a column. What you should use instead are backticks:

SELECT * FROM poems WHERE `order`='$page'
jprofitt
  • 10,802
  • 4
  • 34
  • 45
  • 1
    That nailed it. I had tried everything but that it seems. Without anything around it I was getting the boolean error, with single quotes was getting nothing. Thanks so much. – user2503413 Jun 20 '13 at 12:56
0

I think its due to quotes used in sql query. Try this

$qry = mysql_query("SELECT * FROM poems WHERE order ='". $page. "'");
user11
  • 311
  • 1
  • 3
  • 11