2012-05-21 84 views
0

你好,我使用PDO第1次和得到錯誤上午......不不到哪我錯了,這裏是我的代碼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 

錯誤是無法通過套接字連接到本地MySQL服務器' /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; 

請幫助

+0

你有沒有輸出你的'$ host','$ dbname','$ user'和'$ pass'變量的值是否在函數內?這可能是他們是空的 – Laimoncijus

+0

是的,我已經嘗試了兩種方式....直接也(用戶名和通行證),但仍然無法正常工作 – Harinder

回答

3

您的變量$host,$dbname,$user$pass由於在函數中使用它們而超出範圍[1]。 您可以通過他們這樣的:

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