我有一个聚合物(1.0)应用程序我正在尝试创建(基于拉伸教程联赛表),我无法弄清楚路由如果使用mvc,所以我选择了单页网站,引用不同的PHP文件。
我在MYSql中有一个表,我试图使用一个简单的echo表脚本合并到表中,但这会重复数百次。我怎么能阻止这个循环?
$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 />';
}
}
我添加了&#39; &LT; 50&#39;但是这只返回50个表头行?
网站现在是http://jay-o.uk/#!/results css,其他数据现在还可以,只是这个讨厌的循环。
答案 0 :(得分:0)
你需要设置一个&#34; LIMIT&#34;在第一个查询中,如果你想避免超时操作,我认为可以调用一个唯一的查询,返回你需要的所有信息,但我不知道,因为你的查询是无法理解的。
您的代码错误,第二个查询不使用null值的$ 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>';
}
答案 1 :(得分:0)
对不起,代码有点凌乱,我一直在研究这个问题,如果我感到沮丧,我不知道我哪里出错,如果我删除$ 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中的视图而不是表格吗?
答案 2 :(得分:0)
嗯..也许你需要这个代码:
// 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是一个空数组,可能问题是查询没有返回任何行。
答案 3 :(得分:0)
关于一个更简单的解决方案,将数据库中的查询限制为1:
$results = mysql_query('SELECT * FROM table_name ORDER BY field_name LIMIT 1');