Jump to content

Archived

This topic is now archived and is closed to further replies.

althanis

[resolved]How to use a variable in SQL statement.

Recommended Posts

Hi I'm using php to connect to a Microsoft Access DB and I'd like to use a variable that I'm getting from a search form to the database. This is how I'm currently getting the variable and connecting:

[code]
//retreving the parameter
$emp_name = trim(strtolower($_GET['parameters']));

$conn = new COM("ADODB.Connection") or die("Cannot start ADO");

// Microsoft Access connection string.
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=H:\\path\mof_employees.mdb");

// SQL statement to build recordset.
$rs = $conn->Execute("SELECT
firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephone
FROM employee";
[/code]

I'd like to use $emp_name in the SQL statement. Any help?

Share this post


Link to post
Share on other sites
[b]FINAL EDIT: Resolved[/b] I had to put single quotation marks around the {$emp_name} for it to work. Thanks a lot Daniel0!

When I try this as the SQL statement:

[code]
SELECT firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephone
FROM employee
WHERE firstname LIKE {$emp_name}[/code]

I get the following error:

"Parse error: parse error, unexpected ';'"

EDIT: Sorry I was forgetting a close bracket that I accidentally deleted, that's what caused that error. However, it's still not working, I get this error instead now:

[quote]

Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Provider for ODBC Drivers<br/><b>Description:</b> [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.' in H:\XAMMP\xampplite\htdocs\wordpress\empsearch.php:60 Stack trace: #0 H:\XAMMP\xampplite\htdocs\wordpress\empsearch.php(60): com->Execute('SELECT ????????...') #1 {main} thrown in H:\XAMMP\xampplite\htdocs\wordpress\empsearch.php on line 60
[/quote]

Share this post


Link to post
Share on other sites
Probably missing the ) in the Execute function

// SQL statement to build recordset.
$rs = $conn->Execute("SELECT firstname, middlename, surname, emp_id, section_name, dooh, job_title, dob, picture, email, telephone
FROM employee
WHERE firstname LIKE {$emp_name};"[b])[/b];

Also, im guessing the %..% for LIKE is in the $emp_name variable already?

Share this post


Link to post
Share on other sites
Thanks darkwinter, you were right on both counts, I was missing the close brackets, and then I had to add the % to cater for the whitespace at the ends. Thanks!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.