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

LVL 6
NeoAshuraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
OnALearningCurveCommented:
Hi NeoAshura,

I think this should just work,  If you replace
 
WHERE customer.customer_name = '" . $val . "'";

With

WHERE customer.customer_name = 'Cooke & Sons LTD';

as a test does it then return any values?

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

any ideas?
0
 
Lukasz ChmielewskiCommented:
You would have to think about special chars, try using amp; instead of & in that query.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
NeoAshuraAuthor Commented:
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
0
 
Lukasz ChmielewskiCommented:
0
 
OnALearningCurveCommented:
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
0
 
Lukasz ChmielewskiCommented:
$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


0
 
pius_babbunCommented:
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 .% "'";
0
 
NeoAshuraAuthor Commented:
@ 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.
0
 
OnALearningCurveCommented:
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.
0
 
OnALearningCurveCommented:
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.
0
 
NeoAshuraAuthor Commented:
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.
0
 
Lukasz ChmielewskiCommented:
try this
<input type="button" onclick="var val = escape(document.getElementById('ddlCompanies').options[document.getElementById('ddlCompanies').selectedIndex].value); var url = 'mobilenum.php?val=';url+=val;window.location=url;" value="Submit..." />
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
NeoAshuraAuthor Commented:
Roads hit the nail on the head, Points will be awarded all round for time and effort.

All is greatly appreciated.
0
 
OnALearningCurveCommented:
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.
0
 
NeoAshuraAuthor Commented:
Time as well as answers are appreciated.

Time is money in this world. so I appreciate all relevant attempts to help me
0
 
Lukasz ChmielewskiCommented:
Thank you all gentlemen.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.