I need proper syntax/procedure for inserting a variable into a SELECT query...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: I need proper syntax/procedure for inserting a variable into a SELECT query...

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Location
    Las Vegas, NV USA
    Posts
    3

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  3. #3
    Join Date
    Apr 2010
    Location
    Las Vegas, NV USA
    Posts
    3
    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

  4. #4
    Join Date
    Apr 2010
    Location
    Las Vegas, NV USA
    Posts
    3

    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

  1. Help with complex SQL query
    By hullabaloo in forum Database
    Replies: 0
    Last Post: 06-10-2009, 06:32 AM
  2. select query for a single table_ORDER BY
    By vindhvp in forum Database
    Replies: 7
    Last Post: 03-26-2009, 08:59 AM
  3. Proper Case in the Query
    By rkbnair in forum Database
    Replies: 3
    Last Post: 01-15-2007, 05:52 PM
  4. Replies: 7
    Last Post: 07-25-2001, 01:45 PM
  5. Replies: 3
    Last Post: 05-31-2000, 04: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center