-
Posts
88 -
Joined
-
Last visited
Posts posted by cybernet
-
-
does anyone know a class that can generate a menu with category and subcategory that connects to mysql with pdo ?
thank you
-
so i found a pdo class to connect to mysql with pdo and looks easy to customize
however i have an error that i can't figure it out
class
<?php class ApplicationResourcePool { static var $_dbHandle; // line 5 private static $_dbConfig = array( 'dsn' => 'mysql:dbname=vax', 'username' => 'vax', 'password' => 'veryrandpasswd', ); public static getDbHandle(){ if(self::$_dbHandle == null){ self::$_dbHandle = new PDO( self::$_dbConfig['dsn'] , self::$_dbConfig['username'], self::$_dbConfig['password'] ); } return self::$_dbHandle; } } class StockMapper { protected $_dbh; public __construct($dbh = null) { if($dbh == null){ $this->_dbh = ApplicationResourcePool::getDbHandle(); } else { $this->_dbh = $dbh; } } public getStockById($stockId){ $sth=$this->_dbh->prepare("SELECT * from stock WHERE id = :stockId"); $sth->bindParam(":stockId", $stockId); $sth->execute(); $result = $sth->fetch(PDO::FETCH_ASSOC); return $result[0]; } } $stockMapper = new StockMapper(); $stockData = $stockMapper->getStockById('302'); ?>
Parse error: syntax error, unexpected 'var' (T_VAR), expecting variable (T_VARIABLE) in /path/index.php on line 5
i really don't know what to do
-
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");
-
i think i forgot to mention, simple ...
but still i gave the example above
-
never mind
i helped my::self
-
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
-
// bump
no one ?
-
i've tried and it's not working
give me a little help please
-
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
?
-
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
-
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
-
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
-
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 ?
-
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
-
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");
-
Which means that this
isset($status)
always return true.so what should i use ?
-
So what's the problem then?
if status didn't had a value - don't show <p> tag at all
and if it did show the <p> tag with error or success message ...
doesn't work
$show_status doesn't show anything
-
@lemmin thank you very much for explaining me with so much detail
but, you see this is what i did in the first place // before coming up with that ? statement : see
the code was
$htmlout = "<div>a</div> lalalalala <p style='color:red'>".$status."<br /></p> lalala";
and then i realised if the form wasn't submitted i will get
<p style='color:red'><br /></p>
so that will be shown for no reason / so <p> that will be there for nothing cause its empty
so that's why i chose that ternary operator
if status didn't had a value - don't show <p> tag at all
and if it did show the <p> tag with error or success message ...
i hope that all of you, understood what's my malefic plan
thanks for your time
-
Well, you have seemingly no logic to determine if the form was submitted or not.
i don't how to explain my self in order for you guys to understand me ...
-
if ( !isset($_POST[ $x ])
This is equivalent to saying:
if ( !isset($_POST['phone'])
, since "phone" was the last array item in the loop. Is that what you expected?i know ...
what i'm trying to do is make $show_status to either show NULL if the form wasn't submitted, if form submitted show either $lang['error'] OR $lang['success']
-
Eh, what? You can search INT fields...
that's why i edited the post cause i tested my "advice"
i replied like that cause i thought in a logical way / how can you search for a-Z in a numeric field / it's impossible to find because mysql doesn't accept text in int field / so why run the query ...
Who said $search contains non-int characters? MySQL will just typecast it anyway.
i presumed that backend user wont search for 1347289747
-
foreach( array('lastname','firstname','email', 'message', 'phone') as $x ) { ${$x} = $_POST[ $x ]; } if ( !isset($_POST[ $x ]) || empty($message) || empty($email) )
This makes very little sense. What are you trying to do here?
for each value in the $array is created, $message $email etc ... // i guess you already knew this
if the form isn't posted or message field is empty .. do that
what i'm i missing ? :-\
-
in the html part i have the $show_status variable which i want to show NULL if the form wasn't submitted
and if the form was submitted i want that $show_status to show either $lang['error'] OR $lang['success']
but $show_status shows NULL when executed
-
Eh, what? You can search INT fields...
that's why i edited the post cause i tested my "advice"
i replied like that cause i thought in a logical way / how can you search for a-Z in a numeric field / it's impossible to find because mysql doesn't accept text in int field / so why run the query ...
unexpected (T_VAR), expecting variable (T_VARIABLE)
in PHP Coding Help
Posted · Edited by cybernet
please stop mocking me
i presume it should be "private" instead of "static" ... right ?