I would like to replace the table name in a database with a php variable so that it can be store in a variable file included on all pages, and then all the instances of the table name in the website can be changed simply by changing the value of the one variable.
I have never done this before (is it possible?), and would like to know what the proper syntax would be? For example, if I set $name = “user_name”, then could I use it in all mysql queries such as “SELECT … FROM $name WHERE …” ?
I am not using PDO. The original table names were not reserved words, the two tables that I did this with were “locations” (which I gave the variable name $locations) and “user_info” which I gave the variable name $information. By the way, before I replaced the table names in my mySqL queries with the variables, everything worked fine. I have the following in my variables.php file (in the includes folder)
$mysql_link = mysql_connect("localhost","root","jfisuela")
or die("Cannot make a connection to the database");
mysql_select_db("sampleDatabase",$mysql_link)
or die("Cannot select the database");
I had no issues with my queries before I replaced the table names with variables.
I think I know what my issue was, because in your example you are concatenating the variable to the rest of the query string. I was using the following:
$query = "SELECT user_name FROM $information WHERE ...";
.
Should it be
$query = "SELECT user_name FROM " . $information . " WHERE ...";
?
This is where my total lack of experience shows up!
That is exactly why you want to set error_level to E_ALL (or even -1) on development machines (not on production machines, it will just clutter the logs), so you get a warning for undefined variables. Makes it a lot easier to spot errors like these
Yeah, with you on that - we seem to be banging out this simple message over and over ad infinitum.
It really is the starting place if you want to “divide and conquer” code errors when you are not sure if;
a) your html form is bust
b) your PHP is badly written
c) your SQL statement is badly written,
and my favourite…
d) your database does not even have any matching data, so always returns nothing no matter how much you fiddle with a) b) and c) above.
Is a carefully crafted message and “how to” worthy of a sticky I wonder?
If people read them it’s worth it. Basic debugging methodology is a good skill to learn; a lot of people make assumptions about where the problem isn’t.
Yeah, debugging is a skill you learn, but I wish someone had showed me, I had to work it out logically myself - mixed in with tricks I learned hanging around here.
Even if there was a sticky “how to debug your code”, ppl would not generally read it - but at least there’d be a link which would just need the intro “read this first, then tell us what errors you are still getting <link>”.
Overall though, its less about “how to debug your code”, its more about “how to progressively develop your code proving to yourself after each step that what you planned to happen actually did happen before you go off and complicate it even more” - which is an entirely different mindset.