double quoted variable inside single quoted sql query variable - php

'...where T4."firstName"!=\'ERD\''
*I am sure you know but: ''\'ERD\'' means "ERD" inside single quotation marks.
This is a part of sql query but I want to use a variable instead of ERD. Having trouble with double and single quotation marks. How should I write the variable?
An option to prevent SQL injection is appreciated. I am using ODBC.

I would suggest concatenating it:
"SELECT * from table1 where firstName = '" . $ERD . "'";

Related

Avoid having to use double quotes for columns in PostgreSQL using PHP

I am using PHP with PostgreSQL (PDO) and I am wondering is there a way to not have to wrap my SQL statement columns in double quotation marks when making a call? For example, I have the following code that retrieves the column name needed
$columnname = $_GET["columname"];
but since PostgreSQL requires a column to be inside a double quotation mark, I then have to modify the code to the following
$columnname = '"' . $_GET["columname"] . '"';

Update query not working but the code seems fine

Here is the Query string I am using, i have tried many different itterations...
if (!mysqli_query($db_connection,'UPDATE `questions` SET
`question`='.$question.', `answer1`='.$answer1.', `answer2`='.$answer2.',
`answer3`='.$answer3.', `answer4`='.$answer4.', `rationale`='.$rationale.',
`resources`='.$resources.' WHERE `question_id`='.$id))
{
echo("Error description: " . mysqli_error($db_connection));
}
this is the error I get:
Error description: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax.
I have tried using single quotes, double quotes without ` and with. Nothing seems to work.
It's not a case of using single or double quotes but doing both correctly, you need to wrap your strings in double quotes and open and close your concatenation with singles eg ’answer’ = " ' .$variable. ' ", ..... Then the quotes become part of your string.
Try this:
$query = mysqli_query($db_connection,"UPDATE `questions` SET question`='{$question}',`answer1`='{$answer1}', `answer2`='{$answer2}', `answer3`='{$answer3}', `answer4`='{$answer4}', `rationale`='{$rationale}', `resources`='{$resources}' WHERE `question_id`=".$id);
if ($query)
{
echo("Error description: " . mysqli_error($db_connection));
}
First of all, as the comments stated prepared statements is the way to go. You are open to SQL injection.
Secondly, as for the mysql error message, It looks like you are missing a single quote at the end of your query. WHERE question_id='.$id. Also Echo out the query string and you'll notice that your query does not have single quotes around your parameters. You are using single quotes to create the query string but not for the query parameters. Use double quotes for the query string, and single quotes for the parameters:
"UPDATE `questions` SET
`question`='".$question."', `answer1`='".$answer1."', `answer2`='".$answer2."',
`answer3`='".$answer3."', `answer4`='".$answer4."', `rationale`='".$rationale."',
`resources`='".$resources."' WHERE `question_id`="'.$id."'"

SQL query statement in PHP doesn't work

I hava a problem using SQL query in PHP, I've tried to find out what wrong about my code, anyway this code work well in SQL query via phpMyAdmin and show the result correctly
When I use the condition WHERE RoomNo ='D003' or any Room No. (I tried both 'D003' and "D003") it doesn't query anything at all
Please help.
Here is my code
onclick = "javascript: openListOfValue('ADD_LINE','Room','Select RoomNo, RoomType FROM Room WHERE RoomNo ="D003"','RoomNo,RoomType');
<script>
function openListOfValue(mode, table, initSQL, columnname){
window.open("listofvalue.php?mode="+mode+"&table="+table+"&initSQL="+initSQL+"&columnname="+columnname,"popup","width=600,height=350");
}
</script>
SQL string literals are quoted with single quotes.
As your HTML string literal uses double quotes, you are using single quotes for your Javascript string literal. In order to have single quotes inside this string you must escape them. In Javascript you use the backslash to escape a single quote:
onclick = "javascript: openListOfValue('ADD_LINE','Room',
'Select RoomNo, RoomType FROM Room WHERE RoomNo = \'D003\'','RoomNo,RoomType');"

PDO: Quotes in SQL

I'm seeing some weirdness when I try to run a query using PDO. The following code shouldn't return results, but it does:
$safe_path = $this->_databaseConnection->quote($unsafe_path);
$sql = "SELECT * FROM routes WHERE path=$safe_path LIMIT 1";
$statement_handle = $this->_databaseConnection->query($sql);
var_dump($statement_handle->fetchAll());
I'm confused because there aren't single quotes around the $safe_path variable as there would be if I were using the mysqli extension - but it's working. If I enclose $safe_path in quotes, no results are returned. This seems strange to me.
You are already quoting the $safe_path variable with your first line in the sample:
$safe_path = $this->_databaseConnection->quote($unsafe_path);
That is why it works as it stands. If you attempt to add quotes yourself in the:
$sql = "SELECT * FROM routes WHERE path='$safe_path' LIMIT 1";
line then you would be doubling up the quotes and therefore breaking the SQL query.
Please see the manual page for quote() for more information:
PDO::quote() places quotes around the input string (if required) and
escapes special characters within the input string, using a quoting
style appropriate to the underlying driver.
The PDO quote method just add quotes in a string context.
http://php.net/manual/en/pdo.quote.php
PDO::quote() places quotes around the input string (if required)[...]
Aren't you adding quotes?
$safe_path = $this->_databaseConnection->quote($unsafe_path);

PHP escaping question

I have just read the following code but do not understand why there is " and also ' used. Thank you!
$sql='SELECT uid,name FROM users WHERE user="'.mysql_real_escape_string($_POST['login_name']).'" AND ..
There shouldn't be.
The "correct" $sql might look like this:
$sql="SELECT uid,name FROM users WHERE user='".mysql_real_escape_string($_POST['login_name'])."';
You use ' in SQL to say it's a string / literal.
I would suggest that you look into prepared statements, i don't trust mysql_real_escape_string nor mysql_very_real_seriously_this_is_the_real_escape_string, that php-syndrome is not to trust .
This is a PHP program to write an SQL query (and store it in a string).
The target SQL looks like this:
SELECT uid,name FROM users WHERE user="something" AND …
So in PHP terms:
$foo = 'SELECT uid,name FROM users WHERE user="something" AND …'
But you want to replace "something" with dynamic data. In this case the posted login_name — but made safe for MySQL.
$foo = 'SELECT uid,name FROM users WHERE user="' .
mysql_real_escape_string($_POST['login_name']) .
'" AND …'
A better approach is to use prepared statements.
The single quotes surround the SQL-statement ("SELECT..."), the double quote surround the data for the field "user" (though I'd use the quotes the other way around).
The query would look something like this (use single quotes):
SELECT uid FROM users WHERE user='snake'
To assign this query to the variable $sql, you'd have to enclose it in quotes, using double quotes this time, so PHP doesn't assume, the string would end before 'snake':
$sql = "SELECT uid FROM users WHERE user='snake'";
And as you won't always be asking for 'snake' statically, you exchange 'snake' with a dynamic name, exiting/entering the $sql-string by using double quotes again:
$sql = "SELECT uid FROM users WHERE user='" . $dynamic . "'";
If you only wanted one type of quotes, you'd have to escape the quotes that enclose the user-string.
the " will be literally included in the final mysql request so the request send to the mysql database will be:
SELECT uid,name FROM users WHERE user="loginname" AND ..
The single quotes are used to define your string in PHP. The double ones delimit your text field (login_name) in your SQL query.
This is done to avoid escaping the quotes of the query, if the same were used.
You can use single or double quotes for wrapping strings in php. However, there are differences.
With single quote strings, you cannot inline variables (eg: $a = 'hi $name'), nor can you escape characters (eg: $a = 'hi!\n$name').
Here is a nice summary: http://www.jonlee.ca/php-tidbit-single-quotes-vs-double-quotes/
Also on a side note.. Not sure if double quotes should be used for encasing strings in SQL. I do believe you should use single quotes in most DBs.
Looks like the single quotes are used for the PHP code what form the query and the double quotes are use for the query itself
More on Single/Double quotes
you can always echo out the $sql value to see how the Single/Double quotes look before executing the SQL against a DB.
something like:
$sql='SELECT uid,name FROM users WHERE
user="'.mysql_real_escape_string($_POST['login_name']).'";
// Print the SQL
echo $sql."<br />";

Categories