I have the following code:
$sql = "SELECT table.field, table2.field2 FROM
table, table2";
$stmt = $this->db->prepare($sql);
$stmt->execute();
$x = $stmt->fetchAll(PDO::FETCH_ASSOC);
I know it is not enough information, but I do not understand why the code executes only if I comment the fetchAll line. When that line executes, I do not get anything, just blank browser, no errors or anything. In firebug the response is blank... What can it be??
You are doing a Cartesian product of table
and table2
. This can be a HUGE amount of data.
For each row in table, you are getting all the rows in table2
. So if table
has 100 lines, and table2 has 500 lines, your query will try to fetch 50.000 lines.
You'll either want a JOIN:
SELECT table.field, table2.field2
FROM table
JOIN table2
ON table.field3 = table2.field3
Or a UNION
SELECT table.field AS FIELDNAME FROM table
UNION ALL
SELECT table2.field2 AS FIELDNAME FROM table2
If a Cartesian product is really what you want, try adding a LIMIT and see if that works. Then later increase or remove the limit
SELECT table.field, table2.field2
FROM table, table2 LIMIT 10
Depending on what your tables contain (you indeed did not give enough info).