使用php在mysql中检索单行数据


retrieve single row data in mysql using php

我创建了一个网站,该网站有多个登录名和唯一信息。我想从一个用户那里检索数据。例如,我的用户名是qwert,密码是1234,我想将他的唯一信息检索到数据库中。我在w3schools中使用了示例代码,它选择了所有数据,但我只想从只登录的用户那里检索数据

有人能帮我吗?任何帮助都将不胜感激。

mysql_select_db("xone_login", $con);
$result = mysql_query("SELECT * FROM admin WHERE username = '$myusername' ");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['overtime'] . "</td>";
  echo "<td>" . $row['daily_rate'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

将该教程中SQL中的代码替换为以下代码(并调整表和列名):

SELECT * FROM USERS where name ='qwert' and pass = MD5('1234')

为了避免SQL注入攻击,请注意清理您的变量!

You need to use a where clause
Also you will need to specify limits on the query to restrict the result set to 1 record
$select = "SELECT * FROM usertable WHERE username = '$user' LIMIT 0, 1";
$query = mysql_query($select) or die(mysql_error());
$result = mysql_fetch_assoc($query);
//Prints the array
print_r($result);