PHP select MySQL 没有按预期插入详细的错误消息

标签 php html mysql

我在尝试使用 php 脚本时遇到了问题。当我启动它时,脚本完美地连接到 MySQL 和数据库,但是当我尝试从“codes”数据库中选择数据时,它会给出如下错误:

Could not get data:

我怎样才能让它说出来

Could not get data: (then the error here)

相反?

PHP 代码:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

   $sql = 'SELECT code FROM codes';
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
      echo "Code:{$row['code']}";
   }

   echo "Fetched data successfully\n";

   mysql_close($conn);  
?>

最佳答案

你将 mysql 和 mysqli 混合在一起,所以我修复了错误:

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
$servername = "localhost";
$username = "root";
$password = "pass";
$dbname = "table";

// Create connection
$conn = mysqli_connect($servername, $username, $password);
        mysqli_select_db($conn, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";

   $sql = 'SELECT username FROM users';
   $retval = mysqli_query($conn ,$sql);

   if(! $retval ) {
      die('Could not get data: ' . mysqli_error());
   }

   while($row=mysqli_fetch_array($retval,MYSQLI_ASSOC)) {
      echo "Code:{$row['username']}";
   }

   echo "Fetched data successfully\n";

   mysqli_close($conn);  
?>

关于PHP select MySQL 没有按预期插入详细的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42359635/

相关文章:

php 图片显示问题

c++ - 在 MySQL 中运行 HTTP 服务器以接收来自客户端的数据

php - Laravel:在父表上与 *where* 而不是 *find* 的 Eloquent 关系

javascript - 使用 Bootstrap Modal 的 fullcalendar 打开事件

php - php 如何在变量中插入双引号

html - 使用 XSLT 2.0/3.0 使用多个步骤将 CDATA 中的纯文本解析为 html。部分路途

php - drupal 外露过滤器 "is empty (NULL)"下拉菜单不起作用

php - TCPDF 不同边的特定边框

javascript - 如何在 fillRect() 中添加可编辑文本

javascript - 检查输入是否为空,如果不为空,则将类添加到父div