dimanche 25 mars 2018

Using quotes in MySQL where statement (column name)

Does anyone know of any way where you can use quotes in the column name in a where statement?

For example

 SELECT * FROM users WHERE "username" = 'Joe Bloggs'

Instead of

 SELECT * FROM users WHERE username = 'Joe Bloggs'

Here's why I want to do it.

I'm making a Laravel application, and i'm trying to create my own generic submit sql function through ajax, where a user can specify all the arguments they require.

For example,

JS

 var argument = "id";
 var argumentValue = 7;
 var value = "Smith";
 $.post('/genericUpdate', {'argument':argument, 'argumentValue':argumentValue, 'value':value});

Global Controller (PHP)

public function genericUpdate(Request $request){
  $input = $request->all();
  $argument = $input['argument'];
  $value = $input['value'];
  $argumentValue = $input['argumentValue'];
  $query = DB::update('UPDATE users SET surname = ? where ? = ?', [$value, $argument, $argumentValue]);
}

But unfortunately this runs the following statement

UPDATE users SET surname = "smith" WHERE "id" = "7";

If there is no way to do it in MySQL, does anyone know of a way to convert "id" to id in JS or PHP?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire