Table join in php

PHP: How to join two HTML tables into one?

I have two HTML tables. Each has three rows and one column only. I’d like to join them programmatically such that I get one table with two columns and three rows. Is there some function or hack to achieve this? How can I achieve this? For example First Table:

 
 
 

Is your input to the join function this HTML? Or are you generating this HTML in your code? If the latter, this seems easy enough if you have two arrays. $t1 = array(‘data one’, . ) and $t2 = array(‘data one’, . ) . Merge those two arrays then generate your single table.

@TPoy The HTML for both the tables is generated in one .php file and is passed to (actually returned by the function in which the HTML for both the tables is being generated) another .php file. Now in this file I need to join both those tables to get the #table_three

The 1st php’s job is to only generate these two tables ? Are you forced to return tables from it or you can return an array of data that you can use later to generate the three tables if you wish? are the data in both tables of same length (count) ?

Читайте также:  Выравнивание содержимого тегов

@Enissay I am forced to return the html for the tables in a string variable. Yes the data in both the tables are same count.

4 Answers 4

Well, this is a way to proceed (check in-code comments):

$table1 =                 _DATA_; $table2 =                 _DATA_; // Load teh 1st table // Create a DOM object $html1 = new simple_html_dom(); // Load HTML from a string $html1->load($table1); // Load teh 2nd table // Create a DOM object $html2 = new simple_html_dom(); // Load HTML from a string $html2->load($table2); // Find all rows from both tables $rows1 = $html1->find('tr'); $rows2 = $html2->find('tr'); // Build teh 3rd table $table3 = ' '; // Insert rows cells from both initial tables for ($i=0; $i < count($rows1); $i++) < $table3 .= ''; // get row's innerhtml $table3 .= $rows1[$i]->innertext; $table3 .= $rows2[$i]->innertext; $table3 .= ''; >; // finish the table $table3 .= '
'; // Clear DOM objects $html1->clear(); unset($html1); $html2->clear(); unset($html2);

Источник

inner join in php – How to Use Joins in PHP?

inner join in php – A join lets you combine columns from two or more tables into a single result or say a SQL statement that retrieves data from two tables is called a join.

Inner joins let us select rows that have same value in both tables for specified columns thereby returns matching rows.

inner join in php – inner join program code in PHP, MySQL

Inner join shows results from both tables where there is any match between columns in both tables. here you will learn to MySQL JOINS Tutorial: INNER, OUTER, LEFT, RIGHT, CROSS.

INNER JOIN

 mysql_select_db("Memberloyees", $link); $qry="SELECT Member.id,Member.MemberNm, group.group_name FROM Member INNER JOIN group on Member.id = group.group_id"; $result=mysql_query($qry)ordie(mysql_error()); echo"  MemberId MemberNm groupName "; while($row = mysql_fetch_array($result))< echo ""; echo "" . $row['id'] . ""; echo "" . $row['MemberNm'] . ""; echo "" . $row['group_name'] . ""; echo ""; > echo ""; mysql_close($link); ?>

RIGHT OUTER JOIN

 mysql_select_db("Memberloyees", $link); $qry="SELECT Member.id, Member.MemberNm,group.group_name from Member RIGHT OUTER JOIN group on Member.id=group.group_id"; $result=mysql_query($qry)ordie(mysql_error()); echo"  MemberId MemberNm groupName "; while($row = mysql_fetch_array($result))< echo ""; echo "" . $row['id'] . ""; echo "" . $row['MemberNm'] . ""; echo "" . $row['group_name'] . ""; echo ""; > echo ""; mysql_close($link); ?>

MySQL INNER JOIN clause

SELECT select_list FROM t1 INNER JOIN t2 ON join_condition1 INNER JOIN t3 ON join_condition2 . ;

SELF JOIN

 mysql_select_db("Memberloyees", $link); $qry="select a.team_name,b.team_name,a.team_city from Member a, Member b where a.team_city=b.team_city and a.team_name<>b.team_name"; $result=mysql_query($qry)ordie(mysql_error()); echo"  MemberName MemberCity "; while($row = mysql_fetch_array($result))< echo ""; echo "" . $row['team_name'] . ""; echo "" . $row['team_city'] . ""; echo ""; > echo ""; mysql_close($link); ?>

PHP SQL Inner Join

Source Code of innerJoin.php

 mysql_select_db("test", $link); $result = mysql_query("SELECT team.name, comment.comments FROM team INNER JOIN comment ON team.team_id = comment.team_id"); echo " "; while ($row = mysql_fetch_array($result)) < echo ""; echo ""; echo ""; echo ""; > echo "
Name Comments
" . $row['name'] . "" . $row['comments'] . "
"; mysql_close($con); ?>

I hope you get an idea about inner join in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Источник

PHP-Mysql table join from different host

There is a table employee in the database abc_db at abc@localhost(server) and there is another table department in the database xyz_db at xyz@localhost(server). How can I join the tables using php mysql connection. I have written the following code but it does not generate any resource id.

