PHP / MySQL需要一个数据库类

时间:2021-12-12 16:24:47

I need some help. I am working on a project and I found myself writing a bunch of functions for grabbing information from the database:

我需要一些帮助。我正在研究一个项目,我发现自己编写了一堆函数来从数据库中获取信息:

For Example:

function getSlideByUserAndSortId($sort_order) {
global $mysqli;
global $user_id;

if ($stmt = $mysqli->prepare("SELECT slide_id FROM user_slides WHERE user_id = ? AND sort_order = ?")) {
    $stmt->bind_param('ii', $user_id, $sort_order);
    $stmt->execute();
    $stmt->bind_result($returnSlideId);
    $stmt->store_result();
    $stmt->fetch();

    return $returnSlideId;

    $stmt->close();
}
}

function getSlideText($var) {
global $mysqli;
global $user_id;
global $current_slide;

if ($stmt = $mysqli->prepare("SELECT text FROM slide_data WHERE slide_id = ? AND user_id = ? LIMIT 1")) {
    $stmt->bind_param('ii', $current_slide, $user_id);
    $stmt->execute();
    $stmt->bind_result($text);
    $stmt->store_result();
    $stmt->fetch();

    $text = ($stmt->num_rows < 1 ? $var : $text);

    return $text;

    $stmt->close();
}
}

Which is so messy.. What I need is a class like Codeigniter where I can just do:

哪个太乱了..我需要的是像Codeigniter这样的课程,我可以这样做:

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

How does everyone else do it? If you have a lot of information you need to grab and a lot of queries, what is the best way to display the data?

其他人如何做到这一点?如果您需要获取大量信息并进行大量查询,那么显示数据的最佳方式是什么?

1 个解决方案

#1


3  

What you're looking for is an Object Relational Mapper and there have been many created for use with PHP and MySql. Some of the best and most widely used are Propel and Doctrine

您正在寻找的是对象关系映射器,并且已经创建了许多用于PHP和MySql的对象关系映射器。一些最好和最广泛使用的是Propel和Doctrine

However you cand find many other suggestions on this * question

但是,您可以在此*问题上找到许多其他建议

#1


3  

What you're looking for is an Object Relational Mapper and there have been many created for use with PHP and MySql. Some of the best and most widely used are Propel and Doctrine

您正在寻找的是对象关系映射器,并且已经创建了许多用于PHP和MySql的对象关系映射器。一些最好和最广泛使用的是Propel和Doctrine

However you cand find many other suggestions on this * question

但是,您可以在此*问题上找到许多其他建议