【问题标题】:PHP: Uncaught Error Cannot use object of type mysqli_result as arrayPHP:未捕获的错误不能使用 mysqli_result 类型的对象作为数组
【发布时间】:2019-07-21 16:08:28
【问题描述】:

我有一个简单的部分,我在其中显示来自数据库的数据,

如果用户点击例如建筑并选择例如阿尔及利亚,我将显示[251, 211,712] 如果用户点击例如电源并选择例如。埃及 我正在显示[406, 228,559]

现在我想如果用户单击按钮All available industries 并选择例如。阿尔及利亚 我想在 SQL 中以这样的简单方式显示这个[251+203+130, 211,712+179,877+154,946]

SELECT sum(SumofNoOfProjects) as sum_projects, sum(SumofTotalBudgetValue) as sum_value FROM `meed` WHERE Countries = 'Algeria'

给我这个[611, 546535]

这是我的解决方案

HTML

<div id="interactive-layers">
    <div buttonid="43" class="video-btns">
        <span class="label">Construction</span></div>
    <div buttonid="44" class="video-btns">
        <span class="label">Power</span></div>
    <div buttonid="45" class="video-btns">
        <span class="label">Oil</span></div>
    <div buttonid="103" class="video-btns">
        <span class="label">All available industries</span>
    </div>
</div>

这里是js ajax

$("#interactive-layers").on("click", ".video-btns", function(){
    if( $(e.target).find("span.label").html()=="Confirm" ) {

        var selectedCountries = [];

        $('.video-btns .selected').each(function () {
            selectedCountries.push( $(this).parent().find("span.label").html() ) ;
        });

        if( selectedCountries.length>0 ) {
            if(selectedCountries.indexOf("All available countries")>-1) {
                selectedCountries = [];
            }


        } else {

            return;
        }

        var ajaxurl = "";
        if(selectedCountries.length>0) {
            ajaxurl = "data.php";
        } else {
            ajaxurl = "dataall.php";

        }

        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                    countries: selectedCountries.join(","),
                    sector: selectedSector
            },
            success: function(result){
                console.log(result);
                result = JSON.parse(result);
                $(".video-btns").each(function () {
                    var getBtn = $(this).attr('buttonid');
                    if (getBtn == 106) {
                        var totalProjects = $("<span class='totalprojects'>"+ result[0] + "</span>");
                        $(this).append(totalProjects)
                    }else if(getBtn ==107){
                        var resultBudget = result[1]
                        var totalBudgets = $("<span class='totalbudget'>"+ '&#36m' +" " + resultBudget +"</span>");
                        $(this).append( totalBudgets)
                    }
                });
                return;
              }
        });
    }
});

更新这里是更新的data.php

      <?php

$selectedSectorByUser = $_POST['sector'];
$countries = explode(",", $_POST['countries']);
echo '$countries';

 $conn = mysqli_connect("localhost", "root", "", "meedadb");
 if (mysqli_connect_errno())   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 $result = mysqli_query($conn, "SELECT * FROM meed");
 $data = array();

 $wynik = [];
$totalProjects = 0;
$totalBudget = 0;

 while ($row = mysqli_fetch_array($result))
 {
    if($row['Sector']==$selectedSectorByUser && in_array($row['Countries'],$countries ) ) {

     $totalProjects+= $row['SumofNoOfProjects'];
     $totalBudget+= $row['SumofTotalBudgetValue'];

    }elseif($selectedSectorByUser =="All available industries"){
      $result = mysqli_query($conn, 
      "SELECT sum(SumofNoOfProjects) as 'SumofNoOfProjects, sum(SumofTotalBudgetValue) as SumofTotalBudgetValue 
      FROM `meed` 
      WHERE Countries = '$countries'");

      while( $row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
         echo json_encode([ $row['SumofNoOfProjects,'], $row['SumofTotalBudgetValue '] ] );
         exit;
        }

      exit;
    }
 }

 echo json_encode([ $totalProjects, $totalBudget ] );
exit();
?>

现在当用户点击All available industries btn 并选择一个国家时,我收到以下错误

b>致命错误:未捕获的错误:无法使用 mysqli_result 类型的对象作为 C:\custom-xammp\htdocs\editor\data.php:23 中的数组

我需要改变什么才能得到我想要的?任何帮助或建议将不胜感激,

【问题讨论】:

    标签: javascript php jquery mysql ajax


    【解决方案1】:

    你应该选择一行(至少)

    if (mysqli_connect_errno())   {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    
    $result = mysqli_query($conn, 
        "SELECT sum(SumofNoOfProjects) as sum_projects, sum(SumofTotalBudgetValue) as sum_value 
        FROM `meed` 
        WHERE Countries = '$countries'");
    
    while( $row=mysqli_fetch_array($result,MYSQLI_ASSOC);) {
     echo json_encode([ $row['sum_projects'], $row['sum_value'] ] );
     exit;
    }
    

    针对多个国家

    假设你 $_POST['countries'] 包含 "'Egypt','Algerie'" 那么你可以使用查询作为

    "SELECT sum(SumofNoOfProjects) as sum_projects, sum(SumofTotalBudgetValue) as sum_value 
        FROM `meed` 
        WHERE Countries IN (" . $_POST['countries'] . ");"
    

    【讨论】:

    • 你确定你有一个有效的连接吗?? .. 我已经更新了 asnwer 并添加了一个与 db 连接的检查 .. 你确定你的查询返回一个有效的结果
    • asnwer 使用 while 更新 .. 如果您至少有一个有效行,则应该执行 while 循环.. 您确定您正在查看正确的 php 代码吗??
    • 此解决方案仅在我选择一个国家/地区时有效,但如果我选择多个国家/地区,我会得到 [0,0],我需要在 sql 语句上更改什么以便用户可以选择多个国家?
    • 您应该使用IN 子句而不是= 更改国家/地区的查询过滤器,并使用用圆括号括起来的正确逗号分隔的值列表..
    • @user9964622 答案已更新......有一个建议......希望很明确......如果你不介意你可以投票给我其他人的回答......
    猜你喜欢
    • 2017-11-12
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2023-04-03
    • 2018-07-07
    • 2018-08-25
    相关资源
    最近更新 更多