我正在尝试通过mysqli 在同一页面上输入和显示数据,但它显示错误" 无法在线获取mysqli "并且无法显示记录。
<?php
$db=mysqli_connect("localhost","root","","abc") or die("Not connected".mysqli_error());
$database=mysqli_select_db($db,'abc') or die("Database not found".mysqli_error());
if(isset($_POST['submit'])){
$roll=$_POST['roll'];
$name=$_POST['name'];
$ins=mysqli_query($db,"insert into abc1 (roll,name)values('$roll','$name')");
}
mysqli_close($db);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post">
Roll No <input type="text" name="roll" />
Name <input type="text" name="name" />
<input type="submit" name="submit" value="Submit" />
</form>
<table><tr>
<td>Roll No.</td>
<td>Name</td>
</tr>
<?php $query=mysqli_query($db,"select * from abc1");
$result=mysqli_query($db,$query);
$id=0;
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<tr><td><?php echo $row['roll']; ?></td>
<td><?php echo $row['name']; ?></td>
<?php } ?>
</tr></table>
</body>
</html>
任何帮助将不胜感激。 在此先感谢
答案 0 :(得分:1)
您正在调用mysqli_query函数两次:
$query = mysqli_query($db,"select * from abc1");
$result = mysqli_query($db,$query);
错误:无法在线获取mysqli
意思是,您在mysqli_fetch_array函数中传递资源ID而不是Query Statement。所以你只需要将其用作:
$query = "select * from abc1";
$result = mysqli_query($db,$query);
旁注:
无需再次连接数据库:
$database=mysqli_select_db($db,'abc')
mysqli_connect()
已经连接了您的数据库。
答案 1 :(得分:1)
你正在做两次mysqli_query。 试试这个。
$query = "select * from abc1";
$result = mysqli_query($db, $query);