如何使用php/sql将输入与mysql数据进行比较? [英] How do I compare input to mysql data with php/sql?
问题描述
我刚接触PHP和mySQL DB.我正在尝试在其中输入用户名(UN)来输入网站的这一部分.我有一个mySQL DB(test),带有一个名为"test"的用户表. 我知道我正在连接ok,因为我通过创建一个简单的页面来打开数据库并列出所有用户(在UN字段中)或选择一个特定用户的简单页面进行了测试.然后,我创建了一个名为"input.php"的页面来测试获取输入.如此处所示>
<html>
<body>
<form action="test.php" method="get">
UN: <input type="text" name="U">
<input type="submit">
</form>
</body>
</html>
从上面输入的内容将转到下面的"test.php",其中将使用我的数据库中的当前数据对其进行检查.
<?php
$hostname = "test.db.some#.somehost.com";
$username = "test";
$dbname = "test";
$password = "password";
$usertable = "test";
$yourfield = "UN";
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
$query = "SELECT * FROM $usertable WHERE $yourfield = $_GET["U"]";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield"];
echo "Hello: $name<br>";
}
}
else {
echo "User dosen't exit!";
}
mysql_close();
?>
这是我得到的错误> *解析错误:语法错误,意外的",期望在第20行的/home/content/81/11107981/html/test.php中出现T_STRING或T_VARIABLE或T_NUM_STRING * >
我知道我要走了,但我要雪茄. ;)
mysql_ *已弃用.您应该避免使用它们.
更改
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
到
$connection = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
$query = "SELECT * FROM $usertable WHERE $yourfield = $_GET["U"]";
到
$query = "SELECT * FROM $usertable WHERE $yourfield = '".$_GET["U"]."'";
和
mysql_close();
到
mysql_close($connection);
I'm new to working with PHP and a mySQL DB. I"m trying to make a user input there user name(UN) to enter this one part of my site. I have a mySQL DB(test) with a users table called "test". I know that I'm connecting ok, because I tested it by creating a simple page to open the DB and list all the users(from the UN field), or select a specific one. I then created a page called "input.php" for a test of getting input. As seen here>
<html>
<body>
<form action="test.php" method="get">
UN: <input type="text" name="U">
<input type="submit">
</form>
</body>
</html>
The input from above goes to "test.php" below where it is checked with current data in my DB.
<?php
$hostname = "test.db.some#.somehost.com";
$username = "test";
$dbname = "test";
$password = "password";
$usertable = "test";
$yourfield = "UN";
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
$query = "SELECT * FROM $usertable WHERE $yourfield = $_GET["U"]";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield"];
echo "Hello: $name<br>";
}
}
else {
echo "User dosen't exit!";
}
mysql_close();
?>
And this is the error I get> *Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/content/81/11107981/html/test.php on line 20*
I know I'm close, but I want the cigar. ;)
mysql_* are deprecated. You should avoid them.
Change
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
to
$connection = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
,
$query = "SELECT * FROM $usertable WHERE $yourfield = $_GET["U"]";
to
$query = "SELECT * FROM $usertable WHERE $yourfield = '".$_GET["U"]."'";
And
mysql_close();
to
mysql_close($connection);
这篇关于如何使用php/sql将输入与mysql数据进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!