MySQL在PHPmyadmin上的终端中工作,但在php脚本中不工作?

时间:2018-08-31 14:21:32

标签: php mysql

标题几乎解释了这是我尝试过的,这是我的小测试脚本,用于检查mysql查询是否有效

$servername = "HIDDEN";
$username = "HIDDEN";
$password = "HIDDEN";
$dbname = "fbaFees";
/////////////////////////////////////

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected to database successfully<br><br>";

$sqlcheck = "SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC LIMIT 1 OFFSET 1;"; 

if ($conn->query($sqlcheck) === TRUE) {

    echo "New record created successfully<br>";
} else {
    echo "Error: " . $sqlcheck . "<br>" . $conn->error;
}

此脚本给我这个错误:

Error: SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC 
LIMIT 1;

这是在phpmyadmin上运行的mysql查询

Error: SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC 
LIMIT 1;

非常感谢您的帮助!

编辑: enter image description here

2 个答案:

答案 0 :(得分:2)

尝试使用以下命令更改代码:

$query = "SELECT * FROM fbafee_table WHERE ASIN = 'B07GC6DXFQ' ORDER BY ID DESC LIMIT 1 OFFSET 1;"; 
$result = $conn->query($query);
if ($result && $result->num_rows>0) {
    while($row = $result->fetch_assoc()) {
         // do stuff with your row data
         echo $row['COLUMN_NAME'];   
    }
}
else
{
    echo "Error: " . mysqli_error($conn) . "<br>" . $query;
}

SELECT查询从数据库中选择数据,因此,如果您检查结果是否严格等于TRUE,则结果不是。您需要循环结果。如果没有返回任何行,则使用mysqli_error($conn)显示错误。

答案 1 :(得分:0)

$ conn-> query方法在发现记录不是布尔TRUE时将返回结果集。如果未找到结果,则返回布尔值false。因此,您必须先稍微更改代码以检查不可用性,然后再检查可用性,如下所示:

if (!$conn->query($sqlcheck)) {

   echo "Error: " . $sqlcheck . "<br>" . $conn->error;

} else {

   echo "New record created successfully<br>";
}