Re: Strings

From: Date: Sun, 18 Mar 2001 14:31:46 +0000
Subject: Re: Strings
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-7577@lists.php.net to get a copy of this message
In article <003801c0afa2$2d5853e0$890083ca@oemcomputer>, mjl@consultingrooms.com ("Mick Lloyd") wrote: > Can anyone advise on how to surround a string variable in a query. I select > a string code from a column using: > > $result = mysql_query("SELECT * FROM $TA WHERE Profileid = '$profileid'"); > $row = mysql_fetch_array($result); > > I then want to extract from a second table an id that relates to the code > from the first query. I've tried various combinations of: > > $result = mysql_query("select Primaryid from primarycodes where Code = > '$row[Primaryexpertise]'") Associate array indexes should be quoted: $row[Primaryexpertise] //not like this $row['Primaryexpertise'] //like this $row["Primaryexpertise"] //or like this Also, even though simple variable references are expanded within double-quotes (ex. "where Profileid = '$profileid'"), the PHP parser seems to be less certain about how to deal with array indexes: is the square bracket intedned as plain text, as a special character (such as in a regular expression) or is it actually an array index? I find that dropping out of quotes whenever referencing array indexes eliminates a lot of parse errors. For example: "select Primaryid from primarycodes where Code = '" . $row['Primaryexpertise'] . "'" Another option is to use extract() so that the array element can be treated as a simple string variable: extract($row); $result = mysql_query("select Primaryid from primarycodes where Code = '$Primaryexpertise'") > String manipulation has me baffled most of the time! Re-read the manual's chapters on strings and arrays. Also check out the Zend site. There are some interesting articles and tutorials over there that address issues not yet adequately covered in the manual. -- CC

« previous php.db (#7577) next »