I have a database which is build of lets say 4 tables. Each table has the same structure of fields. One of this fields is a data/time field.
Now. Lets say there are 3 records in each table.
And the question is how to gather all this records from all tables into one php array and sort this array by data/time field ?
For now I can only do that (and I now that it is not very effective but works fine):
1. Create a function which connect to database and transfere a query to database
Code: Select all
<?php
function connect_and_do($qu)
{
$host = 'localhost';
$user = 'root';
$pass = '';
$db_name = 'my_database';
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
$result = mysql_query($qu);
mysql_close();
return $result;
}
?>Code: Select all
$query1_SQL = "SELECT * FROM table1 WHERE field1 = '".$some_value."';";
$query2_SQL = "SELECT * FROM table2 WHERE field1 = '".$some_value."';";
$query3_SQL = "SELECT * FROM table3 WHERE field1 = '".$some_value."';";
$query4_SQL = "SELECT * FROM table4 WHERE field1 = '".$some_value."';";Code: Select all
$result1 = connect_and_do ( $query1_SQL );
$result2 = connect_and_do ( $query2_SQL );
$result3 = connect_and_do ( $query3_SQL );
$result4 = connect_and_do ( $query4_SQL );Code: Select all
print ( "<table border = \"1\">" );
while ( $draw1 = mysql_fetch_row ( $result1 ))
{
print ( "<tr>" );
for ( $i = 0; $i < count ( $draw1 ); $i++ )
{
print ( "<td width = \"150\">".$draw1[$i]."</td>" );
}
print ( "</tr>");
}
while ( $draw2 = mysql_fetch_row ( $result2 ))
{
print ( "<tr>" );
for ( $j = 0; $j < count ( $draw2 ); $j++ )
{
print ( "<td width = \"150\">".$draw2[$j]."</td>" );
}
print ( "</tr>");
}
while ( $draw3 = mysql_fetch_row ( $result3 ))
{
print ( "<tr>" );
for ( $k = 0; $k < count ( $draw3 ); $k++ )
{
print ( "<td width = \"150\">".$draw3[$k]."</td>" );
}
print ( "</tr>");
}
while ( $draw4 = mysql_fetch_row ( $result4 ))
{
print ( "<tr>" );
for ( $l = 0; $l < count ( $draw4 ); $l++ )
{
print ( "<td width = \"150\">".$draw4[$l]."</td>" );
}
print ( "</tr>");
}
print ( "</table>" );Its pretty complicated and this complication is probably unnessesery I know
Anyway the result is almost what I would like except one detail
Sorry for so huge post