我正在尝试使用 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;
}