[PHP-DB] Re: Another newbie question -- ANSWERED!!

2004-09-07 Thread Pete Holsberg
On Mon, 6 Sep 2004, Pete Holsberg wrote:

 The following code results in the error message Couldn't
 execute query.

 $sql = SELECT * FROM $table ;
 $sql .= WHERE `LastName` LIKE $search_string ;
 $sql .= OR `FirstName` LIKE $search_string ;
 $sql .= OR `Spouse` LIKE $search_string ;
 $sql .= OR `Street` LIKE $search_string ;
 $sql .= OR `Email` LIKE $search_string LIMIT 0, 30 ;
 $sql .= ORDER BY Street, HouseNum, LastName;


Here's the query string that worked:

$sql = SELECT * FROM $table ;
$sql .= WHERE `LastName` LIKE` '%$search_string%' ;
$sql .= OR `FirstName` LIKE '%$search_string%' ;
$sql .= OR `Spouse` LIKE '%$search_string%' ;
$sql .= OR `Street` LIKE '%$search_string%' ;
$sql .= OR `Email` LIKE '%$search_string%' ;
$sql .= ORDER BY Street, HouseNum, LastName;


With a Unix shell programming background, I'm puzzled by
the use of 'tics' (single quotes, apostrophes' around
%$search_string%. I would have though that they would have
removed the special meaning of the $.

Also, should I have back tics around a column name
EVERYWHERE it's used in a SELECT (in this case, following
ORDER)?

Thanks.

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Re: Another newbie question -- ANSWERED!!

2004-09-07 Thread Greg Donald
On Tue, 2004-09-07 at 12:16, Pete Holsberg wrote:
 Also, should I have back tics around a column name
 EVERYWHERE it's used in a SELECT (in this case, following
 ORDER)?

Only if you plan to use keywords as field names, otherwise it's
pointless.


-- 
Greg Donald

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Re: Another newbie question -- ANSWERED!!

2004-09-07 Thread John Holmes
From: Pete Holsberg [EMAIL PROTECTED]
$sql = SELECT * FROM $table ;
$sql .= WHERE `LastName` LIKE` '%$search_string%' ;
$sql .= OR `FirstName` LIKE '%$search_string%' ;
$sql .= OR `Spouse` LIKE '%$search_string%' ;
$sql .= OR `Street` LIKE '%$search_string%' ;
$sql .= OR `Email` LIKE '%$search_string%' ;
$sql .= ORDER BY Street, HouseNum, LastName;
With a Unix shell programming background, I'm puzzled by
the use of 'tics' (single quotes, apostrophes' around
%$search_string%. I would have though that they would have
removed the special meaning of the $.
The single quotes around the search string are necessary to define the 
string. You can use double or single quotes, really, so long as you're 
making a string. Without the quotes, the database will think it's a column 
name.

Also, should I have back tics around a column name
EVERYWHERE it's used in a SELECT (in this case, following
ORDER)?
Backticks are not required anywhere, really, in this case. When they are 
required is when you have a column name that is either a reserved word or 
has special characters in it. If this is MySQL, it'll allow you to have 
column named called order, desc, foo bar, etc, and not complain during 
the table creation. It will complain when it hits those reserved words in 
the queries, though, so that's why you use the backticks.

---John Holmes... 

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP-DB] Re: Another newbie question -- ANSWERED!!

2004-09-07 Thread randy
On Tue, 7 Sep 2004 13:16:20 -0400 (EDT), Pete Holsberg [EMAIL PROTECTED] wrote:

 With a Unix shell programming background, I'm puzzled by
 the use of 'tics' (single quotes, apostrophes' around
 %$search_string%. I would have though that they would have
 removed the special meaning of the $.

Since you started the string with dbl quotes ($sql), any variables you
use in that string will work even if you wrap it in single quotes
(tics). If you use single quotes for the $sql string, you have to
'step out' of the string (or whatever you call it).

$sql = SELECT * FROM $table; // works
$sql = 'SELECT * FROM '.$table; // works
$sql = 'SELECT * FROM $table'; // doesn't work

randy

-- 
randy [EMAIL PROTECTED]

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php