Skip to main content
Known Participant
February 24, 2013
Answered

Can I use a php session username to select a mysql table?

  • February 24, 2013
  • 1 reply
  • 3343 views

In my site I have a situation where  

$_SESSION['MM_Username']

    will be the same name as a prepared MYSQL table.

(There will be a limited number of Users and tables)

Instead of writing a long series of "else if" to cope with each user on each page, I wish to employ the   $_SESSION['MM_Username']   to point to the correct MSQL table.

Take for example

<?php  $query_rstPalabs = "SELECT * FROM patricia WHERE yesno >= 1";  ?>

How can I substitute the    $_SESSION['MM_Username']   for "patricia"  here in a way that the SQL would select the table "patricia"?

Please be aware that I am not a professional designer, and flounder in the shallows of PHP/MSQL.  I use DW2004MX to help.
Thank you.

This topic has been closed for replies.
Correct answer sudarshan.t

Smiffy47,

Assign a variable to $_SESSION['MM_Username']. Call the variable within your query:

Example:

$user = $_SESSION['MM_Username'];

<?php  $query_rstPalabs = "SELECT * FROM $user WHERE yesno >= 1";  ?>

Be sure to use session_start in the beginning of your page so that you're allowed to call session variables within your application.

<?php session_start(); ?>

This should be added before your DOCTYPE declaration (Very beginning of your page).

-ST

1 reply

sudarshan.t
sudarshan.tCorrect answer
Inspiring
February 24, 2013

Smiffy47,

Assign a variable to $_SESSION['MM_Username']. Call the variable within your query:

Example:

$user = $_SESSION['MM_Username'];

<?php  $query_rstPalabs = "SELECT * FROM $user WHERE yesno >= 1";  ?>

Be sure to use session_start in the beginning of your page so that you're allowed to call session variables within your application.

<?php session_start(); ?>

This should be added before your DOCTYPE declaration (Very beginning of your page).

-ST

smiffy47Author
Known Participant
February 24, 2013

Thanks Sudarshan.

Very helpful.

Participating Frequently
February 25, 2013

Why do you have tables named after users?  Something seems wrong. Why not use a single user table? Is each table a completly different design?