Good day.!

Thank you for all your support to all my thread. By the way Ive got some error regarding of mysql query. I want to load all data from the table depending on the table name on the variable.!I dont know whats wrong with my query below.!

$coursesubject=$row_reclog['tablename']; 

mysql_select_db($database_enamysqldb, $enamysqldb);
$query_reclevelsub = "SELECT * FROM $coursesubject";
$reclevelsub = mysql_query($query_reclevelsub, $enamysqldb) or die(mysql_error());

$row_reclog; is a field from mysql database then i put it in a variable called $coursesubject. But when i put the variable that holds the tablename in the query, the error said that $coursesubject table cannot be found. Whats wrong with my query.? Maybe i forgot to put some string concantination. But i dont know what is it.

Pls help. thank you very very much.

God bless.!

Member Avatar for diafol

Do this to check that you've actually got a valid querystring:

$query_reclevelsub = "SELECT * FROM {$coursesubject}";

echo $query_reclevelsub;
exit;

Note you should "brace out" or escape your $coursesubject variable.

Below are 2 options to help solve the problem. Both do the same thing but with different amounts of complexety. My preference is the second code box. And so they are:

$enamysqldb=mysql_connect('localhost','username','password');
mysql_select_db($database_enamysqldb, $enamysqldb);

$coursesubject=mysql_real_escape_string($row_reclog['tablename']);
$query_reclevelsub = "SELECT * FROM $coursesubject";
$reclevelsub = mysql_query($query_reclevelsub,$enamysqldb) or die(mysql_error());

== and ==

mysql_connect('localhost','username','password');
mysql_select_db($database_enamysqldb);

$coursesubject=mysql_real_escape_string($row_reclog['tablename']);
$query_reclevelsub = "SELECT * FROM `".$coursesubject."`";
$reclevelsub = mysql_query($query_reclevelsub) or die(mysql_error());

Remember to replace the words username and password with your real mysql username and your real mysql password.

Below are 2 options to help solve the problem. Both do the same thing but with different amounts of complexety. My preference is the second code box. And so they are:

$enamysqldb=mysql_connect('localhost','username','password');
mysql_select_db($database_enamysqldb, $enamysqldb);

$coursesubject=mysql_real_escape_string($row_reclog['tablename']);
$query_reclevelsub = "SELECT * FROM $coursesubject";
$reclevelsub = mysql_query($query_reclevelsub,$enamysqldb) or die(mysql_error());

== and ==

mysql_connect('localhost','username','password');
mysql_select_db($database_enamysqldb);

$coursesubject=mysql_real_escape_string($row_reclog['tablename']);
$query_reclevelsub = "SELECT * FROM `".$coursesubject."`";
$reclevelsub = mysql_query($query_reclevelsub) or die(mysql_error());

Remember to replace the words username and password with your real mysql username and your real mysql password.

Thank you sir.!problem has now been solved.. Thank you very very much.!It mean so much to me..God bless us.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.