1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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

Discussion in 'MySQL' started by poseidon, Jan 30, 2006.

  1. #1
    Hi,

    I have already create a db and populate it. I have already run some SQL queries and everything looks fine. Now when I am using PHP to connect to it and than retrieving data and displayign it, it's giving me above error. Here is the code

    config.php
    <?php
    // This is an example of config.php
    $dbhost = 'localhost';
    $dbuser = 'test';
    $dbpass = 'xyzxyz';
    $dbname = 'files';
    ?> 
    Code (markup):
    opendb.php
    <?php
    // This is an example opendb.php
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    mysql_select_db($dbname);
    ?> 
    Code (markup):
    select.php
    <?php
    include 'config.php';
    include 'opendb.php';
    
    $query  = "SELECT * FROM games";
    $result = mysql_query($query);
    
    echo "test";
    [COLOR="Red"]while($row = mysql_fetch_array($result, MYSQL_ASSOC))[/COLOR]
    {
        echo "Name :{$row['author']} <br>";
    } 
    
    include 'closedb.php';
    ?>
    
    Code (markup):
    the red line is where the problem is :(
     
    poseidon, Jan 30, 2006 IP
  2. dsm56

    dsm56 Active Member

    Messages:
    863
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    78
    #2
    try:

    while($row = mysql_fetch_assoc($result))
    {
    echo "Name :{$row['author']} <br>";
    }


    if that doesnt work its a prob with ure sql statement.
     
    dsm56, Jan 30, 2006 IP
  3. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try using this "@mysql_fetch_array()" within your code. I had this issue when going to an upgraded PHP and had to add the "@" in.
     
    ServerUnion, Jan 30, 2006 IP
  4. dsm56

    dsm56 Active Member

    Messages:
    863
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    78
    #4
    That will just silence the error, it will not solve it.
    If you are getting that error it means there is a deeper problem, and the while loop will never run. So if you silence the error it will not achieve anything.

    P
     
    dsm56, Jan 30, 2006 IP
  5. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #5
    are you pulling data from the POST or GET? You may have to specify where the data is being pulled from opposed to just using the variable name.
     
    ServerUnion, Jan 30, 2006 IP
  6. poseidon

    poseidon Banned

    Messages:
    4,356
    Likes Received:
    246
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok guys, I have solved the problem. There was some problem with include files as well as with the database. Thanks to all of u for helping.Green reps to everyone :)
     
    poseidon, Jan 30, 2006 IP
  7. borutho

    borutho Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hello,
    i'm still new with php & mysql. and i've got a little problem. i get the same error message, but i just couldn't find any solution to it. can anyone please tell me what to do? thanks! here's the code:

    <?php
    $con = mysql_connect("localhost","vikingas_post","kickass");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("vikingas_website", $con);
    
    $result = mysql_query("SELECT * FROM vikingas_post");
    
    
    
    [COLOR="Red"]while($row = mysql_fetch_array($result))[/COLOR]
      {
      echo "<hr>";
      echo "<h3>";
      echo $row['Date'];
      echo "</h3>";
      echo "<br />";
      echo "<br />";
      echo $row['Post'];
      echo "<br />";
      echo "<br />";
      echo $row['Signature'];
      
      }
    
    mysql_close($con);
    ?>
    Code (markup):
    the problem is in the red line.
    thanks a lot!:eek:
     
    borutho, Oct 28, 2007 IP
  8. Lordy

    Lordy Peon

    Messages:
    1,643
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you need to have it in formation

    mysql_fetch_array($result, $result_type)
     
    Lordy, Oct 28, 2007 IP
  9. borutho

    borutho Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    umm ok... sorry about this. i've had a little typo in the code which i didn't notice. thanks for your help anyway! rep:D
     
    borutho, Oct 29, 2007 IP
  10. 5starpix

    5starpix Notable Member

    Messages:
    6,380
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    235
    #10
    sry this bump is needed. i have the same problem, its saying 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/tamayi/public_html/index.php on line 17'

    the script:

    the ones in red is causing the problem, but i jus dont know whats wrong. please help me..
     
    5starpix, Dec 13, 2007 IP
  11. moin1407

    moin1407 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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
  12. dlowe

    dlowe Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I'm Also getting: PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    Code: while ($row = call_user_func($func_name,$res_id)){

    Any Suggestions?
     
    dlowe, Dec 21, 2010 IP
  13. dlowe

    dlowe Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Also getting: PHP Warning: Invalid argument supplied for foreach()

    Code:$r = DBquery($q);

    $out = array();

    foreach ($r as $item)

    {

    $id = $item['id'];

    Any Suggestions?
     
    dlowe, Dec 21, 2010 IP