In article <003801c0afa2$2d5853e0$890083ca@oemcomputer>,
 [EMAIL PROTECTED] ("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:

$result = mysql_query("select Primaryid from primarycodes where Code =

> 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.


PHP Database Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to