The first step is to open Navicat and create a new database.
The second step is to create a new table in the database.
Related recommendations: "Navicat for mysql graphic tutorial"
The third step is to save the table.
The fourth step is to add data to the table.
The fifth step is to open the ide, enter the following php code, and use localhost to open the php file.
<?php // ip地址、用户名、密码 $con=mysql_connect("localhost","root","123456"); // 判断是否连接成功 if(!$con){ die('数据库连接失败'.mysql_error()); }else{ // 设置编码格式 mysql_query("set name utf8"); // 选择数据库 mysql_select_db("test",$con); // 选择数据表 $result=mysql_query("select * from user"); // 输出表格及th echo "<table border='2' width='300'> <tr> <th>id</th><th>name</th> <th>password</th> </tr>"; // 遍历数据表中的内容 while($row=mysql_fetch_array($result)){ echo "<tr>"; // userid === 用户id 与数据表中的id名称对应 echo "<td>".$row['userid']."</td>"; // username === 用户名称 与数据表中的username名称对应 echo "<td>".$row['username']."</td>"; // password === 用户密码 与数据表中的密码对应 echo "<td>".$row['password']."</td>"; echo "</tr>"; } echo "</table>"; } // 关闭数据库 mysql_close($con); ?>
The page displayed in the browser:
The above is the detailed content of How to connect navicat database to php. For more information, please follow other related articles on the PHP Chinese website!