简体   繁体   English

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

[英]MySQL works in terminal on PHPmyadmin but does not work in php script?

Title pretty much explains it here is what I have tried this is my little test script to check if the mysql query works 标题几乎解释了这是我尝试过的内容,这是我的小测试脚本,用于检查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;
}

This script gives me this error: 这个脚本给了我这个错误:

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

This is the mysql query that works on phpmyadmin 这是在phpmyadmin上运行的mysql查询

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

Any help is greatly appreciated! 任何帮助是极大的赞赏!

edit: 编辑: 在此处输入图片说明

Try changing the code with the following: 尝试使用以下命令更改代码:

$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;
}

The SELECT query selects data from the database, so if you check if the result is strictly equal to TRUE , the result is not. SELECT查询从数据库中选择数据,因此,如果您检查结果是否严格等于TRUE ,则结果不是。 You need to cycle the results. 您需要循环结果。 If there's no rows returned, then you display the error using mysqli_error($conn) . 如果没有返回任何行,则使用mysqli_error($conn)显示错误。

Well the $conn->query method will return a result set when it finds are record not the boolean TRUE. 那么$ conn-> query方法将在发现记录不是布尔TRUE时返回一个结果集。 It returns the boolean false if there is no result found. 如果未找到结果,则返回布尔值false。 So you have to slightly change your code first to check for unavailability and then the availability as follows 因此,您必须先稍微更改代码以检查不可用性,然后再检查可用性,如下所示

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

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

} else {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM