在mysql中使用php检索单行数据
问题描述:
我创建了一个网站,它有多个登录信息和唯一信息..我想从一个用户检索数据。例如我的用户名是qwert,我的密码是1234,我想检索他的唯一信息到数据库。我使用w3schools中的示例代码,它选择所有的数据,但我想要做的只是从用户那里只检索数据。在mysql中使用php检索单行数据
任何人都可以帮助我吗?任何帮助都感激不尽。
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注入攻击!
+1
我认为,还应该提及一些关于SQL注入的内容。 – Lion 2012-07-13 19:56:43
答
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);
+1
这有零SQL转义,并作为一个不行的例子。 – tadman 2012-07-13 20:22:05
你可以在这里添加你的代码,以便我们可以看到需要做什么? – andrewsi 2012-07-13 19:48:41
您需要一个'WHERE'子句。 'SELECT * FROM yourtable WHERE username ='the_logged_in_username'' – 2012-07-13 19:49:50
欢迎来到*。没有任何代码示例,我们无法知道你想要做什么。 – Lion 2012-07-13 19:50:52