0

I'm trying to implement these two functions in a separate file functions.php and call it in index.php

function is_field($column, $table, $requested) {

    $is_field_query = "SELECT ".$column." FROM ".$table." WHERE ".$column."='".$requested."'";
    $is_field_result = $mysqli->query($is_field_query);

    $is_true = $is_field_result->num_rows;

    $is_field_result->close();

    return $is_true;

}

function get_content($column, $table, $requested) {

    $get_content_query = "SELECT ".$column." FROM ".$table." WHERE ".$column."='".$requested."'";
    $get_content_result = $mysqli->query($get_content_query);

    $get_content_row = $get_content_result->fetch_array(MYSQLI_ASSOC);
    $get_content_content = $get_content_row["content"];

    $get_content_result->close();

    return $content;

}

I have tried it over and over again and I have no idea why it wont work. The first one is returning 1 for valid or 0 for invalid. The second retrieves the content from a specific cell in the MySQL table. Any help would be much appreciated.

4

2 に答える 2

0

関数内で使用し$mysqliていますが、MySQLi リソース自体を渡すことはありません。次のように関数を記述することを検討してください。

function is_field($mysqli, $column, $table, $requested) {

または、MySQLi リソースを受け取り、$this->mysqli関数内で参照するクラスを作成します。

また、次のようなコードも別の問題である可能性があります。

$is_field_result = $mysqli->query($is_field_query);

$is_true = $is_field_result->num_rows;

$is_field_resultかどうかを確認していませんfalse。したがって、オブジェクトではないものからプロパティをフェッチすることはできないため、次のステートメントで致命的なエラーが発生します。

if (($is_field_result = $mysqli->query($is_field_query)) === false) {
    die($mysqli->error);
}
$is_true = $is_field_result->num_rows;
于 2013-03-12T03:45:53.597 に答える
0

接続からの $mysqli の受け渡しを受け入れるために、関数に追加のフィールドを追加する必要があったことが、機能しなかった理由であることが判明しました。

function is_field($mysqli, $column, $table, $requested) {

$is_field_query = "SELECT * FROM $table WHERE $column='$requested'";
if (($is_field_result = $mysqli->query($is_field_query)) == false) {        
    die($mysqli->error);        
}   
$is_true = $is_field_result->num_rows;
$is_field_result->close();
return $is_true;

}

function get_content($mysqli, $column, $table, $requested) {

$get_content_query = "SELECT * FROM $table WHERE $column='$requested'";
if (($get_content_result = $mysqli->query($get_content_query)) == false) {
    die($mysqli->error);
}
$get_content_row = $get_content_result->fetch_array(MYSQLI_ASSOC);
$get_content = $get_content_row["content"];
$get_content_result->close();
return $get_content;

}
于 2013-03-12T05:55:10.580 に答える