Chesso Posted May 16, 2007 Share Posted May 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51625-some-hopefully-simple-phpmysql-questions/ Share on other sites More sharing options...
Orio Posted May 16, 2007 Share Posted May 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51625-some-hopefully-simple-phpmysql-questions/#findComment-254318 Share on other sites More sharing options...
Chesso Posted May 16, 2007 Author Share Posted May 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51625-some-hopefully-simple-phpmysql-questions/#findComment-254324 Share on other sites More sharing options...
Orio Posted May 16, 2007 Share Posted May 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/51625-some-hopefully-simple-phpmysql-questions/#findComment-254328 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.