Home >Backend Development >PHP Tutorial >php example-php reads specific rows from the database (example)
The following editor will bring you an article about php reading specific rows (examples) from the database. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
Sometimes we need to read specific data from the database to verify the user’s input. At this time, the sql statement that needs to be executed is:
select * from table_name where id='num';
Need to execute such a statement.
So how to execute such a statement.
In order to execute sql statements, we need to connect to the database
<?PHP $servername='localhost'; $username="root"; $password="1234"; $dbname="mydb1"; //创建链接 $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error) { die("链接失败".$connect_error); } //定义语句 $sql="select * from users where username='wangdenggao'"; //执行语句 $result=$conn->query($sql); if($result->num_rows>0)//这个检验是必须的,而且我们要知道的是$result也就是执行sql语句的返回的值是一个数组 这个数组看做一个对象,我们可以用箭头得到他的一些的属性,而且fetch_assoc()是获取数据数组中的每一行 { while ($row=$result->fetch_assoc()) { echo "<br>Username".$row['username']."<br>Password:".$row['password']; } } ?>
The above is the detailed content of php example-php reads specific rows from the database (example). For more information, please follow other related articles on the PHP Chinese website!