TecBrat
TecBrat

Reputation: 3729

Query works if I don't bind, but fails when I do. Where is my error?

I have tried variations of this:

$prep_get_company=$connection->prepare("SELECT * FROM sl_customer WHERE company_name = ':company_name'");
$prep_get_company->bindParam(':company_name',$company_name);

and I get no results but if I do this:

$prep_get_company=$connection->prepare("SELECT * FROM sl_customer WHERE company_name = '$company_name'");

It returns my desired results. The contents of $company_name is a string with spaces.

Upvotes: 0

Views: 57

Answers (1)

Prix
Prix

Reputation: 19528

Remove the single quotes on your query:

SELECT * FROM sl_customer WHERE company_name = ':company_name'

Should have been:

SELECT * FROM sl_customer WHERE company_name = :company_name

The single quote will treat it as a literal value which would make your prepared statement fail to bind as it think there is nothing to bind when using the single quotes.

The prepared statement will by itself arrange the data you're binding to it as needed.

Upvotes: 1

Related Questions