2015-11-09 71 views
1

我有這個查詢,在phpmyadmin工作,但不工作在PHP。任何替代方案?如何使用PHP變量爲MYSQL準備查詢字符串?

$servername = "localhost"; 
$username = "user"; 
$password = "password"; 
$dbname = "dbname"; 
$conn = new mysqli($servername, $username, $password, $dbname); 
if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);} 

$champname = 'name'; 

$sql = "SELECT column1, column2, column3, column4, column5 
FROM champions 
WHERE champion = '$champname' ORDER BY ID DESC LIMIT 1"; 

$result = $conn->query($sql); 
$row = $result->fetch_assoc(); 

任何幫助將高度讚賞

我猜'$champname'是這裏的問題。

+1

爲什麼你會說它不能在PHP中工作? PHP只是執行查詢。如果你有某種錯誤,你需要顯示它。確保在調試時報告所有錯誤。 – Andrius

+0

是否有錯誤日誌?如果是的話,請編輯你的問題 – jogo

+0

什麼是錯誤? –

回答

1

試試這個

$sql = "SELECT column1, column2, column3, column4, column5 
FROM champions 
WHERE champion = $champname ORDER BY ID DESC LIMIT 1"; 

$sql = "SELECT column1, column2, column3, column4, column5 
FROM champions 
WHERE champion = `$champname` ORDER BY ID DESC LIMIT 1"; 
0

$ SQL = 「選擇列1,列2,欄3,column4,column5 FROM冠軍 WHERE冠軍=「」。$ champname。「'ORDER BY ID DESC LIMIT 1」;

相關問題