[英]How can i stop a php while loop repeating over and over?
我有一個正在嘗試創建的聚合物(1.0)應用程序(基於拉伸教程排行榜),如果使用 mvc,我無法弄清楚路由,所以我選擇了單頁站點,引用不同的 php 文件。
我在 MYSql 中有一個表,我嘗試使用簡單的回顯表腳本將其合並到表中,但這會重復數百次。 我怎樣才能停止這個循環?
$result = mysql_query('select * from results',$connection) or die('cannot show tables');
while($tableName = mysql_fetch_row($result)) {
$sql = ('SELECT `home_team_name`,`away_team_name`,`home_goals_for`, `away_goals_for`, DATE_FORMAT(`fixture_date`, "%e %M %Y"), TIME_FORMAT(`fixture_time`, "%h:%i %p") FROM `results` ORDER BY fixture_date desc LIMIT 20 '.$table) or die('cannot show columns from '.$table);
echo '<h3>',$table,'</h3>';
$result2 = mysql_query($sql);
if(mysql_num_rows($result2)) {
echo '<table cellpadding="4" cellspacing="0" class="table table-striped table-hover table-bordered">';
echo '<tr class="info"><th>Home team</th><th>Away Team</th><th>Score</th><th>Score</th><th>Date<th>Time</th></tr>';
while($row2 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
}
我添加了“<50”,但這僅返回 50 個表標題行?
該站點位於http://jay-o.uk/#!/results 上,css 和其他數據目前還可以,只是這個討厭的循環。
如果你想避免超時操作,你需要在第一個查詢中設置一個“LIMIT”,我也認為可以調用一個唯一的查詢來返回你需要的所有信息,但我不知道,因為你的查詢是無法理解。
您的代碼是錯誤的,第二個查詢不使用為空值的 $table 變量,將它放在 limit 參數之后的目的是什么?
$results = mysql_query('SELECT * FROM table_name ORDER BY field_name LIMIT 50');
if(mysql_num_rows($results)) {
print '<table>';
while ($row = mysql_fetch_row($results)) {
print '<tr>';
foreach ($row as $field_name => $field_value) print '<td>'.$field_value.'</td>';
print '</tr>'
}
print '</table>';
}
抱歉,代碼有點亂,我已經研究了一段時間,如果令人沮喪,我不確定我哪里出錯了,如果我刪除 $tableName 變量,我會得到一個空數組,無論我做什么,這是結果。
我在 json 中嘗試過無濟於事...
$db = mysql_connect($host,$user,$pass);
if (!$db) {
die('Could not connect to db: ' . mysql_error());
}
//Select the Database
mysql_select_db($db_name,$db);
$resulting = mysql_query("select * from results", $db);
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($resulting, MYSQL_ASSOC)) {
$row_array['home_team_name'] = $row['home_team_name'];
$row_array['away_team_name'] = $row['away_team_name'];
$row_array['home_goals_for'] = $row['home_goals_for'];
$row_array['away_goals_for'] = $row['away_goals_for'];
//push the values in the array
array_push($json_response,$row_array);
$json = json_encode($json_response);
//echo $json;
$json_result = print_r((array) json_decode($json,true));
echo $json_result;
}
剩下這個:jay-o.uk/config/config.php
這可能是因為我指的是 mySql 中的視圖而不是表嗎?
好吧..也許你需要這個代碼:
// Create an array
$items = array();
while ($row = mysql_fetch_array($resulting, MYSQL_ASSOC)) {
/* print new insert */
var_dump('new item');
/* add a new item */
$items[] = array(
'home_team_name' => $row['home_team_name'],
'away_team_name' => $row['away_team_name'],
'home_goals_for' => $row['home_goals_for'],
'away_goals_for' => $row['away_goals_for']
);
}
$json_response = json_encode($items);
print $json_response;
如果 $json_response 是一個空數組,問題可能在於查詢不返回任何行。
一個更簡單的解決方案如何,只需將來自數據庫的查詢限制為 1:
$results = mysql_query('SELECT * FROM table_name ORDER BY field_name LIMIT 1');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.