Jump to content

In Need For A Database Class


cybernet

Recommended Posts

can anyone of you can give me a database class that uses PDO ?

 

something like this

class SQL {
private static $dbh = null;
private function __construct($host, $user, $pass, $data, $table) {
$this->host = (!empty($host)) ? $host: 'localhost';
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->table = $table;
static::connect();
}
private function __destruct() {
static::close();
}
public static function connect() {
$dsn = 'mysql:dbname=' . $this->table . ';host=' . $this->host;
static::$dbh = new PDO($dsn, $this->user, $this->pass);
static::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function close() {
static::$dbh = null;
}
public static function query($query) {
$sth = static::$dbh->prepare($query);
$sth->execute();
}
}

 

but should include query count, if possible ^_^

Link to comment
Share on other sites

The php coding help forum section is for help with actual code you are writing. It's not for asking for code. Moving thread the misc forum section...

 

Also, the sample code you posted can only execute simple queries without any parameters in them since it does not have anyway of binding parameters to the prepared query statement. So, it's too simple as it is.

Link to comment
Share on other sites

How about PDO itself? Why would you want to wrap PDO inside a class? From your example above I don't see you add anything useful.

 

i'm building a project for the first time with PDO

 

this is how i configured it

 

index.php

 

<?php
require_once (dirname(__FILE__) . '/inc/main.php');

userlogin();

$HTMLOUT = '';

$stmt = $db->query("SELECT row_id, name, mobile FROM location LIMIT 3");
// $stmt->execute(array($id, $name));
$stmt->setFetchMode(PDO::FETCH_OBJ);


$db = null;
$stmt = null;

?>

 

inc/main.php

 

<?php

require_once (dirname(__FILE__) . '/pdo_conn.php');
require_once (dirname(__FILE__) . '/mail/class.Mail.php');

error_reporting(E_ALL);


function userlogin() {
global $db;
unset($GLOBALS["CURUSER"]);

$ip = getip();
$nip = ip2long($ip);

$id = 0 + get_cookie('uid');


$fetch_user_details = $db->prepare("SELECT * FROM users WHERE user_id = :bitches_id LIMIT 1");
$fetch_user_details->bindParam(':bitches_id', $id, PDO::PARAM_INT);
$fetch_user_details->execute();
$fetch_user_details->setFetchMode(PDO::FETCH_OBJ);
$row = $fetch_user_details->fetch();


$user_id = $row->user_id;
$user_ip = $row->user_last_ip;
$update_user_details = $db->prepare("UPDATE users SET user_last_access = UNIX_TIMESTAMP(), user_last_ip = :last_ip WHERE user_id = :u_id LIMIT 1");
$update_user_details->bindParam(':last_ip', $user_ip, PDO::PARAM_STR, 15);
$update_user_details->bindParam(':u_id', $user_id, PDO::PARAM_INT);
$update_user_details->execute();

$GLOBALS["CURUSER"] = $row;

}

function is_logged_in() {
global $CURUSER;
if (!$CURUSER) {
header("Location: domain.net/login.php?403");
exit();
}
}

?>

 

inc/pdo_conn.php

 

<?php

$db = new PDO('mysql:host=localhost;dbname=abc;charset=UTF-8', 'abc', 'xam');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

?>

 

until a few days ago it worked, but when i starting to expand my project

i started to get this error

 

PHP Fatal error: Call to a member function prepare() on a non-object in /root/inc/main.php"

 

the error reffers to this line

$fetch_user_details = $db->prepare("SELECT * FROM users WHERE user_id = :bitches_id LIMIT 1");

Link to comment
Share on other sites

The error means exactly what is says it means..

Call to a function of a non-object

 

A non-object indicates that there is no object to be pulling a function from. I willing to bet that it has to do with you using globals,...incorrectly

In PHP global variables must be declared global inside a function

That quote is from the manual.

 

Also, you shouldn't even be using globals. You have already included the file with the $db declaration...ie pdo_conn.php, Making it global is redundant. You might also want to check whether or not your pdo_conn.php file is actually being included

Link to comment
Share on other sites

Posting your original thread/code as an answer to ignace's question has nothing to do with a question about a database class that uses PDO that can count queries.

 

You seem to think that wrapping a database class around PDO, which is already a database class - as was already stated in your original thread, will somehow fix your error. It won't. Whatever change you made in your original code, from the time it worked, to the time it started producing that error is what caused the problem. You must troubleshoot why your code is producing that error. Stick to your original thread for your original code and question.

 

If you want someone to help you find a database class that used PDO that can count queries, that's what this thread is for. Don't switch back to your original code/error in this thread.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.