cybernet Posted October 30, 2012 Share Posted October 30, 2012 (edited) 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"); Edited October 30, 2012 by cybernet Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/ Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 Either the main page has lost the file that declares $db as a new PDO (have no idea why this is in a file all it's own), $db is being destroyed or changed to a different type of variable in the code between being declared as an object and being called for the prepare, some other problem is preventing $db from being declared in the first place. What debugging have you done already? To be fair though, none of these are the biggest problem with your code. Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388646 Share on other sites More sharing options...
trq Posted October 30, 2012 Share Posted October 30, 2012 Don't try and force functions to use variables declared in the globals namespace, it defeats the a big part of there purpose. Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388659 Share on other sites More sharing options...
cybernet Posted October 30, 2012 Author Share Posted October 30, 2012 What debugging have you done already? To be fair though, none of these are the biggest problem with your code. 1. none :-\ 2. like i said this is the first time i'm using pdo ... so i did how i thought at the moment Don't try and force functions to use variables declared in the globals namespace, it defeats the a big part of there purpose. how should i proceed ? can you please help me thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388717 Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 first thing I would do would be to take these lines from pdo_con.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); and put them in the main.php just above where you are getting the error. Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388721 Share on other sites More sharing options...
cybernet Posted October 30, 2012 Author Share Posted October 30, 2012 (edited) if i would do that, then i have to call userlogin() on each page. on a search on google i found that i should create a class to connect to database, but i really don't know how to make it secure and flawless http://stackoverflow...object-php-help i found some tuts online, but there are many stuff that i don't need ( at least for know ) this would be an example http://www.tiny-thre...o-class-update/ i know that i sound very ... n00b and dumb also, but i really need an advice here @muddy i've checked youre blog and i saw that you have something there with classes http://muddy-dev.blogspot.ro/2012/10/php-classes-simple-sample-class.html but i didn't find a class that extends pdo ( mysql ) can you give me a tip, tutorial, something ? Edited October 30, 2012 by cybernet Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388726 Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 PDO is it's self an abstraction layer class - this meens that it sits on top of the actual interface objects and provides a relativly standard way of interacting with any database that PDO has drivers to support. In my opinion, unless you commonly link to different types of storage engine it's more complicated than it ever needs to be. If your up for learning about classes, and the only engine your planning on using just now is mysql, I'd say the best thing to do would be to create your own mini abstraction layer using mysqli rather than using PDO. Alternativly, if you want to keep with PDO then you could write a class that extends PDO, and sets the PDO Attributes on construct. If you want help with either of these approaches let me know and I'll fling some more info your way. Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388757 Share on other sites More sharing options...
cybernet Posted October 30, 2012 Author Share Posted October 30, 2012 (edited) well, as you can see in my signature i'm a MySQL freak so i would like to create a class that extends pdo and connects to mysqli i want to use pdo because it's safe for queries for now all i want is a class that connects to MySQL with mysqli ( it's supposed to be iMproved ), one that has a query count, and possible a way to cache queries for start if you could help only with the class, and the rest i will try to figure out myself thanks in advance Edited October 30, 2012 by cybernet Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388806 Share on other sites More sharing options...
kicken Posted October 30, 2012 Share Posted October 30, 2012 for now all i want is a class that connects to MySQL with mysqli ( it's supposed to be iMproved ), one that has a query count, and possible a way to cache queries There is no connecting with Mysqli, mysqli is just a different access layer within PHP. When it comes to talking with mysql pdo, mysqli, and mysql all use the same fundamental mysql library. The difference is the API that is exposed to PHP and what they allow you to do. On that level, PDO and Mysqli are more or less equivalent. You can extend PDO if you want, just like you can extend any other class. I do in order to provide a bit nicer debugging features and set some initial settings. You would still be connecting the same way though. Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388848 Share on other sites More sharing options...
PFMaBiSmAd Posted October 30, 2012 Share Posted October 30, 2012 (edited) No matter what database class you use, you need to troubleshoot why $db isn't an instance of it at the point where the error is occurring at. About the only way your code could be producing that error, since the pdo db connection would be producing an exception if it is failing, assuming your connection code is actually being executed, is if you are overwriting $db somewhere in the code you didn't post. What's the code for your functions getip() and get_cookie()? And any other code from the start of index.php up to the point of that error that you might have left out? Edited October 30, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388855 Share on other sites More sharing options...
cybernet Posted October 31, 2012 Author Share Posted October 31, 2012 function get_cookie($name) { global $ResT_BV; if ( isset($_COOKIE[$ResT_BV['cookie_prefix'].$name]) AND !empty($_COOKIE[$ResT_BV['cookie_prefix'].$name]) ) { return urldecode($_COOKIE[$ResT_BV['cookie_prefix'].$name]); } else { return FALSE; } } function getip() { if (isset($_SERVER)) { if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && validip($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && validip($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } else { $ip = $_SERVER['REMOTE_ADDR']; } } else { if (getenv('HTTP_X_FORWARDED_FOR') && validip(getenv('HTTP_X_FORWARDED_FOR'))) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_CLIENT_IP') && validip(getenv('HTTP_CLIENT_IP'))) { $ip = getenv('HTTP_CLIENT_IP'); } else { $ip = getenv('REMOTE_ADDR'); } } return $ip; } i didn't include this in the 1st post because they were irrelevant to the issue i'm having so does anyone have a simple class { database } to use with pdo ? please Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1388924 Share on other sites More sharing options...
Christian F. Posted October 31, 2012 Share Posted October 31, 2012 (edited) PDO is a class..? $database = new PDO (...); If this isn't what you meant, please expand upon your questions with some more details. PS: Don't use global inside your functions, that's breaking with the idea behind using functions in the first place. Send the data as a parameter instead, like you've done with the $name. If that's a config array, then you might want to use constants instead. They're globally accessible, and (as the name implies) constant. So once they're defined nothing can change them. Edited October 31, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389012 Share on other sites More sharing options...
cybernet Posted October 31, 2012 Author Share Posted October 31, 2012 (edited) i said that pdo is a class, because at the time i wrote the post, i remembered a code line that began with class XyZ extends DB { ... where DB was the pdo connect thing ... @Christian F, thank you for the information you really clarified that error for me Edited October 31, 2012 by cybernet Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389029 Share on other sites More sharing options...
Christian F. Posted October 31, 2012 Share Posted October 31, 2012 Glad I could help, though I must confess I'm a bit confused as to exactly how I did that. In any case, you're welcome. (Thought you were asking for a class that could use PDO, when PDO is a class itself.) Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389035 Share on other sites More sharing options...
cybernet Posted October 31, 2012 Author Share Posted October 31, 2012 (edited) Glad I could help, though I must confess I'm a bit confused as to exactly how I did that. In any case, you're welcome. (Thought you were asking for a class that could use PDO, when PDO is a class itself.) 1. Send the data as a parameter instead, like you've done with the $name. 2. yes, i was asking for a class that could use PDO like this one 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(); } } so now i should use : function userlogin($db) { unset($GLOBALS["CURUSER"]); } // etc instead of : function userlogin() { global $db; unset($GLOBALS["CURUSER"]); } // etc ? Edited October 31, 2012 by cybernet Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389037 Share on other sites More sharing options...
cybernet Posted October 31, 2012 Author Share Posted October 31, 2012 i've tried and it's not working give me a little help please Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389072 Share on other sites More sharing options...
cybernet Posted November 2, 2012 Author Share Posted November 2, 2012 // bump no one ? Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389498 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2012 Share Posted November 2, 2012 (edited) No one what? Without the current code (i.e. the it) and the symptoms or errors you got (not working doesn't tell us any useful information) when you ran that code, no one here can help. Edited November 2, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1389502 Share on other sites More sharing options...
cybernet Posted November 4, 2012 Author Share Posted November 4, 2012 never mind i helped my::self Quote Link to comment https://forums.phpfreaks.com/topic/270065-trouble-with-pdo/#findComment-1390110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.