[英]How to dump mysql table relations in PHP
我進行了很多搜索,但是找不到轉儲表關系的方法,例如一對一,一對多與PHP。
有沒有辦法在PHP中處理此問題?
結果可能是:
array(
'tableA' => array(
'one-to-one' => array('tableB', 'tableC'),
'one-to-many' => array('tableD'),
'tableB' => array(
'one-to-one' => array('tableA')
...
)
任何建議,不勝感激。
我在http://dev.mysql.com/doc/refman/5.0/en/show-table-status.html上找到了一個腳本,該腳本描述了用正則表達式解析表信息。 我已經操縱了代碼才能正常工作。 這是代碼。
<?php
$conn = mysql_connect('localhost', 'user', 'pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
//DB connection already established
mysql_query("use db");
$res = mysql_query("SHOW CREATE TABLE tbl");
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
//Only work on InnoDB foreign key info.
if(preg_match_all(
'/FOREIGN KEY \(`(.*)`\) REFERENCES `(.*)` \(`(.*)`\)/',
$row['Create Table'],
$matchArr)) {
print_r($matchArr); //which writes down the result
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.