Jump to content

Recommended Posts

I somewhat inconsistently use certain ways with php and mysql, like with queries I sometimes use quotes, sometimes don't, sometimes use ` and sometimes don't.

 

Sometimes I can directly insert a variable (but fails without quotes around it.....), but when accessing an array like $r['myval'] it seems I need to get out of the string, .$r['myval']. back into string.

 

What exactly is the right and minimal way? lol.

 

1. Do I need to use ``? if so, where is it appropriate or should I always use it.

 

2. Like "SELECT * FROM table WHERE id=4", fine no need for '' but, "SELECT * FROM table WHERE id='$id'", fails without ''s, why?

 

3. Like "SELECT * FROM table WHERE id=" .$r['myval']. ""; why go through all that, can't I use '' like with Q2?

 

Things like this lol.

Link to comment
https://forums.phpfreaks.com/topic/51625-some-hopefully-simple-phpmysql-questions/
Share on other sites

Use backsticks (``) around table names or column names that are reserved word. More info.

About variables, you should always use single quotes around them. The problem with arrays is that they have single quotes around the key name too, so when you are using array values use: $sql = "... WHERE column='".$array['key']."' AND ...".

When using regular variables, just put single quotes around them: $sql = "... WHERE column='$id' AND ..."

 

Hope it helps,

Orio.

Thanks, that's mostly what I have been doing (although rarely using `` at all, so key names like table/field names? not value settings).

 

like `myfield`=somevalue or `myfield`='$somevar'.

 

Is it the same case for when intergrating php variables and array values in echo's, like for form action url's etc.

When it comes to variables inside strings, I always separate:

$var = "Some text ".$foo." More text ".$bar['key']." bla bla";

 

But php handles things like:

$var = "Some 'Text' and quotes ''''' a var:  $bar['key'] more quotes '''''''''''''''' bla bla";

 

See more info about php and strings here.

 

Orio.

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.