首页 > 编程知识 正文

php面向对象mysql(PHP 对象)

时间:2023-12-19 00:42:52 阅读:317515 作者:QVPK

本文目录一览:

请问php如何面向对象操作mysql数据库呢?

$db=@new mysqli("localhost","root","123456");

If (mysqli_connect_errno()) //检查连接是否成功,mysqli_connect_errno()函数返回连接失败的错误编号。

{

echo "数据库连接失败";

eixt;

}

else

echo "数据库连接成功"

php面向对象操作mysql。我需要修改什么地方?

试修改代码如下:

?php

class mysql

{

private $host;

private $root;

private $pass;

private $conn;

private $table;

function __construct($host, $root, $pass, $table)

{

$this-host = $host;

$this-root = $root;

$this-pass = $pass;

$this-table = $table;

$this-connect();

}

function connect()

{

$this-conn = mysql_connect($this-host, $this-root, $this-pass) or die(mysql_error());

mysql_select_db($this-table, $this-conn) or die(mysql_error());

}

function query($table)

{

mysql_query($table,$this-conn);

}

function fn_inset($table, $name, $value)

{

echo "insert into $table ($name) value ($value)";

//$this-query("insert into $table ($name) value ($value)");

}

}

$db = new mysql('localhost','root','','xiaoguang');

$db-fn_inset('table', 'id,table,content,time', "'','我插入的信息','我插入的内容',time()")

?

请问如何用php面向对象操作mysql数据库?

$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno($con))

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

mysqli_query($con,"SELECT * FROM Persons");

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)

VALUES ('Glenn','Quagmire',33)");

mysqli_close($con);

请问PHP怎样用面向对象方式连接MySQL数据库?

?php

$servername = "localhost"; //你的连接地址,可以是ip

$username   = "username"; //你的用户名

$password   = "password"; //你的密码

$dbname     = "myDB"; //你要连接数据库的名字

// 创建连接

$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接

if ($conn-connect_error) {

    die("连接失败: " . $conn-connect_error);

}

echo "连接成功";

学习可以参考:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。