虽然我在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
这里的启动表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
发布于 2018-02-21 21:50:12
这将产生您想要的输出(来自您的评论):
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 (实际上没有意义)更改为一个简单的命令:内容()
向“ishegg”大喊大叫,这有助于缩小问题的范围
发布于 2018-02-21 21:13:43
默认情况下,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;
}https://stackoverflow.com/questions/48915444
复制相似问题