【发布时间】:2013-03-17 22:57:13
【问题描述】:
我正在尝试使用 php while 循环在 html 表中显示 db.table 中的 25 行数据以循环 25 行。目前我没有 25 行限制器,只是想在此刻显示数据。这是我所拥有的。
<table>
<td><strong>User Name</strong></td>
<td><strong>User Email</strong></td>
<td><strong>Is User an Admin</strong></td>
<td><strong>Is User Active</strong></td>
<?php
$sql = 'SELECT name, login, is_admin, active
FROM db.users';
$result = db_exec_prepared_stmt($sql);
while($rows = mysql_fetch_assoc($result)) {
$user_name = $rows['name'];
$user_email = $rows['login'];
$user_admin = $rows['is_admin'];
$user_active = $rows['active'];
echo '<tr>
<td>' . $user_name . '</td>
<td>' . $user_email . '</td>
<td>' . $user_admin . '</td>
<td>' . $user_active . '</td>
</tr>';
}
?>
</table>
我知道 mysql_fetch_assoc() 在这里不起作用,但我需要帮助才能获得正常运行的代码。
这里是 db_exec_prepared_stmt() 函数。
function db_exec_prepared_stmt($sql, $params=array(), $query_type='select') {
$types = '';
foreach($params as $p) {
if( is_numeric($p)
&& ($p <= 2147483647)
&& (numberOfDecimals($p) === 0)
) $types .= 'i';
else $types .= 's';
}
$db = db_open_connection();
if($stmt = $db->prepare($sql)) {
if($types != '') {
$binds = array_merge( array($types), $params );
call_user_func_array( array($stmt, 'bind_param'), makeValuesReferenced($binds) );
}
switch($query_type) {
case 'select':
$results = db_fetch_assoc($stmt);
break;
case 'insert':
$stmt->execute();
$results = $db->insert_id;
break;
default:
$stmt->execute();
$results = null;
break;
}
if('' != $stmt->error) printf("Error %s: %s.\n", $stmt->errno, $stmt->error);
}
else {
printf("Error %s: %s.\n", $db->errno, $db->error);
$results = null;
}
db_close_connection($db);
return $results;
}
【问题讨论】:
-
这看起来像是一道作业题。另外你用什么来连接你的数据库? mysql_connect_db?
-
新 MySQLi();您在下面的回答帮助我弄清楚了。连接和函数 db_exec_prepared_stmt() 都是可以正常运行的遗留代码。这是为了我的工作。开发中的培训。
-
热心,记住
echo"<pre>";var_dump($i);是你的朋友,尤其是当你从 SELECT 查询中得到意想不到的结果时。
标签: php mysql while-loop html-table