我尝试使用php脚本遇到了问题。因此,当我启动它时,脚本完美地连接到MySQL和数据库,但是当我尝试从'代码中选择数据时数据库然后它给我一个这样的错误:"无法获取数据:"。它应该做的是说"无法得到数据:(然后是错误)",但它没有。提前致谢。 :)
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);
?>
答案 0 :(得分:2)
你把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);
?>