-
I need proper syntax/procedure for inserting a variable into a SELECT query...
Hello All,
Here's what I want to do:
Take this - $sql = 'SELECT name FROM galleries WHERE spec = XYZ'
where XYZ is a variable passed down from a query string.
What is the proper syntax/procedure for placing a variable in the above statement? I'm trying everything I can think of to no avail.
For what it's worth, I'm using MySQLi.
Thanks in advance for any help you can offer!
Sincerely,
wordman
-
I don't know MySQLi, but I would try something like this:
$sql = 'SELECT name FROM galleries WHERE spec = ' + XYZ
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Phil,
Thank you for the prompt answer. It's still not working though, so here is the exact code I am using:
//get data from query string
$_SERVER['QUERY_STRING'];
$spec = $_GET['ct'];
//includeMySQL connector function
if (! @include('includes/connect.php')) {
echo 'Sorry, database unavailable';
exit;
}
//create a connection to MySQL
$conn = dbConnect('query');
// prepare query
$sql = 'SELECT g_name FROM galleries WHERE g_spec = ' + $spec ;
// submit the query
$result = $conn->query($sql) or die(mysqli_error());
// extract the first record as an array
$row = $result->fetch_assoc();
Dreamweaver won't preview it on my local test server which tells me something is not right with the code.
Any and all help would be immensely appreciated.
Very sincerely,
wordman
-
I need proper syntax/procedure for inserting a variable into a SELECT query...DONE
Quit looking, I found it.
This is the part that I was missing. I found it on a php developer forum and just tested it...works perfect!
$sql = "SELECT g_name, g_desc FROM galleries WHERE g_spec = '$spec%' " ;
What I didn't know was that I had to surround the variable in quotes and add the % at the end. Now, I can pull the data from my DB however I like and it works.
I wanted to post this here in case anyone else had a similar hitch.
Many thanks to everyone here for your help!
Cheers,
wordman
Similar Threads
-
By hullabaloo in forum Database
Replies: 0
Last Post: 06-10-2009, 05:32 AM
-
By vindhvp in forum Database
Replies: 7
Last Post: 03-26-2009, 07:59 AM
-
By rkbnair in forum Database
Replies: 3
Last Post: 01-15-2007, 05:52 PM
-
By Rab in forum VB Classic
Replies: 7
Last Post: 07-25-2001, 12:45 PM
-
By Craig in forum Database
Replies: 3
Last Post: 05-31-2000, 03:19 AM
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|