Error when using a variable to select database table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doc1355
    New Member
    • Sep 2007
    • 16

    Error when using a variable to select database table

    Hi,
    I have a script that I need to use a variable to select the database table that I want to update or to get the results from. But for some reason when I use the variable I get error. If I replace the variable with the table name, it works fine.

    Here is the part of the code related to this issue:
    [CODE=php]<?php
    $memberID = $_GET['cm'];
    if ($memberID < 50001)
    $data_table = 'new_members' &&
    $auth_token = "first_toke n";
    else
    $data_table = 'old_members' &&
    $auth_token = "second_tok en";



    $find_customer = mysql_query("SE LECT * FROM '$data_table' WHERE id = $memberID");
    while($customer _info = mysql_fetch_arr ay($find_custom er))
    {
    $customer_full_ name = $customer_info['first_name'] . " " . $customer_info['last_name'];
    $customer_email _address = $customer_info['email'];
    }
    ?>
    [/CODE]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Doc.

    Try using backticks instead of quotes:
    [code=mysql]
    SELECT * FROM `$data_table` WHERE id = $memberID
    [/code]

    Comment

    • doc1355
      New Member
      • Sep 2007
      • 16

      #3
      I did that but same problem, still not connecting to the table.
      I also tried with out quotes, but no luck.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Doc.

        What happens when you echo mysql_error()?

        Comment

        • doc1355
          New Member
          • Sep 2007
          • 16

          #5
          I get multiple Undefined variable error messages.


          Notice: Undefined variable: customer_email_ address in F:\USERS\userna me\www\live\for ms\registration \success.php on line 177

          Notice: Undefined variable: customer_full_n ame in F:\USERS\userna me\www\live\for ms\registration \success.php on line 173

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Examine this a bit closer
            [PHP]if ($memberID < 50001)
            $data_table = 'new_members' &&
            $auth_token = "first_toke n";
            else
            $data_table = 'old_members' &&
            $auth_token = "second_tok en"; [/PHP]

            Comment

            Working...