我正在使用php並嘗試使用pdo連接到mysql。我讀過,我必須首先有pdo擴展名(不知道我是否做,如何檢查它)。我已經將phpmyadmin的config.inc.php文件更改爲大聲的外部連接。看起來是這樣的:通過php連接到mysql與pdo
<?php
/* Servers configuration */
$i = 0;
/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'localhost';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = '8000';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'http';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'http'; ////I've also try with config here
$cfg['Servers'][$i]['user'] = 'xxxx';
$cfg['Servers'][$i]['password'] = 'xxxxx';
$cfg['Servers'][$i]['AllowNoPasswordRoot'] = true;
?>
我的PHP文件是這樣的:
function connect() {
global $pdo;
$pdo = new PDO("mysql:host=localhost:8000;dbname=sakila", "xxxx", "xxxxxx");
}
function get_actors_by_last_name($letter){
global $pdo;
$stmt = $pdo->prepare('
SELECT actor_id, first_name, get_actors_by_last_name
from actor
where first_name like :letter
limit 50');
$stmt->execute( array(':letter' => $letter . '%'));
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
?>
的頁面是一個簡單的表格,是想帶給取決於他們的名字的第一個字母的演員姓名。每次嘗試使其在加載時都能夠使用頁面庫存,而不會給我帶來任何問題,而不是問題或例外。如果我改變主機參數,並把正在使用我的MySQL這樣的主機名值(在實際代碼的真實主機值):
$pdo = new PDO("mysql:host=mysql.value;dbname=sakila", "xxxx", "xxxxxx");
我得到這個消息:
致命錯誤:未捕獲在/Users/josecarro/Desktop/jquery/ajax/dataBase/functions.php:13堆棧跟蹤:消息'SQLSTATE [HY000] [1130]主機'xxxx'不允許連接到此MySQL服務器'時發生異常'PDOException' #0 /Users/josecarro/Desktop/jquery/ajax/dataBase/functions.php(13):PDO - > __ construct('mysql:host = MacB ...','xxxx','xxxx',NULL)#1 /Users/josecarro/Desktop/jquery/ajax/dataBase/index.php(7):connect()#2 {main}拋出/Users/josecarro/Desktop/jquery/ajax/dataBase/functions.php on這使我行13
認爲,實際上PDO擴展工作。 要打開服務器和使用上的目錄終端這條線在我的文件有:
open http://localhost:8000 && python -m SimpleHTTPServer
,並在同一目錄下,我開始用PHP
php -S localhost:8000
我不知道什麼問題。雖然我相信我有pdo我試圖用phpbrew安裝它(以防萬一),安裝另一個php版本(7.0.7)。但我得到的股票,當我嘗試用
phpbrew ext install pdo-mysql
安裝擴展其實我真的不知道我在做什麼是地獄的。我見過的PHP文件,你必須編譯像這樣:
--with-pdo-mysql[=DIR]
我不知道這是什麼意思......我很失落,因爲你可以看到。我認爲它可能比我想象的要容易,但我不知道該怎麼做,改變什麼。請幫助!!!
您正在使用的數據庫端口 –
混亂HTTP端口,你談論的PHP文件?在pdo線上?我必須卸下端口,只需將'host = localhost' ? – Chema