jdwme Posted May 15, 2012 Share Posted May 15, 2012 I'll start off by saying I'm just learning PHP but I'm running into an issue where I keep receiving a "Query was empty" error and was wondering if someone could look at my code to see where I'm going wrong. Any hel is greatly appreciated, I just can't seem to find the error. I was getting a boolean error until I added die to line 58 and that's when it starting saying the query was empty. <?php $db_host = 'localhost'; $db_user = '********_****'; $db_pwd = 'ithisismypassword'; $database_icon_score = '********_****_score'; $table_5 = 'round5_score_sheet'; $table_6 = 'round6_score_sheet'; $s = round; $con = mysql_connect($db_host, $db_user, $db_pwd); if (!$con) {die('Could not connect: ' . mysql_error()); } mysql_select_db("********l_****_score", $con); if (round == 5) { $table = round5_score_sheet; } if (round == 6) { $table = round6_score_sheet; } else { echo "Wrong Round"; } $query = mysql_query("SELECT * FROM $table ORDER BY channel"); $result = mysql_query($query) or die(mysql_errno().':'.mysql_error()); // the above will tell you what, if any, is the mysql error / echo "<html'> <head> <title>South Area ICON Scores</title> </head> <body> <div id='content'> <table> <tr> <th>EmployeeName</th> <th>Judge</th> <th>Round</th> <th>Channel</th> <th>Q1Comments</th> <th>Q1Score</th> <th>Q2Comments</th> <th>Q2Score</th> <th>Q3Comments</th> <th>Q3Score</th> <th>Q4Comments</th> <th>Q4Score</th> <th>Q5Comments</th> <th>Q5Score</th> <th>TotalScore</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['EmployeeName'] . "</td>"; echo "<td>" . $row['Judge'] . "</td>"; echo "<td>" . $row['Round'] . "</td>"; echo "<td>" . $row['Channel'] . "</td>"; echo "<td>" . $row['Q1Comments'] . "</td>"; echo "<td>" . $row['Q1Score'] . "</td>"; echo "<td>" . $row['Q2Comments'] . "</td>"; echo "<td>" . $row['Q2Score'] . "</td>"; echo "<td>" . $row['Q3Comments'] . "</td>"; echo "<td>" . $row['Q3Score'] . "</td>"; echo "<td>" . $row['Q4Comments'] . "</td>"; echo "<td>" . $row['Q4Score'] . "</td>"; echo "<td>" . $row['Q5Comments'] . "</td>"; echo "<td>" . $row['Q6Score'] . "</td>"; echo "<td>" . $row['TotalScore'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted May 15, 2012 Share Posted May 15, 2012 You're calling mysql_query() too many times. Just once is enough. Quote Link to comment Share on other sites More sharing options...
Illusion Posted May 15, 2012 Share Posted May 15, 2012 $query = mysql_query("SELECT * FROM $table ORDER BY channel"); isn't supposed to be $query = "SELECT * FROM $table ORDER BY channel"; Quote Link to comment Share on other sites More sharing options...
jdwme Posted May 15, 2012 Author Share Posted May 15, 2012 so I've tried both suggestions here relating to the following code $query = mysql_query("SELECT * FROM $table ORDER BY channel"); $result = mysql_query($query) or die(mysql_errno().':'.mysql_error()); // the above will tell you what, if any, is the mysql error / O I've tried just $query = "SELECT * FROM $table ORDER BY channel" and it showed the error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY channel' at line 1 I also tried $result = ($query) or die(mysql_errno().':'.mysql_error()); // the above will tell you what, if any, is the mysql error / and it gives the same exact error. Quote Link to comment Share on other sites More sharing options...
Illusion Posted May 15, 2012 Share Posted May 15, 2012 Seems like $table value is not set. There is something wrong with your php code ... find it by yourself. You have some variables declared and never been used. Trying printing $query value and check whether it is coming as you expected or not. Quote Link to comment Share on other sites More sharing options...
jdwme Posted May 15, 2012 Author Share Posted May 15, 2012 $table value is being set though, it's set through an if statement if (round == 5) { $table = $table_5; } the script is being run on a page where I have a dropdown box that lets you select round 5 or round 6, once you choose a round and hit go it should run the script to display the data from one of two tables in my database directly under my dropdown Quote Link to comment Share on other sites More sharing options...
Illusion Posted May 15, 2012 Share Posted May 15, 2012 Did you check ...query is coming out as expected by printing $query value? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 15, 2012 Share Posted May 15, 2012 $table value is being set though, it's set through an if statement if (round == 5) { $table = $table_5; } the script is being run on a page where I have a dropdown box that lets you select round 5 or round 6, once you choose a round and hit go it should run the script to display the data from one of two tables in my database directly under my dropdown I'm quite certain that IF statement is not hitting because round has not been declared properly. Know what I mean? This entire condition block is a mess: if (round == 5) { $table = round5_score_sheet; } round5_score_sheet needs to be enclosed in quotes (single or double), and I have no idea what 'round' is, but I'm thinking you meant to declare at some point earlier so your code should look something like: if ($round == 5) { $table = 'round5_score_sheet'; } Because neither of your conditions are hitting, there is no table name being passed to the query. This is a solid lesson in error handling and control in that all conditions must be met before a query is to be executed, and if none are reached, the query does not run. This: $query = mysql_query("SELECT * FROM $table ORDER BY channel"); $result = mysql_query($query) or die(mysql_errno().':'.mysql_error()); // the above will tell you what, if any, is the mysql error / Needs to turn into: $query = "SELECT * FROM $table ORDER BY channel"; $result = mysql_query($query) or die(mysql_errno().':'.mysql_error()); // the above will tell you what, if any, is the mysql error / Quote Link to comment Share on other sites More sharing options...
jdwme Posted May 15, 2012 Author Share Posted May 15, 2012 Thanks MrMarcus, that explanation helped a lot. I've made the changes you suggested but wanted to ask if I declared round correctly. I'm getting round from my page with the dropdown, so should I use $round = $_POST['round']; to declare it properly? I tried that and made the corrections but I think something is wrong with my jquery on the page that is supposed to be calling this php file because it never populates anything. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 15, 2012 Share Posted May 15, 2012 You should NOT have separate tables for each round. You are creating a nightmare of data management. You should have ONE table that holds the data, with a column that indicates the round that data belongs to. You would then simply put the submitted round number (after validating it/casting it as a number) into the WHERE clause in your query statement to match just the rows for that round. Quote Link to comment Share on other sites More sharing options...
jdwme Posted May 15, 2012 Author Share Posted May 15, 2012 You should NOT have separate tables for each round. You are creating a nightmare of data management. You should have ONE table that holds the data, with a column that indicates the round that data belongs to. You would then simply put the submitted round number (after validating it/casting it as a number) into the WHERE clause in your query statement to match just the rows for that round. You're probably right, but for what this is being used for, we needed seperate tables. Once the info populates to a table, we are dumping each table to do something with offline, right now though to get that dump I'm having to log into myPHPadmin and dump each table manually, I was trying to show the data on a webpage in a table and export it from there if possible just so someone wouldn't have to log into my cpanel to dump the data, which is what we are doing now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.