$conn = mysql_connect("localhost","abc","abcdb"); $conn1 = mysql_connect("localhost","xyz","xyzdb"); $db_select=mysql_select_db("abc_db",$conn); $db_select1=mysql_select_db("xyz_db",$conn1); $sql="SELECT * FROM employee e LEFT JOIN department d ON e.department_id=d.id "; $res=mysql_query($sql); 

You cannot join across two connections, but you can join across databases on the same host — and you only need a single connection for that. See the end of @RobertPitt’s answer.

5 Answers 5

You can’t join two tables using different connections to the database, not from PHP, nor on the MySQL server. (@RobertPitt has a good point: do you actually need two connections? It’s possible to join two tables on the same host, but in different databases, within one connection — assuming your connection has the necessary privileges to access both)

If you have control over one or other of the databases, you might try setting up a federated table; make sure that the performance is OK though (if the db machines don’t have a fast, low-latency connection (i.e. directly joined by a cable), don’t bother), and there is a long list of limitations.

  • replicate the table from one server to the other (tricky to set up)
  • «join» them manually in PHP (gross, inefficient, but pretty much your only choice if you don’t have control over the database)

Ibe just read some of the documentation and it does not seem to be a HUGE help, but I never knew about it so at least I know of it now 🙂

Источник

MYSQL table join in PHP

I have searched for my question but none seems to answer my particular need. It is a simple table join but every thing i have tried has failed. maybe you can help. NOTES: all rows have post_id in POSTS but not all post_ids are related in CALENDAR, ie not all posts have calendar entries. And I am using MYSQL in PHP, not MYSQLi. POST_CAT- post_id, cat_id POSTS- post_id, title, description CALENDAR- post_id, on_date, to_date currently i found the relevant post_ids through a many to many selection then used a series of for loops to output from a single table. this works. but i need to include the calendar entries for relevant post_id in one array:

mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("adamsmap_db") or die(mysql_error()); if (isset($_POST['activities'])) < $post_id = mysql_query("SELECT post_id FROM post_cat WHERE cat_id = '1'"); while ($row = mysql_fetch_array($post_id, MYSQL_NUM)) < for ($i=0;$i < count($row); $i++) < $output = mysql_query("SELECT user_id, title, description FROM posts WHERE post_id='$row[$i]'"); while ($rowoutput = mysql_fetch_array($output, MYSQL_NUM)) < for($i=0;$i < count($rowoutput);$i++) < echo $rowoutput[$i]."&nbsp"; >echo" 

"; >>>>

For future users seeing your questions. If you know this, great! Please, don’t use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi — this article will help you decide which. If you choose PDO, here is a good tutorial.

Источник

Table Join in php mysql

I want to join all the tables, in all tables all columns names are same. My query is below. Please help me to fetch the data. My table structure is:

CREATE TABLE IF NOT EXISTS `play_school` ( `token_id` varchar(255) NOT NULL, `id` int(11) NOT NULL AUTO_INCREMENT, `ad_id` varchar(255) NOT NULL, `title` text NOT NULL, `category` varchar(255) NOT NULL, `name` text NOT NULL, `image` varchar(255) NOT NULL, `content` text NOT NULL, `offer` text NOT NULL, `note` text NOT NULL, `price` varchar(255) NOT NULL, `address` varchar(255) NOT NULL, `contact_no` text NOT NULL, `email_id` text NOT NULL, `timestamp` date NOT NULL, `status` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `token_id` (`token_id`), UNIQUE KEY `token_id_2` (`token_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ; 

All tables structure are same for car_showroom ,coaching , electronic . like play_school table, in all tables ad_id=’xyz’ are same and the get id is also ‘xyz’.now i want to fetch data from all table where ad_id=’xyz’. This query is running without any error . but didn’t fetch the data.

$email_id = $_GET['id']; $result = $con->prepare("SELECT * FROM bike_showroom JOIN car_showroom JOIN coaching JOIN college JOIN electronic JOIN furniture_showroom JOIN hospital JOIN job JOIN mobile_shop JOIN pets_shops JOIN play_school JOIN real_estate JOIN services JOIN shopping_store JOIN stationary_shops JOIN sweet_shop WHERE bike_showroom.ad_id = '".$email_id. "' AND car_showroom.ad_id = '".$email_id. "' AND coaching.ad_id = '".$email_id. "' AND college.ad_id = '".$email_id. "' AND electronic.ad_id = '".$email_id. "' AND furniture_showroom.ad_id = '".$email_id. "' AND hospital.ad_id = '".$email_id. "' AND job.ad_id = '".$email_id. "' AND mobile_shop.ad_id = '".$email_id. "' AND pets_shops.ad_id = '".$email_id. "' AND play_school.ad_id = '".$email_id. "' AND real_estate.ad_id = '".$email_id. "' AND services.ad_id = '".$email_id. "' AND shopping_store.ad_id = '".$email_id. "' AND stationary_shops.ad_id = '".$email_id. "' AND sweet_shop.ad_id = '".$email_id."' "); $result->execute(); $row = $result->fetch(); for($i=0; $row = $result->fetch(); $i++)

Источник

Оцените статью