我要打印输入到文本框中的ID的名称和姓氏。下面是PHP和HTML代码:
<head>
<title>
Search your name by ID
</title>
</head>
<?php
if(isset($_POST["searchname"]))
{
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
$a = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($a);
echo "$row[0] , $row[1] , $row[2]";
}
else
{
echo "error";
}
?>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
输入ID时输出:
, ,
MySQL表中有条目,但我无法获取它们。我的密码怎么了?
更新:,我也尝试过mysql_fetch_array
,但它不起作用。
发布于 2015-09-21 11:31:00
主要问题是您是miximg mysqli
和mysql
。这些是完全不同的API。假设你有
$id = $_POST["searchname"];
$connect = new mysqli("localhost","adarsh","Yeah!","adarsh");
接下来你应该:
$result = $connect->query("Select * from users where id='$id'");
然后得到结果:
while ($row = $result->fetch_assoc()) {
var_dump($row);
}
当然,与其直接将值放入查询中,不如使用准备语句。
更新:关于错误:
mysql
(这是不推荐的,您的不能再使用它了,)时,您不能使用任何mysqli
函数,反之亦然。new
创建mysqli对象时,您应该以面向对象的方式工作,即从mysqli对象调用方法。发布于 2015-09-21 12:08:38
试试这个:
<html>
<head>
<title>
Search your name by ID
</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="text" maxlength="6" name="searchname">
<input type="Submit" name="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST["searchname"])){
$id = $_POST["searchname"];
$connect = mysql_connect("localhost","adarsh","Yeah!","adarsh");
$result = mysql_query("Select * from users where id='$id'",$connect);
$row = mysql_fetch_assoc($result);
print_R($row);
}else{
echo "there is something wrong";
}
https://stackoverflow.com/questions/32693890
复制相似问题