tags:

views:

120

answers:

2

I have a bunch of PHP code that is auto-generated and it puts all table names and field names within quotes. To wit:

INSERT INTO "t_ML_u_Visitor"("c_u_ID") VALUES ...

Since the code is Auto-Generated it would be much preferred if I could somehow get MySql to accept this syntax rather than subjecting the generated code to a post-process, that strips the quotes.

I have tried this:

$conn=$mysqli = new mysqli("localhost", "root", "", "myDB");
$R=$conn->options(MYSQLI_INIT_COMMAND, "SET SQL_MODE = 'ANSI'"); 

But to no avail.

Help?

+2  A: 

The normal quote character is the back-quote - eg

create table `quoted`( `v` varchar(10) );

Can you change the quote character your auto-generated queries use to suit ?

EDIT: I just tried ANSI mode, and it does what you want:

set @session sql_mode = ANSI;
create table "world"( "v" varchar(10));

The problem then becomes that double-quote can no longer be used to quote literal strings.

Martin
How do I do this from PHP?
Joshua
I don't use PHP, but I think you just run the "set session" statement separately as any other SQL statement using the $conn->query() command: its effect will hold for the duration of your connection to the database.
Martin
A: 

MySQL uses backticks (`) to escape. Your query then looks like this:

INSERT INTO `t_ML_u_Visitor` (`c_u_ID`) VALUES (...)

If you are using PHP and Mysqli (as I see) you can use the function real_escape_string() of mysqli like this:

$conn = new mysqli("localhost", "root", "", "myDB");
$query = "INSERT INTO t_ML_u_Visitor (c_u_ID) VALUES (...)";
$escaped = $conn->real_escape_string($query);
$result = $conn->query($escaped);

See also: http://de2.php.net/manual/en/mysqli.real-escape-string.php

Tobias
No No No... I realize that I can simply remove the quotes and fix everything. The quotes are already there. I would like to set the SQL_MODE to ANSI from PHP and have it work WITHOUT removing the quotes from all the auto-generated queries.
Joshua
hmm.. I checked out some things and found the sourcecode of joomla using mysqli. They use simply `$conn->query("SET SQL_MODE = 'ANSI'");` as Martin said.sry for misunderstanding your question.
Tobias
see: http://stackoverflow.com/questions/3377743/what-is-character-called/3377836#3377836 quotes quotes quotes...
Talvi Watia