SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "SELECT * FROM feedback WHERE .............

    I have 2 tables. Jokes table and Feedback table.

    Jokes table has fields such as ID (primary key) and jokes
    Feedback table has jokeID (copied from jokes table ID ), and feedback, and name

    I have successfully displayed the joke.
    but, How do I also display the all the feedback given for that particular joke ?

    I think its gonna be something like this, but its not working.
    PHP Code:
    $result mysql_query("SELECT * FROM feedback WHERE jokeID= '" $row['id'] . "' "); 

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your query is fine, except for the extra single quotes MySQL will forgive (they don't belong around numeric columns), so the issue is in code you haven't shared.

    My guess is you're reassigning $result within a loop that relies on $result pointing to the first result set.

  3. #3
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have saved the codes below as feedbackdisplay . php
    I still somehow suspect the error is with
    PHP Code:
    WHERE jokeid'" . $row['id'] . "' "); 
    Cause, If I remove that WHERE clause, it perfectly displays every feedback for everything.

    But when I started the WHERE thinggi .... it displays nothing. lol


    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","username","123456");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("JOKESDatabase"$con);


    $result mysql_query("SELECT * FROM feedback WHERE jokeid= '" $row['id'] . "' ");
    echo 
    "<table border='0'>
    <tr>
    <th>Feedback</th>
    <th>Name</th>

    </tr>"
    ;

    while(
    $row mysql_fetch_array($result))  
     {

    echo 
    "<tr>";

      echo 
    "<td>" $row['feedback'] . "</td>";
      echo 
    "<td>" $row['name'] . "</td>";
      echo 
    "</tr>";

     }
     
    echo 
    "</table>";
    ?>

    In the MAIN jokes page...
    PHP Code:

    <?php
    $con 
    mysql_connect("localhost","username","123456");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("JOKESDatabase"$con);


    $result mysql_query("SELECT * FROM jokes WHERE ID = '" $_GET['id'] . "'");

    //run the while loop that grabs all the jokes
    while($row mysql_fetch_array($result))  

      {
      echo 
    "<tr>";
      echo 
    "<td>" $row['joke'] . "</td>";
      
      echo 
    "</tr>";
      }
    echo 
    "</table>";


    mysql_close($con);
    ?>    

    <?php
    include("feedbackdisplay . php"); 
    ?>

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The effect of include() is the same as pasting the contents of the file at that point in the code.

    You've already connected to the database and such, so get rid of all that code. You're also already using the variables $result and $row, so you can't use them within the loop without breaking it.

    Here's an example:

    PHP Code:
    <?php 
    $con 
    mysql_connect("localhost","username","123456"); 
    if (!
    $con) die('Could not connect: ' mysql_error()); 
    mysql_select_db("JOKESDatabase"$con); 

    $result mysql_query("SELECT * FROM jokes WHERE ID = " $_GET['id']); 

    while(
    $row mysql_fetch_array($result)) {

      echo 
    "<b>Joke:</b> " wordwrap($row['joke'],60"\n"true) . "<br />";
      echo 
    "<b>Feedback:</b><br />";

      
    $result2 mysql_query("SELECT * FROM feedback WHERE jokeid = " $row['id']);

      while (
    $row2 mysql_fetch_array($result2)) {

        echo 
    $row2['feedback'] . " written by " $row2['name'] . "<br />";

      }

      echo 
    "<br />";

    }

  5. #5
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW, yes it works now!!!
    WOOHOO
    LOVE YOU Dan Grossman


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •