Heim  >  Artikel  >  Backend-Entwicklung  >  PHP输出整张表的内容

PHP输出整张表的内容

WBOY
WBOYOriginal
2016-06-06 20:33:111187Durchsuche

学了这么久 居然不知道怎么做 ~~

回复内容:

学了这么久 居然不知道怎么做 ~~

$result = mysql_query($sql);
while( $rows = mysql_fetch_array($result))
{
print_r($rows);
}

很简单 只需要开启PHP的mysqli扩展 仿照如下代码写出来就行

<code><?php $con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// 执行SQL语句
result = $conn -> query( 'select * from data_base' );
$row = $result -> fetch_row(); //取一行数据
echo row[0]; //输出第一个字段的值
// 或者这样
while($row=mysql_fetch_array($result)) { 
     //对每行记录进行运算 处理,如 :echo $row['name']."<br>"; 
}

mysql_close($con);
</code>

http://php.net/mysqli
mysqli::query 执行 SELECT 成功后返回 mysqli_result, 然后调用 mysqli_result::fetch_all 就可以获得一个完整的结果集数组. mysqli_result::fetch_all 的参数默认为 MYSQLI_NUM , 其他参数 MYSQLI_ASSOC, MYSQLI_BOTH.

<code><?php $mysqli = @new mysqli($db_host, $db_username, $db_password, $db_name, $db_port);
if ($mysqli->connect_errno) exit($mysqli->connect_error);
$mysqli->set_charset('utf8');
$result = $mysqli->query('SELECT * FROM table');
print_r($result->fetch_all());
$result->close();
$mysqli->close();
</code>

楼上说得方法都对,但都忘记了一点很重要的东西,
就是这整张表如果很大的话, 这么干肯定不行.
楼主可以封装一个类, 分批取出数据 然后置入缓存,
缓存慢了就马上输出到客户端.
这样在性能和用户体验上才更好.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn