php - PHP mysqli代码不起作用

标签 php mysql mysqli

我有这段代码可以在表格中显示网站上的两条记录。目前无法使用。

<?php
//make connection
mysqli_connect('localhost', 'root', 'password', 'databasename')or die('Connection could not be established With msql_connect');
$sql = "SELECT * FROM players ORDER BY bankacc DESC LIMIT 10";
$records = mysqli_query($sql) or die('Connection could not be established With mysqli_query');
?>


要创建表,我需要以下PHP代码:

  <?php
    while($players=mysqli_fetch_assoc ($records)){

    echo"<tr>";

    echo"<td>","<center>".$players['name'];"</center>";"<td>";
    echo "<td>","<center>","$",number_format($players['bankacc']);"</center>";"<td>";

    echo"</tr>";

}//end while

?>


当我尝试运行它时,它说:


  无法使用mysqli_query建立连接
  希望我的网页上有人帮助我。

最佳答案

您会感到困惑/不了解以程序方式或面向对象方式使用mysqli的两种方式。

如果看到代码,您会发现mysqli_connect没有存储在任何地方,因此mysqli_query也会引发错误,必须将两者链接在一起。

编辑:从您问题中的代码,您正在使用一种程序方法,因此,这里是用于修复您的错误的解决方案:

<?php
//make connection
$conn = mysqli_connect('localhost', 'root', 'password', 'databasename')or die('Connection could not be established With msql_connect');

$sql = "SELECT * FROM players ORDER BY bankacc DESC LIMIT 10";

$records = mysqli_query($conn, $sql) or die('A query error has occurred: ' . mysqli_error($conn));
?>

关于php - PHP mysqli代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45524566/

相关文章:

php - PHP 中的 MySQL 查询,条件为数组

java - java连接mysql数据库出错

php - 使用 php 和 mySqli 建表

php - 为什么我的 or die() 函数不起作用

php - 扩展 mysqli 以提高性能的正确方法是什么?

email - PHP 的 `mail` 是如何工作的?

php - .getJSON() 中的 jsoncallback 参数有什么用?

php - 数据库之间的 Codeigniter 切换

php - WHERE 子句中的 Doctrine 过滤器而不是 LEFT JOIN

php - 我无法使用 PHP 中的 select 语句从 MySQL 中获取多个值