I have this query
$query = "Select * FROM table WHERE table.firs_column = 1;
Select * FROM table WHERE table.second_column = 1;
Select * FROM table WHERE table.third_column = 1;
Select * FROM table WHERE table.column = 1";
$stmt = $db->prepare($query);
$result = $stmt->execute();
I want to have multiple results, each one have the result of one query!
how to do it?
It looks like you are using PDO, so you could do something like:
$first_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$second_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$third_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$fourth_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
To get your 4 rowsets.
You can use UNION if table is same for your multiple queries
Select * FROM table WHERE table.firs_column = 1
UNION
Select * FROM table WHERE table.second_column = 1
UNION
Select * FROM table WHERE table.third_column = 1
UNION
Select * FROM table WHERE table.column = 1
After you finish with the first result set, use nextRowset() method to advance to the next result set.
You need to have a PHP MySQL driver extension that supports this method.
Not sure what you're aiming at, but did you try UNION?
Your SQL statement would look like this:
SELECT * FROM table WHERE table.firs_column = 1
UNION
SELECT * FROM table WHERE table.second_column = 1
UNION
SELECT * FROM table WHERE table.third_column = 1
UNION
SELECT * FROM table WHERE table.column = 1;
Please show your desired result if you think of something different.
Since we don't know anything about your database structure, I suggest looking into mysqli::multi_query().
If you're trying to pull related data, I highly suggest you look into doing MySQL JOINs instead. MySQL is another language unto itself that should be learned as a distinct language rather than just a string to be contatenated in PHP.
Related
How is it possible to select the first column in the Where clause. I am trying to make a php function to retrieve table data based on the id, yet since the titles of the id columns are different in various tables, I need to refer to the first column in the Where clause as the first column is always the id column.
The scenario would be something like the following, but it throws errors and says that there is an error in the SQL syntax.
$stmt = $this->conn->prepare("SELECT * FROM $table WHERE column(1) = :id");
Thanks in advance.
I don't think there's a built-in way to do this. But you can query INFORMATION_SCHEMA.COLUMNS to get the column names.
$col_stmt = $this->conn->prepare("
SELECT column_name
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND ordinal_position = 1
AND table_name = :table");
$col_stmt->execute([':table' => $table]);
$first_col = $col_stmt->fetchColumn();
$stmt = $this->conn->prepare("SELECT * FROM `$table` WHERE `$first_col` = :id");
Since there is no built-in way to do this, I came up with the following code block to get the first column and then use it in my SELECT statements.
$stmt = $this->conn->query("SHOW columns FROM $table");
return $stmt->fetch(PDO::FETCH_LAZY)[0];
I think this strategy is a bit shorter than Barmar's, though his is completely right and to the point.
I have some comma-separated records that I am saving in a MySQL database.
I have the following table and records:
tbl_checks
id | items
----+------
1 | 2,5
2 | 2,3
So I would like to fetch data where one of the comma-separated values in the column items is 5.
I have tried:
$sql = "SELECT * FROM tbl_checks WHERE items="; //unable todo at here
so that I can:
$results = $this->db->query($sql)->result() //codeigniter script
I have also tried
$sql = "SELECT * FROM tbl_checks WHERE items=".in_array(5)//this fails
Your current approach isn't that good, if you want to query against this field. Normally, you would use normalization and split that data into multiple tables which could queried better.
If you have stuck with this approach, you could use LIKE and CONCAT.
$id = '2';
$sql = "SELECT * FROM tbl_checks WHERE CONCAT(',', items, ',') LIKE '%,$id,%'";
$value = 5;
$result = $this->db->where("FIND_IN_SET($value,items) !=", 0)
->get("tbl_checks")->result_array();
I'm agree that such database isn't good constructed. Any way this query should work:
SELECT *
FROM tbl_checks
WHERE ( items LIKE "5,%" )
OR (item LIKE "%,5");
I have mysql column called categories. It can contain single or multiple values like this: 1 or 2 or 1,2,3 or 2,12...
I try to get all rows containing value 2.
$query = "SELECT * FROM my_table WHERE categories LIKE '2'";
$rows = mysql_query($query);
This returns row if column only has value 2 but not 1,2,3 or 2,12. How I can get all rows including value 2?
You can use either of the following:
% is a wildcard so it will match 2 or 1,2, etc. Anything on either side of a 2. The problem is it could match 21, 22, etc.
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
Instead you should consider the find_in_set mysql function which expects a comma separated list for the value.
$query = "SELECT * FROM my_table WHERE find_in_set('2', `categories`)";
Like #jitendrapurohut said, you can do it using
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
$rows = mysql_query($query);
But is really bad to store collections like this. A better aproach is as follow:
categories(id_c, name) => A table with each category
my_table(id_m [, ...])
categories_my_table(id_c, id_m)
Then use this query:
SELECT *
FROM my_table m
INNER JOIN categories_my_table cm ON m.id_m = cm.id_m
INNER JOIN categories c ON cm.id_c = c.id_c
WHERE
c.id_c = 2;
EDIT:
#e4c5 link explains why it is bad to store collections like this...
SELECT * FROM my_table WHERE categories LIKE '%2%' AND categories!='1,2,3' AND categories!='2,12';
I have multiple db files which contains the same table with different values.
example files:
wifi_16-09-02_09_34_03.db
wifi_16-09-02_09_44_06.db
wifi_16-09-02_09_60_02.db
How can I select all the rows from multiple files?
I only know to do this with one file.
Here is my code:
$dbfile = 'wifi_16-09-02_09_34_03.db';
$db = new SQLite3('dbs/'.$dbfile);
$sql = 'SELECT * FROM wifi';
$results = $db->query($sql);
I am new on SQLite3, any help would be appreciated.
You can simply open the other databases in the same way.
If you want to have all rows in a single query, you need to attach all the other databases to some main database, and use a compound query:
$db->exec("ATTACH 'dbs/wifi_16-09-02_09_44_06.db' AS db2");
$db->exec("ATTACH 'dbs/wifi_16-09-02_09_60_02.db' AS db3");
...
$sql = <<<'SQL'
SELECT * FROM main.wifi
UNION ALL
SELECT * FROM db2.wifi
UNION ALL
SELECT * FROM db3.wifi
SQL;
$results = $db->query($sql);
How should I go if I wanted to query a query result in php?
Supose I have this..
$result = mysql_query("SELECT * FROM tbl_x WHERE tbl_x.attribute = y");
And now I need to query that $result to filter the results I got from its query. Note that I don't want to do this...
$result2 = mysql_query("SELECT * FROM ( SELECT * FROM tbl_x WHERE tbl_x.attribute = y ) AS tbl_x1 WHERE tbl_x1... etc");
The reason I want to avoid that is I'm getting a "Duplicate column" error when executing a query like that.
I'm looking for something like...
$result = mysql_query("SELECT * FROM tbl_x");
$result2 = mysql_query_result($result);
You can apply more than one term in your search conditions, separated by AND:
$result = mysql_query("SELECT * FROM tbl_x
WHERE tbl_x.attribute = y
AND tbl_x.attribute2 = z");
Re your comments above. It sounds like you are using the Entity-Attribute-Value design.
In order to match multiple attributes, you have to do some tricks. Normally, a WHERE clause can only apply to one row at a time. But since each of your attributes are stored on separate rows, you need to do either one of two solutions:
Join multiple rows into one row, so you can use WHERE on all attributes in one condition.
SELECT config_id
FROM attributes AS s
JOIN attributes AS c USING (config_id)
JOIN attributes AS l USING (config_id)
WHERE (s.attr, s.value) = ('size', 'M')
AND (c.attr, c.value) = ('colour, 'green')
AND (l.attr, l.value) = ('cloth', 'cotton);
Search for any of the several attributes, and then if the number of rows that match is equal to the number of attributes you were searching for, you've found them all.
SELECT config_id
FROM attributes
WHERE (attr, value) = ('size', 'M')
OR (attr, value) = ('colour', 'green')
OR (attr, value) = ('cloth', 'cotton')
GROUP BY config_id
HAVING COUNT(DISTINCT attr) = 3;
You can still have your query of query without getting "duplicate column" error. Just alias and name the columns
$result2 = mysql_query("SELECT tbl_x1.col1, tbl_x1.col2,...etc FROM
(SELECT * FROM tbl_x WHERE tbl_x.attribute = y) AS tbl_x1
WHERE tbl_x1... etc");