I have been staring at just a few lines of code for over an hour and I don't see a single thing wrong with it so I wanted to get some new eyes looking at this to possibly shine some light on the issue. What I am wanting to do is simply fetch a single value from a table in my database, a table that only has 1 row and 4 columns. Then to insert that data into a variable and call the variable later in the file. The problem is for some reason when I call for the data it doesn't retrieve anything. // Database Connection $dbcnx = @mysql_connect("$dbhost","$dbuname","$dbpass"); $dbselect = @mysql_select_db("$dbname"); if ((!$dbcnx) || (!$dbselect)) { echo "Can't connect to database"; } // Fetching data from MySQL $order_sql = @mysql_fetch_row(mysql_query("SELECT * FROM ".$prefix."_table") or die('Query failed: ' . mysql_error())); $order = $order_sql['order']; echo $order; Code (markup): All of the variables for connecting to the database and the prefix are stored in a separate file and they are pulled from that file with no issue. I've even tested to see if it is even looking for the table correctly and it is because I deleted the table which caused it to tell me it didn't exist so I recreated it and still the same empty result. Here is my MySQL structure That's it, nothing special in either the code or the table yet it still refuses to pull the data...any idea what it could be? Thanks in advance
I'd say the first thing to do is get the SQL exactly as it is supposed to look. i.e. "SELECT * FROM ".$prefix."_table" And execute it outside of PHP i.e. phpmyadmin or SQLyog. That will determine if you have a PHP problem or a mysql problem. 2nd thing. If it turns out to be PHP break down that complicated line into individual statements and put some echo statements between there to see what is going on: $order_sql = @mysql_fetch_row(mysql_query("SELECT * FROM ".$prefix."_table") or die('Query failed: ' . mysql_error())); PHP:
ok I've got it solved...thanks for your help though, I did take your advice in breaking it down so you were helpful. Here is what fixed it just in case someone has the same issue in the future: I changed the fetch from mysql_fetch_row to mysql_fetch_assoc and that did the trick. $sql = "SELECT * FROM ".$prefix."_table"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); $sorder_sql = mysql_fetch_row($result); $sorder = $song_order_sql['sorder']; echo $sorder; Code (markup): to $sql = "SELECT * FROM ".$prefix."_table"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); $sorder_sql = mysql_fetch_assoc($result); $sorder = $song_order_sql['sorder']; echo $sorder; Code (markup):