php操作数据库

<html>
<head>
   <title>Finding User</title>
</head>
<body>
  <h2>Finding users from mysql database.</h2>
  <form action="formhandler.php" method="post">
     Fill user name:
     <input name="username" type="text" size="20"/> <br />
	 <input name="submit" type="submit" value="Find"/>
  </form>
</body>
</html>

具体操作步骤如下:

<html>
<head>
   <title>User found</title>
</head>
<body>
   <h2>User found from mysql database.</h2>
<?php
    $username = $_POST['username'];
	if(!$username){
	    echo "Error: There is no data passed.";
		exit;
	}
	
	if(!get_magic_quotes_gpc()){
		$username = addslashes($username);
	}
		@ $db = mysqli_connect('localhost','root','12345678','testphp');
	
		if(mysqli_connect_errno()){
	 	 echo "Error: Could not connect to mysql database.";
	 	 exit;
		}
		
		$q = "SELECT * FROM user WHERE name = '".$username."'";

		$result = mysqli_query($db,$q);
		$rownum = mysqli_num_rows($result);
		
		for($i=0; $i<$rownum; $i++){
		   $row = mysqli_fetch_assoc($result);
		   echo "Id:".$row['id']."<br />";
		   echo "Name:".$row['name']."<br />";
		   echo "Age:".$row['age']."<br />";
		   echo "Gender:".$row['gender']."<br />";
		   echo "Info:".$row['info']."<br />";
		}
		mysqli_free_result($result);

		mysqli_close($db);
	
?>
</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/DamonBlog/p/10121063.html