php连接mysql...mysqli和mysql
mysql_connect()这一系列函数已经不推荐使用了,不安全。
<?php $con = mysql_connect('localhost','root','');// 选择连接数据库系统 if(!$con){ die('no con...' . mysql_error()); } mysql_select_db('test', $con);// 选择哪个数据库 $sql = 'select * from hello limit 1,2';// sql $re = mysql_query($sql, $con);// 查询,结果是个资源 while($row = mysql_fetch_array($re)){// mysql_fetch_assoc print_r($row); }
现在推荐使用mysqli的系列函数。
<?php // 参数分别是:主机名 用户名 密码 数据库名称 端口 $con = mysqli_connect('localhost','root','','test');// 这里已经包括数据库名称 $sql = 'select * from hello limit 1,2'; $re = mysqli_query($con, $sql); while($row = mysqli_fetch_assoc($re)){ print_r($row); }
-
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步