Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php mysql query

Options
  • 21-12-2005 4:39pm
    #1
    Registered Users Posts: 673 ✭✭✭


    hey,

    There is probably a really easy solution to this but i cant seem to figure it out.

    i am doing a php query of a mysql database with the following code:
    $query = "SELECT user_answer_q".$question_code."
    		FROM w1_survey_results
    		WHERE survey_user_id = ".$_SESSION['user_id']."";
    		$results = mysql_query($query)
    			or die(mysql_error());
    
    		while ($rows = mysql_fetch_array($results)) {
    		extract ($rows);
    		echo ?????????
    		}
    

    The select statament has to be partly made up of a variable i.e. "SELECT user_answer_q".$question_code."

    When i get to the bottom of the query and try and echo the results what do i put in for the variable to be echo'd out?


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Ah I see.

    Use an alias for the column you want to select , eg..
    SELECT user_answer_q".$question_code." as answer
    		FROM w1_survey_results
    

    Then you can just write
    echo $answer;
    


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Got it sorted FINALLY!!!! Cheers m8 :D


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    If you *really* want to know if it's possible to do it using the original name of the column, you could have done
    $col_name = "user_answer_q".$question_code;
    .....
    echo $$col_name;
    
    Much harder to read (and marginally less efficient) than the other solution.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Similar question again!!

    Now im trying to create a table with a php script where the table name has a variable in it:

    $sql = "CREATE TABLE ".$week."_survey_results

    is this done with an as statement as well?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    No.

    If you need to refer to this table later on, I would set the name of the table as a variable, e.g.

    $table_name = $week."_survey_results";

    $sql = "CREATE TABLE $table_name";

    Without viewing the rest of your code, I suspect there may be an easier way to do it than dynamically creating tables.


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    Thanks again m8, your a life saver ;)


Advertisement