简体   繁体   中英

Get data from all tables in database using php and mysql

I have a series of websites, all of which must share the same information. Any updates to text must be made across all websites. Instead of editing each site individually and uploading the updates files one at a time, I figured it'd be far better to have a central source using MySQL - update the database, and all websites will be changed at once.

I have limited knowledge of PHP and MySQL - everything below is what I've been able to put together for myself so far, using various online sources:

<?php
//DB INFO///////////////////////////////////////////////////////////////////////////////////////
$host="localhost"; // Host name
$username="####"; // Mysql username
$password="####"; // Mysql password
$db_name="####"; // Database name

// Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get all the data from the "example" table
$pge_logbookabout = "SELECT * FROM pge_logbookabout";
$pge_logbookabout = mysql_query($pge_logbookabout) or die(mysql_error());
$row_pge_logbookabout = mysql_fetch_assoc($pge_logbookabout);
?>

So far, I can use the above to select a table and echo in the HTML using:

<?php echo $row_pge_logbookabout['rep_lbapr'];?>

That's cool, but I'm only able to select one single table using this - I'd like to be able to select ALL tables, and simply enter variables in where I need them.

Will I need to repeat the third section of the above code for each table, or is there a simpler way for me to do this?

To display all records in a table, you need to do:

while($row_pge_logbookabout = mysql_fetch_assoc($pge_logbookabout)){
   echo $row_page_logbookabout['COLUMN'];
}

However if you mean that you want to display all records in each table, therefore you need separate queries to do so.

$query = mysql_query("select * from table1");
while($row_table1 = mysql_fetch_assoc($query)){
 // code here
}


$query = mysql_query("select * from table2");
while($row_table2 = mysql_fetch_assoc($query)){
 // code here
}

Please note this way of connecting to database, quering and fetching data will be deprecated starting PHP 5.5.0. Alternatively you can use PDO prepared statements

Select multiple tables using mysql

Update mutiple tables using mysql

How are you hosting your websites? Are they on the same hosting? If that is the case, you can use localhost. Otherwise you will need to enter the external hosting mysql database credentials.(multisite single database setup).

First you select all tables, then you get the data from each one. You could do this if you are using mysql_* , but I strongly recommend to use mysqli_* or PDO .

$result = mysql_query("show tables"); // Select all tables
while($table = mysql_fetch_array($result)) {
     $queryT = mysql_query("select * from '".$table[0]."'");
     while($rtable = mysql_fetch_array($queryT)){
          // data of the table
     }
}

If you want to process over a series of tables you can use something like:

$query_tables = mysql_query('SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name LIKE "%table%" ');

while($table = mysql_fetch_assoc($query_tables)){

    $query = mysql_query("select * from ".$table['table_name'] );
    // code here
}

You will have to make sure you choose the proper table names in the first query so you are processing the proper tables.

Then you can use php to loop over the tables updating a particular column/field. Not sure if that is what you are looking for...

More information at http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

I agree with the above poster... switch to PDO_MYSQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM