虽然我没有成功使用php路由,因为超出了我目前的知识,我尝试了下面的实现:
$query = "SELECT * FROM persons WHERE SURNAME LIKE 'Ω%' ORDER BY SURNAME ASC;";
$result = mysqli_query($con, $query); //the selected query has 2 results!
/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);
printf("results (%d).", $row_cnt);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysqli_error($con) . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
function generateTableFromResult($result) {
$x = 1;
$html = "<table>";
while($row = mysqli_fetch_array($result)) {
foreach($row as $column => $value) {
copy("persons.php","newfile".$x.".php");
$html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
$myfile = fopen("newfile".$x.".php", "w") or die("Unable to open file!");
fwrite($myfile, $html);
}
$x++;
}
$html .= "</table>";
return $html;
}
// usage:
// ...
$result = mysqli_query($con, $query);
echo generateTableFromResult($result);`
以上脚本创建:
newfile1.php --> table + table (first + second result)
newfile2.php --> table + table (first + second result)
虽然我想要:
newfile1.php --> table (first result)
newfile2.php --> table (second result)
我在这里缺少什么?
编辑:
newfile1.php
的示例内容:
CODE_NO 12101
SURNAME ΩΝΑΣΗ
FIRST_NAME ΧΡΙΣΤΙΝΑ
SOURCE1 ΗΛΙΟΥ
SOURCE2
SOURCE3
PROV ΟΙΚΟΓΕΝΕΙΑΚΗ ΣΧΕΣΗ ΚΑΘΕΤΗ ΜΕ ΚΙΝΗΤΙΚΟΤΗΤΑ
SEX ΓΥΝΑΙΚΑ
BIRTH 1950
DEATH 1989
PLACE ΜΕΓΑΛΟ ΑΣΤΙΚΟ ΚΕΝΤΡΟ
REGION ΑΛΛΕΣ ΞΕΝΕΣ ΧΩΡΕΣ
EDUCATION ΜΕΤΑΠΤΥΧΙΑΚΕΣ ΣΠΟΥΔΕΣ
SPECIAL
WRITING
POLITICAL
MANAGERIAL
MILITARY
RELIGIOUS
SPIRITUAL
SCIENTIFIC
NOBLE ΝΑΙ
CULTURAL
FINANCIAL ΝΑΙ
CONSTITUTIONAL
OPPOSITION
PROF1 Επιχειρηματίας
PROF2 Νεώτερη αριστοκρατική ελίτ πλουτου
PROF3
PROF4
PROF5
PARTY
ACTIVITY1
ACTIVITY2
1800_1832
1833_1862
1863_1911
1912_1922
1923_1944
1945_1973 ΝΑΙ
1974_ ΝΑΙ
HIERARCHY Ανωτάτη Ελίτ
LEADERSHIP
HERE启动表2 这就是表格的样子。请记住,我希望每个人/文件都能写出每个表。
CODE_NO 12100
SURNAME ΩΝΑΣΗΣ
FIRST_NAME ΑΡΙΣΤΟΤΕΛΗΣ
SOURCE1 ΗΛΙΟΥ
SOURCE2 ΔΡΑΝΔΑΚΗ
SOURCE3 ΕΚΔΟΤΙΚΗ
PROV ΑΓΝΩΣΤΗ
SEX ΑΝΔΡΑΣ
BIRTH 1906
DEATH 1975
PLACE ΑΓΡΟΤΙΚΕΣ- ΝΗΣΙΩΤΙΚΕΣ
REGION ΜΙΚΡΑ ΑΣΙΑ
EDUCATION ΔΕΝ ΔΗΛΩΝΕΙ
SPECIAL
WRITING
POLITICAL
MANAGERIAL
MILITARY
RELIGIOUS
SPIRITUAL
SCIENTIFIC
NOBLE ΝΑΙ
CULTURAL
FINANCIAL ΝΑΙ
CONSTITUTIONAL
OPPOSITION
PROF1 Εφοπλιστής
PROF2 Νεώτερη αριστοκρατική ελίτ πλουτου
PROF3
PROF4
PROF5
PARTY
ACTIVITY1
ACTIVITY2
1800_1832
1833_1862
1863_1911
1912_1922
1923_1944 ΝΑΙ
1945_1973 ΝΑΙ
1974_
HIERARCHY Yψιστη Μορφή Ελίτ
LEADERSHIP
答案 0 :(得分:3)
这应该产生你想要的输出(来自你的评论):
function generateTableFromResult($result) {
$x = 1;
$html = '';
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Add the open-table tag
$html = '<table>';
// Iterate through the columns and add them to our variable
foreach($row as $column => $value) {
$html .= "<tr><th>".$column."</th><td>".$value."</td></tr>";
}
// Add the close-table tag
$html .= "</table>";
// Now, since we've added all the rows for that person,
// create, populate and store the file
file_put_contents("newfile".$x.".php", $html);
// Increment $x before we jump to the next person
$x++;
}
// However, this will only return the table for the last person
return $html;
}
我还将您的fopen
和copy
(实际上没有用)更改为一个简单的命令:file_put_contents()
对@ishegg大吼大叫帮助缩小了问题范围
答案 1 :(得分:2)
默认情况下,mysqli_fetch_array()
的获取模式为MYSQLI_BOTH
,这意味着它会以关联的和数字格式获取每个结果。除了对结果进行双循环外,还可以为您提供结果。
要解决此问题,您可以使用MYSQLI_ASSOC
(或使用mysqli_fetch_assoc()
),因此每行只会获得一次。
function generateTableFromResult($result) {
$x = 1;
$html = "<table>";
while($row = mysqli_fetch_assoc($result)) {
foreach($row as $column => $value) {
copy("persons.php","newfile".$x.".php");
$html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
$myfile = fopen("newfile".$x.".php", "w") or die("Unable to open file!");
fwrite($myfile, $html);
}
$x++;
}
$html.="</table>";
return $html;
}
或者,保持原有的功能:
function generateTableFromResult($result) {
$x = 1;
$html = "<table>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
foreach($row as $column => $value) {
copy("persons.php","newfile".$x.".php");
$html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
$myfile = fopen("newfile".$x.".php", "w") or die("Unable to open file!");
fwrite($myfile, $html);
}
$x++;
}
$html.="</table>";
return $html;
}
另外,正如@MagnusEriksson指出的那样,您每次都会将内容附加到$html
,因此您仍然可以在第二个文件中获得这两个结果。为避免这种情况,请将$html = "table";
放在foreach
循环中:
function generateTableFromResult($result) {
$x = 1;
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
foreach($row as $column => $value) {
copy("persons.php","newfile".$x.".php");
$html = "<table>";
$html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
$html.="</table>";
$myfile = fopen("newfile".$x.".php", "w") or die("Unable to open file!");
fwrite($myfile, $html);
}
$x++;
}
return $html;
}