Home  >  Article  >  Database  >  Detailed explanation of php reading specific row instances from the database

Detailed explanation of php reading specific row instances from the database

怪我咯
怪我咯Original
2017-06-16 11:09:171501browse

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;];
}
}
?>

The above is the detailed content of Detailed explanation of php reading specific row instances from the database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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