Log in

View Full Version : php查询MYSQL数据库里的记录并显示出来,怎么样能让速度快点?


West University Place
2015-05-21, 07:21 AM
我平时一直是用mysql_fetch_array($result)函数组织数据。发现数据量大的时候速度会慢下来,不知道大家都怎么查询和显示。

听说可以把数据全部查询出来后由JS去组织,这样能把一部分负担分给客户机,这方法可行吗?哪位有这方面的资料?


下面是我常用的显示方法:
$result=mysql_query('select id,username,address,phone from userlist order by id desc');
while ($rs=mysql_fetch_array($result)){
echo $rs["id"].",";
echo $rs["username"].",";
echo $rs["mydate"].";";
}

Napa
2015-05-21, 07:22 AM
<?php
//这种方法不错.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
$stmt->execute();

/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2);

/* fetch values */
while ($stmt->fetch()) {
printf("%s %s\n", $col1, $col2);
}

/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();

?>


mysql_fetch_array

它打印两种方式:一种是关联数组;一种是索引数组

如果只需要一结果的话,别用它