mysql_fetch_array(): not a valid MySQL result

Discussion in 'PHP' started by crunk_monk, Feb 21, 2009.

  1. #1
    I'm new to php and a friend referred me to this site for help. Im having a hard time figuring out what is causing this mysql_fetch_array error. I know this error shows up when the array is empty but its not and that is what is making me scratch my head over this. Everything was working fine until I uploaded my files to my server then that is when this error started showing up, AFTER the upload(I used coffee cup ftp). Thought it may be due to a corrupted database so I built a new one and dropped the old one. I have a $sql that pulls the latest 5 post in my blog to display them on my homepage. Here is the code

    <?php

    $sql = "SELECT post_id, title, post, post_by, DATE_FORMAT(postdate, '%e %b %Y at %H:%i') AS dateattime FROM posts ORDER BY postdate DESC LIMIT 5";

    $result = mysql_query($sql);

    $myposts = mysql_fetch_array($result) or die mysql_error();
    ?>


    When I remove $result and $myposts I get a T_STRING error but nothing is wrong! It may just be something simple that is causing the array error but, I'm new to php and I don't have enough exp yet to see what is wrong. Can anybody guess as to what is causing this array error?
     
    crunk_monk, Feb 21, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Without seeing your database structure it's hard to guess. I propose that you change this:

    $result = mysql_query($sql);

    to this:

    $result = mysql_query($sql) or die(mysql_error());

    You will be astounded and delighted by how much more informative the output becomes.
     
    SmallPotatoes, Feb 21, 2009 IP
  3. crunk_monk

    crunk_monk Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok tried your suggestion and now I'm getting "No database selected". I'm curious now, what is the difference between:

    or die mysql_error();
    or die (mysql_error());

    Anyways I now added msql_select_db to the connection script and all is good but strange because I never needed this before, weirdddddddddddddd.

    Thanks for the info!
     
    crunk_monk, Feb 21, 2009 IP
  4. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Strictly arbitrary syntax really. Both do the exact same thing essentially :)

    Regards,
    Dennis M.
     
    Dennis M., Feb 22, 2009 IP
  5. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #5
    mysql_connect - connects to your server
    mysql_select_db - selects a database

    Without doing both - you can't use any of your sql queries as you have no tables in your server .. they all are into databases, right ? ;)
     
    ActiveFrost, Feb 22, 2009 IP