1.PHP连接mysql
1.使用mysqli_connect()函数连接到MySQL数据库:
mysqli_connect()函数的格式如下:
mysqli_connect('MySQL服务器地址','用户名','用户密码','要连接的数据库名')
示例:
<?php $db=mysqli_connect('rm-bp1mcj07cq777jh904o.mysql.rds.aliyuncs.com','root','Si971666','adatabase'); //连接数据库 ?>
<?php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>
<?php $db=mysqli_connect('rm-bp1mcj07cq178jh904o.mysql.rds.aliyuncs.com','root','Si97166','adatabase') or die("无法连接到服务器"); print ("成功连接到服务器"); mysqli_close($db); ?>
使用mysql_query()函数执行SQL语句:
<?php $db=mysqli_connect('rm-bp1mcj07cq777jh904o.mysql.rds.aliyuncs.com','root','Si971077','adatabase') or die("无法连接到服务器"); print ("成功连接到服务器<br/>"); $sq="insert into users (name,age,gender,info) values('fangfang',18,'femele','She is a 18 years lady')"; $result=mysqli_query($db,$sq); if($result){echo"插入数据成功!<br/>";}else {echo "插入数据失败!<br/>";} mysqli_close($db); ?>
mysqli_fetch_row获取SQL查询结果:
<?php $db=mysqli_connect('rm-bp1mcj07cq1777jh904o.mysql.rds.aliyuncs.com','root','Si971777','adatabase') or die("无法连接到服务器"); print ("成功连接到服务器<br/>"); $sql="select * from adatabase.users"; $result=mysqli_query($db,$sql); $row = mysqli_fetch_row($result); echo $row[0]; mysqli_close($db); ?>
other....