Jump to content

mysqli error


phpcoderx
Go to solution Solved by mac_gyver,

Recommended Posts

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?

Link to comment
Share on other sites

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
}
Link to comment
Share on other sites

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 by phpcoderx
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.