使用带有oops的mysqli创建数据库并在数据库中插入值
Posted
技术标签:
【中文标题】使用带有oops的mysqli创建数据库并在数据库中插入值【英文标题】:create database using mysqli with oops and insert value in database 【发布时间】:2014-11-21 02:42:33 【问题描述】:我在插入时遇到错误,所以我很困惑。请帮助我或建议我最好的 oop 教程
testdb.php 页面
define('DB_SERVER', 'localhost');
define ('DB_USERNAME', 'ashish');
define ('DB_PASSWORD', 'ashish33');
define ('DB_DATABASE', 'users');
class DB_Class
function __construct()
$connection = mysqli_connect("localhost", "root", "","test");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
testclass.php 页面
class user
public $fname;
public $lname;
public $fullname;
public function __construct()
$this->fname = $fname;
$this->lname = $lname;
$this->fullname = $fullname;
$db = new DB_Class();
$result = mysqli_query($db, "INSERT INTO users(fname, lname, fullname) values ('$this->fname', '$this->lname','$this->fullname')") or die(mysqli_error());
return $result;
post.php
$userTest = new user();
$userTest->fname = $_POST['fname'];
$userTest->lname = $_POST['lname'];
$userTest->fullname = $_POST['fullname'];
?>
【问题讨论】:
你遇到了什么错误。 当你创建一个新用户时,对象的值被分配并保存到数据库中。之后,您分配 POST 变量。您必须从构造函数中获取所有这些,或者将变量分配给构造函数。 警告:mysqli_query() 期望参数 1 为 mysqli,在线 $result = mysqli_query 【参考方案1】:把它放在构造函数的参数里面:
include 'testdb.php';
public function __construct($fname, $lname, $fullname)
$this->fname = $fname;
$this->lname = $lname;
$this->fullname = $fullname;
$db = new DB_Class();
$conn = $db->connect();
// $db = new mysqli('localhost', 'username', 'password', 'database');
$result = mysqli_query($conn, "INSERT INTO users(fname, lname, fullname) values ('$this->fname', '$this->lname','$this->fullname')") or die(mysqli_error());
return $result;
然后,初始化:
$userTest = new user($_POST['fname'], $_POST['lname'], $_POST['fullname']);
旁注:既然您使用的是 mysqli,为什么不使用准备好的语句。
编辑:您在DB_class
上忘记了return
:
class DB_Class
public $conn;
public function __construct()
$connection = mysqli_connect("localhost", "root", "","test");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$this->conn = $connection; // return the mysqli instance
public function connect()
return $this->conn;
【讨论】:
我用过这个方法,但是在 $result = mysqli_query($db, "INSERT INTO users(f @user3364529 你确定在这个文件中找到了DB_Class
吗?
警告:mysqli_query() 期望参数 1 为 mysqli,
我有 print print_r($db) 然后我有 DB_Class Object ()
@user3364529 是否包含在 testdb.php
中 testclass.php
? include 'testdb.php'
【参考方案2】:
you can try the below code in
class user extends DB_Class
public $fname;
public $lname;
public $fullname;
public function __construct()
parent::__construct(); //add this line of code
public function insert($fname, $lname, $fullname)
$this->fname = $fname;
$this->lname = $lname;
$this->fullname = $fullname;
$db = new DB_Class();
$result = mysqli_query($db, "INSERT INTO users(fname, lname, fullname) values ('$this->fname', '$this->lname','$this->fullname')") or die(mysqli_error());
return $result;
【讨论】:
致命错误:当当前类作用域没有父级时,无法访问父级:: 在这些行上显示未定义的变量 $this->fname = $fname;以上是关于使用带有oops的mysqli创建数据库并在数据库中插入值的主要内容,如果未能解决你的问题,请参考以下文章