dongyi5425 2017-09-09 21:16
浏览 49
已采纳

使用方法:sql == true vs sql === true?

please, have a look here; and thanks all! I am trying to create a system for upgrading buildings in my game. I made my upgrade div same for all buildings. When player click the build (div) run a function to appear a hide div filled by ajax post with the costs of upgrading. The class 'name'(same with the building name from db) of the div is sent to a php to get the required data. It works fine untill now... I added a click event to a button that should send the div 'name' into another php which should do the upgrade operation and echo "correct" in response ajax field. But when i click the btn, it show no log error... Error: SELECT ress1,ress2 FROM cost_cladiri WHERE building = 'camp_1' (I ve inserted a name of building from db instead of the variable(div name) to access the php directly, but the same error... Here is my ajax and my php

    function getbuild(e) {  
  $.ajax({
  type: 'post',
  url: '../account-handeling/get_cost_upgrade.php',
  data: {
  building: e.className,
},
  success: function (response){
   $('#display_cost').html(response); 
$("#increase").click(function(){  
    $.ajax({
  type: 'post',
  url: '../account-handeling/upgrade_handle.php',
  data: {
  building: e.className,
}, 
  success: function (response){
   $('#display_cost').html(response);
  }
});
})  
       }
    });
  }

upgrade_handle.php

if(!isset($_SESSION))
{
session_start();
}
$dbserver           = "localhost";
$dbusername         = "root";
$dbpassword         = "";
$db                 = "worlddomination";

$conn = mysqli_connect($dbserver,$dbusername,$dbpassword,$db);
if ($conn->connect_error) 
{
    die("No connection:".$conn->connect_error);
}

$username = $_SESSION['username'];
//$buildcost = $_POST['building'];

$sql    = "SELECT ress1,ress2 FROM cost_cladiri WHERE building = 'camp_1'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$ress1 = $row['ress1'];
$ress2 = $row['ress2'];
if($conn->query($sql) === TRUE){
    echo "$ress1";
}
else{
    echo "Error: ".$sql."<br/>".$conn->error;
}

What i ve done wrong? Thanks!!!

  • 写回答

1条回答 默认 最新

  • doubi4435 2017-09-10 02:39
    关注

    For select query the Mysqli::query method will return mysqli_result object, so as it may be == true it wont be === true.

    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.

    I would try to check if it !== false or instanceof mysqli_result.

    Edit: also you query the same thing two times, once object oriented and once procedural.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题