-3

私はphpとMySQLに少し慣れていません。チュートリアルを進めていて、以下の関数を呼び出すと上記のメッセージが表示されます。

<?php
function get_subject_by_id($subject_id){
    global $connection;
    $query = "SELECT * FROM subjects WHERE id=" . $subject_id . "LIMIT 1";
    $result_set = mysql_query($query, $connection);
        confirm_query($result_set);
    //if no rowes are returned, fetch array will return false
    if ($subject = mysql_fetch_array($result_set)) {
        return $subject;
    } else { 
        return NULL;
    }
}
?>
4

2 に答える 2

7

$query = "SELECT * FROM subjects WHERE id=" . $subject_id . "LIMIT 1";

する必要があります

$query = "SELECT * FROM subjects WHERE id=" . $subject_id . " LIMIT 1";

の前のスペースに注意してくださいLIMIT 1

しかし、あなたが特定していないので、私たちはまだあなたの問題が何であるかを正確には知りません.

于 2012-10-29T21:31:06.433 に答える
0

これが同じコードのPDOバージョンであり、PDOに関するいくつかの良い読み物です。

接続を設定すると、次のようなPDOのはるかに安全なクエリ関数を使用できます。

function get_subject_by_id($subject_id){
    $host = 'localhost';
    $dbname = 'name_of_database';
    $user = 'user_name';
    $pass = 'User_password';
    try{
        $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
    }catch(PDOException $e){  
        echo $e->getMessage();  
    }
    $query = "SELECT * FROM subjects WHERE id=? LIMIT 1";
    $result_set = $DB->prepare($query);
    //Include the variables to pass with the query
    $result_set->execute(array($subject_id));
    //You should modify this function to use PDO as well
    confirm_query($result_set);
    //if no rowes are returned, fetch array will return false
    if ($result_set->rowCount()) {
        return $subject;
    } else { 
        return NULL;
    }
}

そして、ここにいくつかの読書があります:

http://www.php.net/manual/en/book.pdo.php

于 2012-10-29T21:42:57.310 に答える