Home > Backend Development > PHP Tutorial > php example-php reads specific rows from the database (example)

php example-php reads specific rows from the database (example)

微波
Release: 2023-03-11 18:38:02
Original
1040 people have browsed it

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=&#39;localhost&#39;;
$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=&#39;wangdenggao&#39;";
//执行语句
$result=$conn->query($sql);
if($result->num_rows>0)//这个检验是必须的,而且我们要知道的是$result也就是执行sql语句的返回的值是一个数组
这个数组看做一个对象,我们可以用箭头得到他的一些的属性,而且fetch_assoc()是获取数据数组中的每一行

{
while ($row=$result->fetch_assoc()) {
echo "<br>Username".$row[&#39;username&#39;]."<br>Password:".$row[&#39;password&#39;];
}
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template