Drew
Drew

Reputation: 6872

PHP / MySQL Need a Database Class

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:

$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?

Upvotes: 0

Views: 793

Answers (1)

Pastor Bones
Pastor Bones

Reputation: 7371

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

However you cand find many other suggestions on this stackoverflow question

Upvotes: 3

Related Questions