www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Trouble executing query and displaying results

  1. #1
    Join Date
    Nov 2007
    Posts
    47

    resolved [RESOLVED] Trouble executing query and displaying results

    I'm pretty new to PHP/MySQL. Assuming I've connected to the database, which I can do, I'm just wanting to retrieve rows from the db that meet the "WHERE" criteria in the code here, and then display them in the table code shown as long as results exist.

    What am I doing wrong?

    PHP Code:
    $result mysql_query("SELECT DATE_FORMAT(date,'%b %d') as audio_date, `title`, `intro`, `link` FROM `tbl_cssAudio` WHERE `interview` = `yes` ORDER BY event_date");


     while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {
        echo 
    '<tr>
                <td>
                    <div class="audioTitle">'
    $row["title"] .'</div>
                    <div class="audioDate">'
    $row["audio_date"].'</div>
                    <div class="audioIntro>'
    $row["intro"].'</div>
                    <div class="audioLinks><a href="http://www.chrisstewartsports.com/podcasts/audio/'
    $row["link"].'" target="_blank">Listen<img src="http:/www.chrisstewartsports.com/images/listen.png" /></a></div>
                </td>
              </tr>'
    ;
    }

    mysql_free_result($result); 

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    Are you getting any errors? Is anything being displayed?

  3. #3
    Join Date
    Nov 2007
    Posts
    47
    Sorry. I should have included that as well.


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/chriss7/public_html/cssaudio.php on line 18

    Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/chriss7/public_html/cssaudio.php on line 29

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    You are using back ticks around the value you are checking for (yes) - you should be using single quotes. You may also find that date is a reserved word and needs to be enclosed in back ticks, you can check that in the mysql manual.

    Incidentally using the mysql_error() function will pass back the error from mysql itself which is useful for debugging and when asking for help.

    Just as a general tip, it's usually best to store booleans as some form of int datatype rather than 'yes' or 'no' strings. It makes condition testing much easier since in PHP 1 = true and 0 = false, but 'yes' and 'no' both = true, which means you have to write less straightforward code if you ever come to test the values in such a field. Not applicable in this case, but just a general point.
    Last edited by Mindzai; 07-14-2009 at 02:32 PM.

  5. #5
    Join Date
    Sep 2008
    Posts
    408
    PHP Code:
    $result mysql_query("SELECT DATE_FORMAT(date,'%b %d') as audio_date, `title`, `intro`, `link` FROM `tbl_cssAudio` WHERE `interview` = `yes` ORDER BY event_date"); 
    Should be

    PHP Code:
    $result mysql_query("SELECT DATE_FORMAT(date,'%b %d') as audio_date, `title`, `intro`, `link` FROM `tbl_cssAudio` WHERE `interview` = 'yes' ORDER BY event_date"); 
    `yes` isn't a string, it's looking for a mysql column\variable named yes. Also, those errors you received usually mean the SQL wasn't executed.
    Mullanaphy!
    http://www.mullanaphy.com/

    Unless code is provided or an exact example is requested I think I'm going to start using psuedo code from now on...

    Also, I freelance as well. Inquire within!

  6. #6
    Join Date
    Nov 2007
    Posts
    47
    Thanks guys. In addition to the back ticks, I figured out that I was using a field/value in the WHERE clause that hadn't been retrieved from the database in the SELECT clause.

  7. #7
    Join Date
    Nov 2008
    Posts
    2,477
    It is perfectly valid to use a field not included in the select in the WHERE clause. For example:

    Code:
    SELECT `name` FROM `users` WHERE `id` = 1

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles