Connecting Tech Pros Worldwide Forums | Help | Site Map

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

James
Guest
 
Posts: n/a
#1: Jul 16 '05
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

Daniel Tryba
Guest
 
Posts: n/a
#2: Jul 16 '05

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


James <James@nothere.com> wrote:[color=blue]
> $q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";[/color]
^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

Andy Hassall
Guest
 
Posts: n/a
#3: Jul 16 '05

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


On Sat, 28 Jun 2003 12:22:02 +0000 (UTC), Daniel Tryba
<news_comp.lang.php@canopus.nl> wrote:
[color=blue]
>James <James@nothere.com> wrote:[color=green]
>> $q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";[/color]
> ^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 ').[/color]

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

--
Andy Hassall (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
James
Guest
 
Posts: n/a
#4: Jul 16 '05

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


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 <James@NotHere.com> wrote:
[color=blue]
>Thanks, its now working using:
>
>WHERE Contact Like '%$Name%'
>
>Cheers
>
>On Sat, 28 Jun 2003 13:36:19 +0100, Andy Hassall <andy@andyh.co.uk> wrote:
>[color=green]
>>On Sat, 28 Jun 2003 12:22:02 +0000 (UTC), Daniel Tryba
>><news_comp.lang.php@canopus.nl> wrote:
>>[color=darkred]
>>>James <James@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 ').[/color]
>>
>> And wildcards in SQL only apply if you use 'LIKE' instead of '='.[/color][/color]

Roger
Guest
 
Posts: n/a
#5: Jul 16 '05

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



"James" <James@NotHere.com> wrote in message
news:dh1rfvkjstndhgsmdl16g2njhgetj71r8v@4ax.com...[color=blue]
> 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[/color]

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";
}


Closed Thread