phpcoderx Posted September 1, 2013 Share Posted September 1, 2013 I am getting this 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 '' at line 1 This is the code I used: $sql = mysqli_query($con, "SELECT * FROM ".decrypt($_SESSION["name"], ENCRYPTION_KEY)."_table "); while ($row = mysqli_fetch_array($con)) { // displays data } echo mysqli_error($con); I don't know what the problem is,but when I do this:echo "SELECT * FROM ".decrypt($_SESSION["name"], ENCRYPTION_KEY)."_table "); I am getting this: SELECT * FROM ab_table The above query is correct,but why does it flag an error? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 1, 2013 Share Posted September 1, 2013 Well, creating an echo to "duplicate" the query is a terrible way of debugging. Create the query as a string variable and echo that - the actual query - when there is a problem. All it takes is one simple missed character and your debug code will show perfect while the actual code is flawed. In this case you may not have any error at all. mysqli_error() show the last error. That could have occurred before this code was ever executed. You should check if the query failed first - then check what the error is. $query = "SELECT * FROM ".decrypt($_SESSION["name"], ENCRYPTION_KEY)."_table"; $sql = mysqli_query($con, $query); if(!$sql) { echo "Query: $query<br>Error: " . mysqli_error($con); } while ($row = mysqli_fetch_array($con)) { // displays data } Quote Link to comment Share on other sites More sharing options...
phpcoderx Posted September 1, 2013 Author Share Posted September 1, 2013 Thanks for tip! ,but its still flagging an error. Query: SELECT * FROM ab_table 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 '' at line 1 Quote Link to comment Share on other sites More sharing options...
phpcoderx Posted September 1, 2013 Author Share Posted September 1, 2013 (edited) It is quite weird because,I typed the query directly as : $query = "SELECT * FROM ab_table" and it works. But when I use $query = "SELECT * FROM ".decrypt($_SESSION["name"], ENCRYPTION_KEY)."_table" I'm getting this error,even though decrypt($_SESSION["name"], ENCRYPTION_KEY) has the value "ab" Edited September 1, 2013 by phpcoderx Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted September 2, 2013 Solution Share Posted September 2, 2013 encryption/decryption pads some data values with trailing null's, if i remember correctly. A) you should not be dynamically composing the table name, that implies you have created a bunch of tables that hold same type/meaning data. B) do you have a good reason to encrypt/decrypt a value being stored in a session? C) depending on your encryption/decryption being used, you may need to trim trailing nulls. D) if the value in the session variable comes from the user, you need to validate that it contains ONLY an expected value/table name before stuffing it into the query to prevent sql injection. Quote Link to comment Share on other sites More sharing options...
phpcoderx Posted September 2, 2013 Author Share Posted September 2, 2013 I use the user's name to get data from their respective tables.So I encrypted it and escaped the string to avoid possible mysql injections. Trimming the decrypted value solved the problem! Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 2, 2013 Share Posted September 2, 2013 Users shouldn't have separate tables, because databases are not spreadsheets. 1 table for users, then use primary/foreign keys to tie the users to the correct data in the other tables. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2013 Share Posted September 3, 2013 (edited) I encrypted it and escaped the string to avoid possible mysql injections. That makes no sense. Encryption is not a method to be used for making things safe to run in a query. Also, the user has no access to session variables. So, if the value is unsafe - that is because you put it there with an unsafe value. Edited September 3, 2013 by Psycho 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.