By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Can anyone help, why doesn't this work ??

P: n/a
I have a from with 2 fields:
Company & Name

Depening which is completed, one of the following queries will be run:

if($Company){
$query = "Select C*
From tblsample
Where ID = $Company
Order By Company ASC";
}

if($Name){
$query = "SELECT *
FROM TBLsample
WHERE Contact = "%$Name%"
ORDER BY Contact ASC";
}
$result = mysql_query($query);
$number = mysql_numrows($result);
for ($i=0; $i<$number; $i++) {

$CompanyName = mysql_result($result,$i,"Company");
$ContactName = mysql_result($result,$i,"Contact");

Print "Company: $CompanyName";
Print "<P>Name: $ContactName";
}

This work fine for 'Company', but not for 'Name'

How do I do a wild search using $Name in the Where statement.. ?

Ie $Name = Jim
So %Jim% should result in :
Jim
jimmy
jimm
etc !!

Thanks
Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
James <Ja***@nothere.com> wrote:
$q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";

^begin string ^endstring
^beginstring ^endstring

Learn the differences between " and ' and the use of these within a
string (http://nl.php.net/manual/en/language.types.string.php).

Other problems are realted to sql.

-wildcards (%) only work on strings AFAIK
-strings need to be quoted (eg ').

--

Daniel Tryba

Jul 16 '05 #2

P: n/a
On Sat, 28 Jun 2003 12:22:02 +0000 (UTC), Daniel Tryba
<ne****************@canopus.nl> wrote:
James <Ja***@nothere.com> wrote:
$q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";

^begin string ^endstring
^beginstring ^endstring

Learn the differences between " and ' and the use of these within a
string (http://nl.php.net/manual/en/language.types.string.php).

Other problems are realted to sql.

-wildcards (%) only work on strings AFAIK
-strings need to be quoted (eg ').


And wildcards in SQL only apply if you use 'LIKE' instead of '='.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #3

P: n/a
My form and results are on one page.
If I use :

if ($Company) {
$query = "Select Company, Contact
From tblworking
Where ID = $Company
Order By Company ASC";
}

if ($Name ) {
$query = "SELECT *
FROM TBLWorking
WHERE Contact Like '%$Name%'
ORDER BY Contact";
}
$result = mysql_query($query);
$number = mysql_numrows($result);
for ($i=0; $i<$number; $i++) {

$CompanyName = mysql_result($result,$i,"Company");
$ContactName = mysql_result($result,$i,"Contact");

Print "Company: $CompanyName";
Print "<P>Name: $ContactName";
}

As soon as I enter the page it searchs using $Name, even though I have not
entered any information.

This results in me getting a complete list printed on screen, with out me
having to search.

How do I get this to show just the form, then the form and the results ?

Thanks

On Sat, 28 Jun 2003 15:38:53 +0100, James <Ja***@NotHere.com> wrote:
Thanks, its now working using:

WHERE Contact Like '%$Name%'

Cheers

On Sat, 28 Jun 2003 13:36:19 +0100, Andy Hassall <an**@andyh.co.uk> wrote:
On Sat, 28 Jun 2003 12:22:02 +0000 (UTC), Daniel Tryba
<ne****************@canopus.nl> wrote:
James <Ja***@nothere.com> wrote:
$q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";
^begin string ^endstring
^beginstring ^endstring

Learn the differences between " and ' and the use of these within a
string (http://nl.php.net/manual/en/language.types.string.php).

Other problems are realted to sql.

-wildcards (%) only work on strings AFAIK
-strings need to be quoted (eg ').


And wildcards in SQL only apply if you use 'LIKE' instead of '='.


Jul 16 '05 #4

P: n/a

"James" <Ja***@NotHere.com> wrote in message
news:dh********************************@4ax.com...
I have a from with 2 fields:
Company & Name

Depening which is completed, one of the following queries will be run:

if($Company){
$query = "Select C*
From tblsample
Where ID = $Company
Order By Company ASC";
}

if($Name){
$query = "SELECT *
FROM TBLsample
WHERE Contact = "%$Name%"
ORDER BY Contact ASC";
}
$result = mysql_query($query);
$number = mysql_numrows($result);
for ($i=0; $i<$number; $i++) {

$CompanyName = mysql_result($result,$i,"Company");
$ContactName = mysql_result($result,$i,"Contact");

Print "Company: $CompanyName";
Print "<P>Name: $ContactName";
}

This work fine for 'Company', but not for 'Name'

How do I do a wild search using $Name in the Where statement.. ?

Ie $Name = Jim
So %Jim% should result in :if($Name){
Jim
jimmy
jimm
etc !!

Thanks


if ( $Company )
{
$query = "Select C* From tblsample Where ID LIKE '%$Company%' Order By
Company ASC";
}
if ( $Name )
{
$query = "SELECT * FROM TBLsample WHERE Contact LIKE '%$Name%' ORDER BY
Contact ASC";
}
Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.