+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP / MySQL SELECT problem

  1. #1
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    PHP / MySQL SELECT problem

    I'm attempting to set up a simple MySQL SELECT which uses a variable to identify the row it selects.

    If I use the following (using an actual id rather than a variable) it works perfectly:

    <?php
    // Request text for page
    $content = @mysql_query('SELECT text FROM page_text WHERE id="9"');
    if (!$content) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    // Display text for page
    while ($row = mysql_fetch_array($content)) {
    echo $row['text'];
    }
    ?>

    However as soon as I try to introduce a variable such as:

    <?php $id = 9; ?>

    <?php
    // Request text for page
    $content = @mysql_query('SELECT text FROM page_text WHERE id="$id"');
    if (!$content) {
    ...

    it doesn't work

    I've tried a few different options with " and ' changes, but I either get nothing or an error message

    The plan is to use the SELECT statement as an Include, hence wanting to set up the variable.

    Any thoughts?
    Thanks

  2. #2
    manik is offline x10Hosting Member manik is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    45

    Re: PHP / MySQL SELECT problem

    Why not try to print what sql gets generated and see for yourself where things have gone wrong.
    I presume that you are using some text box for that variable. There may be blank spaces with it. Trim it out and see for yourself. but the best way to debug is to insert print statements.

  3. #3
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: PHP / MySQL SELECT problem

    The string must be enclosed in double-quotes for variables to be evaluated. So change it to:

    PHP Code:
    $content = @mysql_query("SELECT text FROM page_text WHERE id=\"$id\""); 
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  4. #4
    stevet70 is offline x10Hosting Member stevet70 is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    35

    Re: PHP / MySQL SELECT problem

    Thanks woiwky, that works a treat

    genius!

+ Reply to Thread

Similar Threads

  1. Problem selecting database with PHP
    By stevet70 in forum Programming Help
    Replies: 2
    Last Post: 01-05-2009, 03:41 PM
  2. PHP memory limit problem when creating zip file on fly?
    By DaveBC in forum Programming Help
    Replies: 8
    Last Post: 09-04-2008, 05:06 PM
  3. problem with finding my mysql database (PHP)
    By wikiadmin in forum Free Hosting
    Replies: 1
    Last Post: 06-29-2008, 03:48 PM
  4. php problem
    By himgar in forum Free Hosting
    Replies: 1
    Last Post: 02-22-2008, 02:41 AM
  5. MySQL problem
    By pensoftware in forum Free Hosting
    Replies: 3
    Last Post: 10-30-2005, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers