SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Syntax Error??

  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Syntax Error??

    Hello... I keep getting the error message,
    "supplied argument is not a valid MySQL result resource".

    What does it mean??... do I have a syntax error??... I went through the following block of code a million times and didn't see anything wrong...


    <?php
    include("../Vault/UserPass.txt");
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM contacts WHERE id=3";
    $result=mysql_query($query);
    $i=0;
    while ($i<1)
    {
    $commentCount=mysql_result($result,$i,"CommentCount");
    $i++;
    }

    echo("$commentCount");
    mysql_close();
    ?>

    At the end of it all, all I want to do is echo the integer from the "CommentCount" field of my database. Can anyone help??

  2. #2
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only want the CommentCount field, you should change your query to:

    $query = "select CommentCount from contacts where id=3";

    then get the data using:

    $commentCount = mysql_result( $result, 0 ) ;

    Mike

  3. #3
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this instead.

    PHP Code:
    <?php
    include("../Vault/UserPass.txt");

    $host "localhost";
    $con mysql_connect($host,$username,$password);
    mysql_select_db($database)or die( "Unable to select database");

    $query="SELECT commentCount FROM contacts WHERE id=3";

    $result=mysql_query($query);

    while (
    $f mysql_fetch_row($result) ) {

    echo 
    $f['commentCount'];

    }

    mysql_close($con);
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    There's not alot wrong with your code, that error is produced not by PHP but by MySql because the query failed at runtime.

    first of all, get rid of the @ symbol as that surpresses the error messages.
    Then add or die(mysql_error()) to the end of your query line to find out why the query failed
    PHP Code:
    mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM contacts WHERE id=3";
    $result=mysql_query($query) or die(mysql_error()); 
    And as MikeBigg and F4nat1c both suggest, don't use select * if youonly want one or two field results.

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Hey... thanks a lot dude(s)!!...
    So that's what the 'asteryx' and 'at' signs are for, eh.

  6. #6
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shaun(OfTheDead)
    Hey... thanks a lot dude(s)!!...
    So that's what the 'asteryx' and 'at' signs are for, eh.
    Yep. The @ compresses mysql errors so that programmers can control the error output. This is good sometimes, but often for debugging purposes it's a pain. The asteryx is to select all columns/fields in the database. Again that's usefull, but not nesseccary in all cases.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?


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
  •