mysql - Can't get PHP PDO and OOP classes to work -
i trying learn how use php pdo object oriented programming.
i have tried following these 2 tutorials: http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/ http://culttt.com/2012/09/24/prevent-php-sql-injection-with-pdo-prepared-statements/
but can't on either of them work.
the second 1 gives download link pre-written wrapper class class.db.php
http://www.imavex.com/php-pdo-wrapper-class/
using pre-written wrapper class , trying simple tutorial.php
(credentials changed):
// include database class include("class.db.php"); // connect database $db = new db("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password"); $results = $db->select("ad_publication"); print_r($results);
the above shows blank page.
i know there nothing wrong pre-written class , text of above example copied directly out of tutorial , comments full of , praise.
i know there nothign wrong credentials works fine:
try { $pdo = new pdo('mysql:host=localhost;dbname=my-db-name', 'my-username', 'my-password'); $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); $pdo->exec('set names "utf8"'); $output = 'connection successful'; echo $output; } catch (pdoexception $e) { $output = 'unable connect database server.' . $e->getmessage(); echo $output; exit(); }
and outputs connection successful
my server running php 5.5 , table used in example above innodb
table.
when run example select statement error logs show:
php notice: undefined variable: ghg678 in /var/www/vhosts/mywebsite.com.au/httpdocs/booking/tutorial.php on line 7
line 7:
$db = new db("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password");
php warning: creating default object empty value in /var/www/vhosts/mywebsite.com.au/httpdocs/booking/class.db.php on line 18
line 18:
$this->error = $e->getmessage(); // (from public function __construct)`
php fatal error: call member function select() on non-object in /var/www/vhosts/mywebsite.com.au/httpdocs/booking/tutorial.php on line 9
line 9:
$results = $db->select("ad_publication"); // (an existing table data in it)
i can't see doing wrong wrapper class not written me , no-one else complaining (heaps of praise) , contents of tutorial.php copied directly page table name changed.
like say, using pdo connection , doing normal pdo queries without wrapper class, work fine.
can here see going wrong or know of should at?
i'm not sure why copped -1 question?? thought complete.
anyway, @sean providing clue answer.
my password in fact have $ character in it.
the connection code of mine work (as shown above) is:
$pdo = new pdo('mysql:host=localhost;dbname=my-db-name', 'my-username', 'my-password');
their code using is:
$pdo = new pdo("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password");
changing "
characters '
worked straight away. password auto-generated hosts (plesk) , don't know enough php know there difference between '
, "
. i've never known why people use 1 , use other. seems still have lot learn.
@sean, because didn't put reply, couldn't choose suggestions answer, don't know how give points, thank steering me in right direction.
Comments
Post a Comment