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

Discussion in 'MySQL' started by SNMedia, Dec 29, 2006.

  1. #1
    Hi

    Im getting the following error

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sharpene/public_html/intouch/chat.php on line 19
    Code (markup):
    My line 19 is:
    echo "<b>".$row["name"]."</b>";
     
    SNMedia, Dec 29, 2006 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    That means your select statement is probably wrong (it's not selecting the necessary data from the database).

    Could you post some more code so we can see exactly what's wrong?
     
    maiahost, Dec 29, 2006 IP
  3. Xonium

    Xonium Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I doubt that's the line php think is 19, but anyway.
    The result sent to mysql_fetch_array() is not valid, that means that the query in
    your code that looks something like this $result = mysql_query("some sql query");
    The problem is either that this mysql query doesnt return any rows or that there is an error in the query, it's probably the first.
     
    Xonium, Dec 29, 2006 IP
  4. SNMedia

    SNMedia Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The full document

    <html>
    <head>
    <style type="text/css">
    <!--
    body,td,th {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #000000;
    }
    -->
    </style>
    </head>
    <body bgcolor="#ffcc66">
    <meta http-equiv="refresh" content="5">
    <?php
    include "sql.php";
    $showit = mysql_query("SELECT * FROM `staffchat` ORDER BY `id` DESC");
    while($row = mysql_fetch_array($showit)){
    echo "<b>".$row["name"]."</b>";
    echo ": ".$row["body"]."<br>";
    }


    mysql_close();
    ?>
    </body>
    </html>
     
    SNMedia, Dec 29, 2006 IP
  5. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #5
    many things could have gone wrong... but my suspicion is that your mysql_query command returned an error.

    1. does your staffchat table contain a name field?
    2. run the exact sql command manually in phpmyadmin directly against the database to determine if the sql command is valid.
     
    daboss, Dec 29, 2006 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    Try replacing your line number 19 and line number 20 with this:

    echo "<b>".$row['name']."</b>";
    echo ": ".$row['body']."<br>";

    Bye :)
     
    JEET, Dec 29, 2006 IP
  7. pentaga128

    pentaga128 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/lmigueza/public_html/dd172/unit 4 (DD172)/topic_15/config.php on line 34


    I keep getting this same warning and I dont know what else to do I have check it as far as the punctuation but maybe I am over looking something since I stare at this so much. I have put the script in here so you know what I am talking about...thanks! :confused:


    <?php


    define('DB_HOST', 'localhost');
    define('DB_USER', 'dd172web_dd172us');
    define('DB_PASSWORD', '1235');
    define('DB_NAME', 'dd172web_test');



    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
    or die(mysql_error() . 'Not able to contact the host');


    mysql_select_db(DB_NAME)
    or die(mysql_error() . 'Not able to connect with database');



    function returnNavigation(){

    $sql = "SELECT * FROM tblcategories";

    $result = mysql_query($sql);

    return resultToArray($result);

    }

    function resultToArray($result){

    $array = array();

    while($rs = mysql_fetch_array($result)){

    $array[] = $rs;

    }

    return $array;
    }

    function returnArtwork($category_id){

    $sql = 'SELECT * FROM tblartwork WHERE category_id = ' . $category_id;

    $result = mysql_query($sql);

    return resultToArray($result);

    }

    ?>

    The one in bold is line 34.
     
    pentaga128, Apr 30, 2007 IP
  8. Arson

    Arson Well-Known Member

    Messages:
    622
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    120
    #8
    instead of ".$row['name']." put {$row['name']}

    i think both will work, but curly brackets are easier :)

    Also, for the query problem. does your sql.php automatically connect your query to the database? Or do you need your query to call to a connection variable. eg. $showit = mysql_query("SELECT * FROM `staffchat` ORDER BY `id` DESC",$CONNECTION);

    if so, perhaps you forgot to have the query connect.
     
    Arson, May 4, 2007 IP
  9. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #9
    damn pentaga128
    lol
    this threads way old

    make your own one next time!
     
    Meth_, May 5, 2007 IP
  10. moin1407

    moin1407 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi

    I know this is a very old thread. I have replied that for the all future visitors with a view to helping them:

    see below:

    $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    Cart is table name here. I know that when table name is too short and without underscore thn mql shows the error. because mysql can not detect short name. when table name is too short and without underscore thn you must have to add ` both side of the table name in query. Or if u dont want to add ` thn select a name with a underscore or select a long name.

    See example and try below one and both will work :

    $result = mysql_query("select count(*) from `cart` where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    or,

    $result = mysql_query("select count(*) from cart_1 where cookieId = '" . GetCartId() . "' and itemId = $itemId");
     
    moin1407, Nov 18, 2010 IP