donglizuo8892 2016-11-30 15:41
浏览 16
已采纳

共享主机上的空白MySQL错误

I am doing a little bit of learning on mysql, php and the like. I'm using a shared hosting plan so am quite limited from a settings changes point of view.

I am attempting to run a simple mysql select command through PHP, but all i get back is a blank error

<?php
$typeID = $_GET['tid'];
//variables for the database server
$server = "localhost";
$user = "codingma_rbstock";
$pwd = "M@nL%V{%RI+h";
$db = "codingma_rbstock";

//variables for the database fields
$itemNo;
$itemNm;
$itemDesc;
$buyPr;
$sellPr;
$quan;
$dept;

//database connection
//create connection
$conn = new mysqli($server, $user, $pwd, $db);

//if the connection fails throw an error.
if ($conn->connect_error){
    die("Connection Failed: " . $conn->connect_error);
}

echo "Welcome to " . $typeID . "<br>";

$sql = "select ITEM_NAME from stock where ITEM_NO='00001'";

if ($conn->query($sql) === TRUE){
    $res = $conn->query($sql);
        if ($res->num_rows > 0){
            echo "success";
        }

}else{
    echo "Error: " .$sql . "<br>" . $conn->error;
}

echo $res;

?>

I have checked and it seems to be connecting to the database fine (I changed a few account details to see if that threw a different error and it did).

I am sure I am missing something completely obvious here! The below is the text output from the error;

Error: select ITEM_NAME from stock where ITEM_NO='00001'

Thanks for any help.

  • 写回答

1条回答 默认 最新

  • dongmei6426 2016-11-30 16:03
    关注

    your problem is in this line

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

    you are doing a variable type check ( === ), the result of that comparision will always fail because, for as long as you have data in your table and your query doesn't fail $conn->query($sql) will not return a boolean value

    mysqli::query documentation says:

    Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
    

    You are using here a SELECT, therefore a successfull result won't be boolean

    Try switching to

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

    Or even better remove that if completely

    EDIT

    The better approach for that part of the code is:

    $res = $conn->query($sql);
            if ($res->num_rows > 0){
                echo "success";
            }
    
    if ($res === false) {
        echo "Error: " .$sql . "<br>" . $conn->error;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路