I am trying to access multiple tables from mySQL via my php in wordpress. My code looks like this...
function retrieve_libraries( $data ) {
$second_db = new wpdb(DB_USER, DB_PASSWORD, "saic3_LibraryIndex", DB_HOST);
$query = "SELECT * FROM `Library` WHERE library_id=74 UNION SELECT * FROM `Library_Collection` WHERE library_id=74";
$list = $second_db->get_results($query);
return $list;
}
Before I had my query like this...
$query = "SELECT * FROM `Library` WHERE library_id=74"
And the data returned just fine. I have been looking into the UNION statement and the UNION ALL and both seem to not work. Am I calling this wrong? is my syntax off? Asking for a friend.
UNION isn't going to work unless Library and Library_Collection have the same number and type of columns.
Based on the table names*, it looks like you probably want to JOIN the two tables instead of UNION them.
$query = "SELECT * FROM `Library`
LEFT JOIN `Library_Collection`
ON Library.library_id = Library_Collection.library_id
WHERE Library.library_id=74";
*(I'm assuming Library_Collection is the things a Library has.)
Related
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);
Hi there I have 2 tables
|id|musicname|url|image|type
and the second table
|id|user|songslist|
inside songsids theres an array like this
1,3,5,6,8 etc ...
What Im aiming to do is select * from table1 and echo out the table1 as in an array but instead of tables two array , the actual row of table1.
So basically To take out each row that contains the id in songslist and put them all into a php array.
I have learned a lot about PHP arrays , but I'm not that good with mysql , Any Idea of how can I do that ?
EDIT
$selectmusiclist = mysql_query("SELECT * FROM music");
$songslist = array();
while ($songs = mysql_fetch_assoc($selectmusiclist)){
$songslist[] = $songs;
}
and then table 2 select:
$username="user1";
$selectuser = mysql_query("SELECT * FROM usersmusic where user=$username");
$user = mysql_fetch_assoc($selectuser);
$songslist = $user['songslist'];
NOW I need to tell the array $songslist[] to output only the songs with id $songslist contained ids
I think running a join like this will give you the results you are after.
SELECT * FROM usersmusic as um
join music as m
on um.songslist = m.id
where user = '$username'
If $username is not a static value make sure you escape it; don't want to get SQL injected in the future.
Also note the mysql_ driver is now deprecated you should consider updating to mysqli or PDO.
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.
I was wondering how to do a query with two tables in php?
I have this single query
?php
$sQuery = "Select * From tb_columnas Where col_Status='activo' Order by col_ID DESC";
$result = mysql_query($sQuery, $cnxMySQL) or die(mysql_error());
$rows_result = mysql_fetch_assoc($result);
$total_rows_result = mysql_num_rows($result);
if ($total_rows_result > 0){
do {
$id_columnas = $rows_result ['col_ID'];
$col_Titulo = $rows_result ['col_Titulo'];
$col_Resumen = $rows_result ['col_Resumen'];
$col_Fecha = $rows_result ['col_Fecha'];
$col_Autor = $rows_result ['col_Autor'];
?>
But I'd like to compare the col_Autor with au_Nombre which is in another table (tb_autores) and get au_Photo and other values from it, how can I do that?
You can do a simple join query without using the JOIN keyword by specifying the two tables in the FROM clause and establishing a relationship in the where clause.
For example
SELECT columns
FROM table1, table2
WHERE table1.field = table2.field
You are asking about SQL Joins, the practicing of putting two or more tables together in an SQL statement to return data from more than 1 table. You join the tables on a common column, such as author.authorid = book.authorid. I suggest looking up JOINS on google, there are many good articles.
A great article on it: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
It sounds like you are looking for a join. Try something like the following:
SELECT * FROM tb_columnas JOIN tb_autores ON tb_columnas = col_Autor WHERE col_Status='activo' ORDER BY col_ID DESC
You need to understand joins for this.
Here you will find very good explanation of the same:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
I'm trying to wrap a count(*) query around an existing Zend_Db select statement, but all I was able to get is:
SELECT `t`.*, COUNT(*) AS `TotalRecords` FROM (SELECT ....) AS `t`
However I like to get rid of the t.* as I only need the count(*).
This is my code so far:
$db = Zend_Registry::get('db');
$select = $dbmodel->getSomething(); //zend select object
$outterSelect = new Zend_Db_Select($db);
$outterSelect->from($select)->columns(array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));
echo $outterSelect->__toString();
Any help is appreciated!
You can simply write:
$outterSelect->from($select, 'COUNT(*) as TotalRecords');