SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Debugging query error?

    Hi guys,

    Can anyone possibly help me understand why the following query is producing:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    PHP Code:
    <?

           
    include("../config.php");

    // Connect to database
        
    $cid mysql_connect($host,$username,$dbpass);
        if (!
    $cid) { echo("ERROR: " mysql_error() . "\n");    }
        
    mysql_select_db("$db");



    // define the maximum results to show (set in config file)
    $max_results $max_articles;


    // Get the data from the Database
    $query "SELECT article_id, name, date from Articles LIMIT $max_results ORDER BY article_id DESC";

            
    $result mysql_db_query($db,"$query",$cid);
            if (!
    $result) { echo("ERROR: " mysql_error() . "\n$result\n");    }
        
        while(
    $row=mysql_fetch_array($result)) 
        {
    ?>
        
    <tr class="row-a">
    <td class="first"><? echo("$row[date]"); ?></td>
    <td><a href="article-<? echo("$row[article_id]"); ?>"><? echo("$row[name]"); ?></a></td>
    </tr>


    <?
        
    }  // end of while statement - getting results from DB
    ?>
    Any help would be greatly appreciated

  2. #2
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Posts
    1,295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result = mysql_db_query($db,"$query",$cid);

    I'd imagine it's because you have quotes around the $query here.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  3. #3
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx 4 the reply
    that kinda worked...

    i had to remove this as well ' LIMIT $max_results ORDER BY article_id DESC' to get it to display results.

    any idea what is wrong with the limit / order by ?

  4. #4
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $query = 'SELECT article_id, name, date from Articles LIMIT '.$max_results.' ORDER BY article_id DESC';

    $result = mysql_db_query($db.', '.$query.', '.$cid);

    also maybe for $result you could do

    $result = mysql_query($query);
    <3php && SitePoint ?>

  5. #5
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi towl337

    I got this error using your suggestion:
    ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY article_id DESC' at line 1

    PHP Code:
    <?

    //  Get Database Info
      
    include("../config.php");

    // Connect to database
        
    $cid mysql_connect($host,$username,$dbpass);
        if (!
    $cid) { echo("ERROR: " mysql_error() . "\n");    }
        
    mysql_select_db("$db");



    // define the maximum results to show (set in config file)
    $max_results $max_articles;


    // Get the data from the Database
    $query 'SELECT article_id, name, date from Articles LIMIT '.$max_results.' ORDER BY article_id DESC'

           
    $result mysql_query($query);
            if (!
    $result) { echo("ERROR: " mysql_error() . "\n$result\n");    }
        
        while(
    $row=mysql_fetch_array($result)) 
        {
    ?>
        
    <tr class="row-a">
    <td class="first"><? echo("$row[date]"); ?></td>
    <td><a href="webmaster-articles/article.php&id=<? echo("$row[article_id]"); ?>"><? echo("$row[name]"); ?></a></td>
    </tr>


    <?
        
    }  // end of while statement - getting results from DB
    ?>

  6. #6
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay for the LIMIT clause you need a start value and end value I think that is the problem. You only have just the $max_results in there.. hopefully someone else can elaborate on this for me.

    Also can I see how you wrote the $max_results variable or rather the $max_articles variable (originally)

    You could test it by changing the query to

    $query = 'SELECT article_id, name, date from Articles LIMIT 0, 15 ORDER BY article_id DESC'; for example to see if that is the problem.
    <3php && SitePoint ?>

  7. #7
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $max_articles = "7";

  8. #8
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'SELECT article_id, name, date from Articles LIMIT 0, 7 ORDER BY article_id DESC';

    'SELECT article_id, name, date from Articles LIMIT 0, '.$max_results.' ORDER BY article_id DESC';

    This means we are starting from row 0 and showing the next 7 rows or whatever max results is.

    You could also do ... LIMIT '.$x.', '.$y.' ORDER BY ... and have both the values as variables, and find a good way to define them. I remember creating a pagination function a while ago that pissed me off using LIMIT clause.

    Leme know if it works.
    <3php && SitePoint ?>

  9. #9
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY article_id DESC' at line 1

    PHP Code:
    $query 'SELECT article_id, name, date from Articles LIMIT 0, '.$max_results.' ORDER BY article_id DESC'

  10. #10
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try putting the LIMIT clause last
    Try this one to test again.
    PHP Code:
    'SELECT article_id, name, date from Articles ORDER BY article_id DESC LIMIT 0, 7'
    PHP Code:
    'SELECT article_id, name, date from Articles ORDER BY article_id DESC LIMIT 0, '.$max_results
    Sorry if I am wrong on this one, although I don't think I am; probably some (less)fresh blood in this thread would be good.
    Leme know.
    <3php && SitePoint ?>

  11. #11
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yippeeeee!
    It worked, thank you so much for your help

  12. #12
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One last question if Im not asking too much??

    PHP Code:
    // Get the data from the Database
    $query = 'SELECT article_id, name, date from Articles ORDER BY article_id DESC LIMIT 0, '.$max_articles; 

           $result = mysql_query($query);
            if (!$result) { echo("ERROR: " . mysql_error() . "\n$result\n");    }
        
        while($row=mysql_fetch_array($result)) 
        {
    ?>
        
    <tr class="row-a">
    <td class="first"><? echo("$row[date]"); ?></td>
    <td><a href="article.php?&id=<? echo("$row[article_id]"); ?>" title="<? echo("$row[name]"); ?>"><? echo("$row[name]"); ?></a></td>
    </tr>


    <?
        
    }  // end of while statement
    How would I go about alternating row classes (<tr>) ?
    I would like the data to come out like:

    first result = <tr class="row-a">
    2nd result = <tr class="row-b">
    third result = <tr class="row-a">
    and so on...

  13. #13
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You really only need to put the alternative class on every second row (the others can have no class), but to do it your way do this:
    PHP Code:
    //Query code here
    $i=0;
    while(
    $row mysql_fetch_assoc($result))
    {
      
    $i++;
      
    $class = ($i &#37; 2 == 0) ? 'row-a' : 'row-b';
      
    $title htmlspecialchars($row['name'], ENT_QUOTES);

    echo <<<EOD
     <tr class="$class">
        <td class="first">
    $row[date]</td>
        <td><a href="article.php?&amp;id=
    {$row['article_id']}" title="$title">$title</a></td>
    </tr>
    EOD;

    The main line is this one:
    PHP Code:
     $class = ($i == 0) ? 'row-a' 'row-b'
    It just means if this row number is divisible by 2 (modulus of zero) then $class = 'row-a', otherwise $class = 'row-b';

    The $i++ increments the loop count each time, so the row classes will alternate.

    IMPORTANT NOTE: The forum is converting my percentage sign to the HTML entities. The code is supposed to have a percentage sign as the modulus operator.
    i.e $i **PERCENTAGE SIGN** 2 == 0

  14. #14
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi craniel
    Thanks for the informative reply!

    What does this mean:
    'echo <<<EOD' & EOD; ?

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adammc View Post
    hi craniel
    Thanks for the informative reply!

    What does this mean:
    'echo <<<EOD' & EOD; ?
    http://us.php.net/manual/en/language...syntax.heredoc


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
  •