0

您好,我第一次使用 PDO 并出现错误……不,我错在哪里,这是我的代码

function student_one_image($student_id){
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $STH = $DBH->query("SELECT * from school_students, school_student_one_image where (school_student_one_image.student_id = school_students.student_id) 
    and school_student_one_image.student_id = '$student_id'");
    $STH->setFetchMode(PDO::FETCH_ASSOC);
    $row = $STH->fetch();
    return $row 

错误是 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock

如果我正在尝试使用 mysql 它可以工作

function student_one_image($student_id){
    $data= mysql_query("SELECT * from school_students, school_student_one_image where (school_student_one_image.student_id = school_students.student_id) 
    and school_student_one_image.student_id = '$student_id'");
    $result = mysql_fetch_array($data);
    return $result; 

请帮忙

4

1 回答 1

3

您的变量$host$dbname和超出范围 [1] 因为您在函数中使用它们。您可以像这样传递它们:$user$pass

function student_one_image($student_id, $host, $dbname, $user, $pass) {

或者通过将以下行添加到函数顶部来更改它们的范围:

global $host, $dbname, $user, $pass;

[1] http://php.net/manual/en/language.variables.scope.php

于 2012-05-21T18:37:40.857 回答