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?
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.
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!
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 ?