我有一個名爲$ addressdb的數據庫。我想通過用戶輸入的結果($ usersName)搜索該數據庫上的表。我的錯誤可能真的很愚蠢。我是mySQL的新手。使用PHP變量來搜索SQL數據庫
<?php
//IF THE LOGIN is submitted...
if ($_POST['Login']){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "addressdb";
$usersName = $_POST['users'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT userID, userName FROM users WHERE userName =$usersName";
$result = mysqli_query($conn, $sql);
...
我的錯誤的行
$sql = "SELECT userID, userName FROM users WHERE userName =$usersName";
更具體的變量調用。
用單引號括住'$ userName':'userName ='$ usersName'' –
我總是喜歡使用'WHERE userName ='「。$ username。」'' – Peter
謝謝!有效! – LifeofBob