Jump to content

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


althanis

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?
Link to comment
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]
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.