php
  • mysql
  • mysqli
  • 2017-08-11 4 views -2 likes 
    -2

    私はいくつかのデータベーステーブルを持っています。それらはすべてデータでいっぱいです。私は、データを取得しようとする次のPHPコードがあります:PHP mysqli_num_rowsはデータベース内のデータでも常に0を返します

    $sql = "select inspection_date, inspection_type, capacity, criticality, man_hours from $table_name where device_number = '$device_number' order by inspection_date"; 
    
    //printf("sql = $sql <br/>"); 
    
    $result = mysqli_query($conn, $sql); 
    
    if (mysqli_num_rows($result) > 0) { 
        while($row = mysqli_fetch_assoc($result)) { 
         $final_array[] = array("device_number"=>$all_ld_formatted[$i]['device_number'], "old_device_number"=>$all_ld_formatted[$i]['old_device_number'], 
          "type"=>$all_ld_formatted[$i]['type'], "building"=>$all_ld_formatted[$i]['building'], "room"=>$all_ld_formatted[$i]['room'], 
          "power"=>$all_ld_formatted[$i]['power'], "inspection_date"=>$row['inspection_date'], "inspection_type"=>$row['inspection_type'], 
          "capacity"=>$row['capacity'], "criticality"=>$row['criticality'], "man_hours"=>$row['man_hours']); 
        } 
    } else { 
        printf("no results <p/>"); 
    } 
    

    上記のコードはfor-loopで実行されています。だからこそ、そこに$ all_ld_formatted [$ i] ['value']のものがあるのが分かります。何らかの理由で、コードは常に「結果なし」を返します。しかし、私がprintf( "sql = $ sql")のコメントを外すと、 PHPMyAdminにクエリをコピーアンドペーストすると、いつも私が探している結果が得られます。助けてくれてありがとう!

    よろしく、

    クリス・マルティーノ

    +0

    ** WARNING **:あなたが使用する必要がありますmysqli' '使用する場合は、[パラメータ化クエリ](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)と['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)を使ってクエリにユーザーデータを追加します。 **重大な[SQLインジェクションのバグ](http://bobby-tables.com/)を作成したため、文字列の補間または連結を使用してこれを実行しないでください。 ** '$ _POST'、' $ _GET'、**任意の**ユーザデータを直接クエリーに入れないでください。誰かがあなたのミスを悪用しようとすると非常に危険です。 – tadman

    +0

    うん、 '$ table_name'って何? '$ table_name'に' device_number = $ device_number'という行がありますか? '$ conn'は有効ですか?他の何か? – chiliNUT

    +0

    異なる顧客データを異なるテーブルに格納していないことを本当に願っています。それはあなたが維持できない怪物を作り出す方法です。 – tadman

    答えて

    0

    は、私はそれを考え出しました。クエリが実行される前に、データベースへの接続を閉じていました。みんなの時間を無駄にして申し訳ありません!

    クリス

    関連する問題