php - Cross-Class PDO Not Performing -


I am working to use all mysql_ * and mysqli connections PDO but I can not even get this The basics right I have two files: which makes the connection and gives returns and which uses the connection for any purpose which I consider to be necessary for some reasons However, I use to properly Can not get the connection to return. If I run any PDO function (Query, Ready, etc.), I get a server response of 500 with an error message. I know, however, that if I'm making a connection and I do all the queries in the same function, then everything is hooky-dori. I'm assuming that this is something simple, so maybe fresh eyes can help me.

db_connect.php (for connection):

  class db_connect {function __construct () {} function __destruct () {} public Function Connect () {require_once 'db_info.php'; {$ Dbh = New PDO (try 'mysql: host =' .db_host. '; Dbname =' .db_DATABASE, DB_USER, DB_PASSWORD); $ Dbh- & gt; Set Attribution (PDO :: ATTRRAMOD, PDO :: ERRMODDEXEPEPS); $ Dbh- & gt; Set Attribution (PDO :: ATTRMEMULUmTAPARPRS, Incorrect); } Hold (PDOException $ e) {echo 'connection failed:'. $ E & gt; GetMessage (); } Return $ dbh; } Public event closed () {$ dbh = null; }}   

operations.php (uses connection):

  class operations {private $ dbh; Function __construct () {require_once 'db_connect.php'; $ This- & gt; Dbh = new db_connect (); $ This- & gt; Dbh- & gt; Connect (); } Function runQuery () {// Cause 500 error, no logs: $ stmt = $ dbh- & gt; Query ("select from myTable"); }}   

I searched around for some answers but none of the solutions I have received does not work for me. Thank you in advance for any help.

You are effectively throwing your PDO object. When you call:

  $ this-> Dbh = new db_connect (); // $ this- gt; Dbh a db_connect $ this- & gt; Dbh- & gt; Connect (); // gives a PDO, which you do not save anywhere   

then you try to do this later:

  $ stmt = $ dbh - & gt; Query ("Select from MyTable"); // $ dbh also does not exist // you mean $ $ - dbh, but this is either $ //- this-> Dbh A db_connect object will not work, which does not apply the query () method   

I think what you want to do, save the PDO object as part of db_connect, And then use it for query:

  class db_connect {public $ connection; Function __construct () {} function __destruct () {} public function connect () {require_once 'db_info.php'; {$ Dbh = New PDO (try 'mysql: host =' .db_host. '; Dbname =' .db_DATABASE, DB_USER, DB_PASSWORD); $ Dbh- & gt; Set Attribution (PDO :: ATTRRAMOD, PDO :: ERRMODDEXEPEPS); $ Dbh- & gt; Set Attribution (PDO :: ATTRMEMULUmTAPARPRS, Incorrect); } Hold (PDOException $ e) {echo 'connection failed:'. $ E & gt; GetMessage (); } $ This- & gt; Connection = $ dbh; Return $ dbh; } Close public functions () {$ this- & gt; Connection = null; }} Classroom Operations {Private $ dbh; Function __construct () {require_once 'db_connect.php'; $ This- & gt; Dbh = new db_connect (); $ This- & gt; Dbh- & gt; Connect (); } Function runQuery () {$ stmt = $ this- & gt; Dbh- & gt; Connection- & gt; Query ("SELECT *) from MyTable" ;; Finally, if you are just using the db_connect class to generate a PDO connection, then you should be connected to a static method () and just save the PDO object (@kkadad). Here, I have moved my need_once to a more appropriate location:  
  ---------- db_connect.php ---------- Need_once "db_info .php"; Square db_connect {public static function connect () {$ dbh = null; {$ Dbh = New PDO (try 'pgsql: host =' .db_host. '; Dbname =' .db_DATABASE, DB_USER, DB_PASSWORD); $ Dbh- & gt; Set Attribution (PDO :: ATTRRAMOD, PDO :: ERRMODDEXEPEPS); $ Dbh- & gt; Set Attribution (PDO :: ATTRMEMULUmTAPARPRS, Incorrect); } Hold (PDOException $ e) {echo 'connection failed:'. $ E & gt; GetMessage (); } Return $ dbh; }} ---------- operations.php ---------- require_once "db_connect.php" class operations {private $ dbh; Function __ composition () {$ this- & gt; Dbh = db_connect :: connect (); } Function runQuery () {$ stmt = $ this- & gt; Dbh- & gt; Query ("Select * FROM word limit 2"); Print_r ($ stmt-> F.L.L.); }}    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -