PHP中为什么mysqli需要实例化,而mysql不需要?
$mysqli=newMySQLi($this->host,$this->username,$this->password,$this->db);$conn=mysql_...
$mysqli = new MySQLi($this->host, $this->username, $this->password, $this->db);
$conn = mysql_connect($this->host, $this->username, $this->password);
这两句话中mysqli需要实例化,而mysql_connect则不需要? 展开
$conn = mysql_connect($this->host, $this->username, $this->password);
这两句话中mysqli需要实例化,而mysql_connect则不需要? 展开
展开全部
mysqli也不一定需要实例化,之所以你要实例化是因为你是要以面向对象的方式来开发这个程序,但是你要是用面向过程的方式来写也是可以的,百度里面有例子你可以看一下
一、面向对象
<?php
$mysqli =new mysqli("localhost", "my_user", "my_password", "world"); //实例化对象
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
/* close connection */
$mysqli->close();
?>
二、面向过程
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", mysqli_get_host_info($link));
/* close connection */
mysqli_close($link);
?>
一、面向对象
<?php
$mysqli =new mysqli("localhost", "my_user", "my_password", "world"); //实例化对象
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", $mysqli->host_info);
/* close connection */
$mysqli->close();
?>
二、面向过程
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
printf("Host information: %s\n", mysqli_get_host_info($link));
/* close connection */
mysqli_close($link);
?>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询