I a set of tables and fields that I would like to select data from. I have tried the below code without success. Could any one explain to me why this does not work, and if possible, how to make it work.
$fields = "table1.field1, table2.field2, table3.field3, table4.field4";
$tables = "table1, table2, table3, table4";
$table = explode(', ', $tables); //explode the tables string
$field = explode(', ', $fields); //explode the fields string
$i=1;
while ($i<=4) {
$sql = 'SELECT ' . $field[$i] . ' FROM ' . $table[$i] . ' WHERE ' . $field[$i] . ' LIKE "%' . $str . '%";';
$results = $readConn->query($sql);
$i++;
var_dump($results);
}
The SQL you’re creating looks like this:
I don’t know what you’re setting $str to, so I don’t have it included here.
The SQL should run, it looks fine, except that last one … You’ll want to adjust your loop to be, <4, instead of <=4
Are you sure that you have a valid connection to the database? Are you getting back NULL or something as a result, or an error?
Also, item #2 in Mark’s answer.
Also, personally, I find this a lot more readable.
than this