2013-12-16 27 views
-4

我有一個名爲testdb的mysql數據庫,我試圖將我的mysql查詢遷移到mysqli查詢。例如:不能使用mysql數據庫的mysqli(互操作性)

$con=mysqli_connect('localhost','root','testpass','testdb'); 

// Check connection 
if (mysqli_connect_errno($con)) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$sql="insert into testtable(description)values("hello")"; 
mysqli_query($con,$sql); 

testtable採用的是MySQL創建,我能夠通過MySQL的查詢訪問它,但是當我試圖通過mysqli的查詢訪問它,我得到一個錯誤,指出:

testdb.testtable not found 

關於可能出錯的任何想法?


編輯

好,我同意,計算器的熒光筆強調語法error.But爲什麼我會得到一個「表‘testdb.testtable’不存在」的時候,語法錯誤是否錯誤?我重申了我確實改變了我的代碼,正如高亮顯示的那樣,但仍然面臨着同樣的問題。我想我的問題還沒有被完全讀取。只有通過stackoverflow突出顯示的語法錯誤纔會被考慮並被破壞我犯了這個錯誤。但朋友們,請把我的問題完整地讀出一次。我想即使是主持人也讓我最擔心的是話題。

+4

StackOverflow上的語法高亮顯示您你做錯了什麼。請仔細閱讀。 –

+0

如果您使用的是MySQLi,那麼開始學習準備好的語句/綁定變量;那麼引用不好的字符串將成爲非問題 –

+0

請在下面檢查我的答案。 – dev1234

回答

0
$con = mysqli_connect('localhost','root','testpass','testdb'); 

// Check connection 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$sql = "INSERT INTO testtable(description) values ('hello')"; 

mysqli_query($con,$sql); 
0

應該是這樣的,Mysqli跟着,OOP,而mysql跟着程序。

檢查這裏的更多信息,編寫查詢與庫MySQLi:http://www.php.net/manual/en/mysqli.quickstart.statements.php

$db = new mysqli('localhost','root','testpass','testdb'); 

if($db->connect_errno > 0){ 
    die('Unable to connect to database [' . $db->connect_error . ']'); 
} 

$sql = "INSERT INTO testtable(description) values ('hello')"; 

if(!$result = $db->query($sql)){ 
    die('There was an error running the query [' . $db->error . ']'); 
} 
+0

感謝mazraara.But我猜mysqli是程序和麪向對象。 – Ravikanth

+0

是的,但它最好總是遵循最佳實踐。使用mysqli的OOP方式。如果我的答案幫助您解決問題,請接受答案。 – dev1234

+0

我試過mazraara.But但並沒有解決我的問題:( – Ravikanth

0

你只需要改變你的查詢

$sql="insert into testtable(description)values("hello")";

$sql="insert into testtable(description)values('hello')";