SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_num_rows():

    Hello all
    I'm having some problems with this code. Can any of you figure it out?

    I get this error:
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 48

    The line 48 has ** on both sides of the line:

    code:
    <?php
    if(isset($_GET['show']) && !(empty($_GET['show'])) && $_GET['show'] == "all") {


    $query = "SELECT * FROM news ORDER BY news_id DESC";
    $result = mysql_query($query) or die("Couldn't execute query: " . mysql_error());

    if(mysql_num_rows($result) == 0) {
    echo 'No news at this time.';
    } else {
    echo '<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">';
    while($row = mysql_free_result($result)) {
    echo '<tr>';
    echo '<td><b>';
    echo $row['topic'];
    echo '</b></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo 'Posted by <a href=\"mailto:';
    echo $row['email'];
    echo '\">';
    echo $row['username'];
    echo '</a>';
    echo ' on ';
    echo $row['post_date'];
    echo ' @ ';
    echo $row['ip_address'];
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo $row['content'];
    echo '</tr>';
    echo '<tr>';
    echo '<td>&nbsp;</td>';
    echo '</tr>';
    }
    echo '</table>';
    mysql_free_result($result);
    }

    } else {

    $query = "SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5";
    $result = mysql_query($query);

    ***if(mysql_num_rows($result) == 0) {***
    echo 'No news at this time.';
    } else {
    echo '<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">';
    while($row = mysql_free_result($result)) {
    echo '<tr>';
    echo '<td><b>';
    echo $row['topic'];
    echo '</b></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo 'Posted by <a href=\"mailto:';
    echo $row['email'];
    echo '\">';
    echo $row['username'];
    echo '</a>';
    echo ' on ';
    echo $row['post_date'];
    echo ' @ ';
    echo $row['ip_address'];
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo $row['content'];
    echo '</tr>';
    echo '<tr>';
    echo '<td>&nbsp;</td>';
    echo '</tr>';
    }
    echo '<tr>';
    echo '<td align=\"right\"><a href=\"?show=all\">Read All News</a></td>';
    echo '</tr>';
    echo '</table>';
    mysql_free_result($result);
    }

    }

    ?>

    I included the db configuration in another file so its not the db. I just dont know what it is pls help

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually man, i messed up that code... change this for one thing...

    PHP Code:
    while($row mysql_free_result($result)) { 
    to this

    PHP Code:
    while($row mysql_fetch_array($result)) { 
    sorry man, that's what happens when i get in a hurry.

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspect that this query:
    SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5

    ...is failing. Therefore $result isn't getting a "valid MySQL result resource".

    Just an introductory guess.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    I suspect that this query:
    SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5

    ...is failing. Therefore $result isn't getting a "valid MySQL result resource".

    Just an introductory guess. [img]images/smilies/smile.gif[/img]
    that shouldn't have anything to do with it though. i've used that query many times and never had a problem.

  5. #5
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the query to mysql_query($sql) or die(mysql_error()) so you can be sure. I've never gotten "not a valid resource" for anything but a failed query.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Second what anode said.

    Also, I'm a little confused about who's asking what.
    seventhsign? det0x? Are the two of you working on the same project?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Second what anode said.

    Also, I'm a little confused about who's asking what.
    seventhsign? det0x? Are the two of you working on the same project?
    i wrote the code for him actually... when i was half asleep. so that clears that up.

    the only problem i noticed was an error that i made that i described above.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seventhsign
    Hello all
    I'm having some problems with this code. Can any of you figure it out?

    I get this error:
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 48

    The line 48 has ** on both sides of the line:

    code:
    <?php
    if(isset($_GET['show']) && !(empty($_GET['show'])) && $_GET['show'] == "all" ) {


    $query = "SELECT * FROM news ORDER BY news_id DESC";
    $result = mysql_query($query) or die("Couldn't execute query: " . mysql_error());

    if(mysql_num_rows($result) == 0) {
    echo 'No news at this time.';
    } else {
    echo '<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">';
    while($row = mysql_free_result($result)) {
    echo '<tr>';
    echo '<td><b>';
    echo $row['topic'];
    echo '</b></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo 'Posted by <a href=""\"mailto:';
    echo $row['email'];
    echo '\">';
    echo $row['username'];
    echo '</a>';
    echo ' on ';
    echo $row['post_date'];
    echo ' @ ';
    echo $row['ip_address'];
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo $row['content'];
    echo '</tr>';
    echo '<tr>';
    echo '<td>&nbsp;</td>';
    echo '</tr>';
    }
    echo '</table>';
    mysql_free_result($result);
    }

    } else {

    $query = "SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5";
    $result = mysql_query($query);

    ***if(mysql_num_rows($result) == 0) {***
    echo 'No news at this time.';
    } else {
    echo '<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">';
    while($row = mysql_free_result($result)) {
    echo '<tr>';
    echo '<td><b>';
    echo $row['topic'];
    echo '</b></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo 'Posted by <a href=""\"mailto:';
    echo $row['email'];
    echo '\">';
    echo $row['username'];
    echo '</a>';
    echo ' on ';
    echo $row['post_date'];
    echo ' @ ';
    echo $row['ip_address'];
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo $row['content'];
    echo '</tr>';
    echo '<tr>';
    echo '<td>&nbsp;</td>';
    echo '</tr>';
    }
    echo '<tr>';
    echo '<td align=\"right\"><a href=""\"?show=all\">Read All News</a></td>';
    echo '</tr>';
    echo '</table>';
    mysql_free_result($result);
    }

    }

    ?>

    I included the db configuration in another file so its not the db. I just dont know what it is pls help
    also.. i don't remember giving you code that had this for example...

    PHP Code:
    echo '<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">'
    since it's echo ' , you don't need to escape the quotes.... but if i did it, oopsie..

  9. #9
    Can we go to a 48 hour day?
    Join Date
    May 2002
    Location
    MI
    Posts
    906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always get this error whenever I don't have a valid connection string.
    mitechie.com
    "Techies just think a little differently
    ...at least that is what they keep telling me."


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
  •