Link to home
Start Free TrialLog in
Avatar of NeoAshura
NeoAshuraFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP QUERY HELP

Hi Experts,

I currently have the following Query.

However if a name for example is Cooke & Son LTD

It does not return the correct value.

Here is my query how can i change it so it displays Cooke & Son from the database.


$sql = "
SELECT * 
FROM customer 
    LEFT JOIN notes 
        ON customer.customer_name = notes.customer_name 
WHERE customer.customer_name = '" . $val . "'";

Open in new window

SOLUTION
Avatar of OnALearningCurve
OnALearningCurve

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NeoAshura

ASKER

Yes that did work, I know that it has something to do with the & sign but didnt know how to solve it.

any ideas?
You would have to think about special chars, try using amp; instead of & in that query.
Hi Roads,

$sql = "
SELECT *
FROM customer
    LEFT JOIN notes
        ON customer.customer_name = notes.customer_name
WHERE customer.customer_name = 'MRS amp; ANNA NORRIS'";

Did not work please note the amp; in between MRS and ANNA
Avatar of OnALearningCurve
OnALearningCurve

OK,

Can you echo out the contents of $val  just before the query is run to see if it is being stored correctly.

In theory
WHERE customer.customer_name = '" . $val . "'";
and
WHERE customer.customer_name = 'Cooke & Sons LTD';

should be exactly the same if it is
$sql = sprintf("SELECT * FROM customer LEFT JOIN notes ON customer.customer_name = notes.customer_name WHERE customer.customer_name = '%s' ",mysql_real_escape_string($val));

Open in new window


Have tried using LIKE search instead of this ?

$sql = "
SELECT *
FROM customer
    LEFT JOIN notes
        ON customer.customer_name = notes.customer_name
WHERE customer.customer_name  LIKE '" %. $val .% "'";
@ OnALearning Curve the echoed value of WHERE customer.customer_name = '" . $val . "'";

was

MRS

when obviously it should be MRS & ANNA NORRIS

@Roads : Thanks for the attemept but the sprintf did also not work.
OK,

So it looks as though we have an issue when the string is being passed to $val

How is $val being defined?

can you post the code that handles that?

Cheers,

Mark.
Looks like Roads_Roads could have nailed it with that link.

It seems to go in exactly the same direction I was starting to head.
Yes i have looked at the link,

the val is passed through a get clause in the url

here is the code below

<input type="button" onclick="var val = document.getElementById('ddlCompanies').options[document.getElementById('ddlCompanies').selectedIndex].value; var url = 'mobilenum.php?val=';url+=val;window.location=url;" value="Submit..." />

i read on the link something about:

escapedStr = window.escape(str)
url=url+"?q="+escapedStr

but was not sure where to put this.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Roads hit the nail on the head, Points will be awarded all round for time and effort.

All is greatly appreciated.
Thanks for the points, very generous

to be fair Roads_Roads got there with the answer and deserved them all

I'll accept them anyway ;)

Cheers,

Mark.
Time as well as answers are appreciated.

Time is money in this world. so I appreciate all relevant attempts to help me
Thank you all gentlemen.