Can't get PHP PDO and OOP classes to work [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to learn how to use PHP PDO as Object Oriented Programming.
I have tried following these two 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 I can't get anything on either of them to work.
The second one gives a download link to a pre-written wrapper class class.db.php from http://www.imavex.com/php-pdo-wrapper-class/
using this pre-written wrapper class and trying something as simple as this tutorial.php (credentials changed):
// Include the database class
include("class.db.php");
// Connect to 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 a blank page.
I know there is nothing wrong with the pre-written class and the text of the above example as it was copied directly out of the tutorial and the comments are full of thanks and praise.
I know there is nothign wrong with my credentials as this 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 to connect to the database server.' . $e->getMessage();
echo $output;
exit();
}
And outputs Connection Successful
My Server is running PHP 5.5 and the table used in the example above is an InnoDB table.
When I run the example select statement my 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 from 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 to a member function select() on a 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 with data in it)
I just can't see what I could be doing wrong especially as the wrapper class was not written by me and no-one else is complaining about it (heaps of praise) and the contents of tutorial.php were copied directly from the page with only the table name changed.
Like I say, using the PDO connection and doing normal PDO queries without the wrapper class, work fine.
Can anyone here see anything that could be going wrong or know of anything that I should look at?

I'm not sure why I copped a -1 for that question??
I thought it was very complete.
Anyway, thanks to #Sean for providing the clue to the answer.
My password does in fact have a $ character in it.
The connection code of mine that does work (as shown above) is:
$pdo = new PDO('mysql:host=localhost;dbname=my-db-name', 'my-username', 'my-password');
Their code that I was using is:
$pdo = new PDO("mysql:host=localhost;dbname=my-db-name", "my-username", "my-password");
changing the " characters to ' worked straight away.
The password was auto-generated by my hosts (Plesk) and I don't know enough about PHP to know that there is a difference between ' and ". I've never known why some people use one and some use the other. Seems I still have a lot to learn.
#Sean, because you didn't put it as a reply, I couldn't choose your suggestions as the answer, so I don't know how to give you the points, but thank you very much for steering me in the right direction.

Related

Strange inconsistency, database connection from require_once not working in only one place

I've encapsulated my MySQLi connection logic in a script named connect_mysqli.php. This working just fine all over my project (9 other pages are having no trouble), but one page is returning this error:
Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\projectName\php_calls\AddItem.php on line 193
Here's the code that's not working in AddItem.php:
$sql = <<<HEREDOC
UPDATE listing_data
SET ebay_id = '$responseObj->ItemID'
WHERE listing_id = '$database_listing_id'
HEREDOC;
require_once(__DIR__ . '/connect_mysqli.php'); //this creates $conn
$conn->query($sql); //this is line 193
And this is the code from connect_mysqli.php:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$db = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8");
Again, this is working without trouble in every other spot in the project. Here's what I've tried so far:
I checked my SQL syntax. I echoed the SQL query I'm attempting and tested it on the command line, the query is working fine.
Double check my SQL again. I copied the code from a working database call into the AddItem.php. It produces the same error. I tested the original page where this code exists, it works correctly in that spot.
I checked to make sure require_once is working correctly. To make sure my relative path was working correctly, and I am in the cwd that I expected I was in:
echo require_once(__DIR__ . '/connect_mysqli.php');
and it produced:
C:\xampp\htdocs\projectName\php_calls/connect_mysqli.php
This was expected. I opened Windows Power Shell and ran:
cat C:\xampp\htdocs\projectName\php_calls/connect_mysqli.php
This displays the code inside connect_mysqli.php! I was beginning to guess my require_once was flawed.
Check to see if there's a naming collision.
var_dump(get_defined_vars());
There is only one $conn.
The connection is not being closed with $conn->close();. If it's closing without my instruction I don't know how or why.
I copied and pasted the code from connect_mysqli.php into AddItem.php and the error goes away. So somehow my require_once must be messing up my connection. AddItem.php and connect_mysqli.php are in the same folder. I tried connecting with this line instead:
require_once('connect_mysqli.php');
I still get an error.
Sorry for the incredibly lengthy question, I wanted to do my research and try everything before creating another question on the topic. For now I can copy the database connection code into AddItem as a workaround, but it's bad practice, and there's clearly some important principle escaping me here that I'd like to understand.
Edit: more information
Nico Haase asked the question that put me on the right track. Line 1 of AddItem.php is a require_once:
require_once(__DIR__ . '\return_item_php_obj_by_id.php');
and inside return_item_php_obj_by_id.php we have the culprit:
require_once(__DIR__ . '/connect_mysqli.php');
//edited out irrelevant code
mysqli_close($conn);
In the original post I said "There's no $conn->close() hiding anywhere." Clearly I was mistaken. I found the hidden close(). When I comment this out, the connection works. Now I've accidentally made my code really hard to read, and I don't want to use a database connection that far away on the stack. Should I leave the connection open so I can use it again with AddItem.php? What's best practice in this case?

Fatal error: Call to a member function query() on a non-object Error

I have a small problem. I'm trying to make a simple register/login system with sessions and I got this error:
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\members\includes\login.inc.php on line 9
This is the relevant line of code:
$result = $conn->query($sql);
The first time I tried it was working.
The rest of the code:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$email = $_POST['email'];
$password = md5($_POST['password']);
$sql = "SELECT email, password FROM member WHERE email = '$email' AND password = '$password'";
$result = $conn->query($sql);
I also have db.php, which is used to connect the MYSQL and everything inside it is fine.
I cannot understand why, the first time I tried it was working I guess and now this kind of error.
I'm also having the db.php which is used to connect the MYSQL and everything inside is fine.. could someone explain me why I keep facing this error ?
I'm going to speculate. I'm speculating that you have a separate file (probably called db.php) which "handles" the setting up of the database connection. I'm further going to speculate that you've a chain of files which are require() (or include())'d into your web app.
I've seen this more times than I care to recall. It's a very old fashioned way of separating code into logical chunks inside PHP - which needs to be left in the past.
I'm speculating that you were previously defining $conn in another script which was included (or required) before this code. A global variable, which had was dependency later in the code execution. Invisible to the file it was declared in.
That's the problem. The quick/hack fix is to rename $conn or the restore the original declaration of it and make sure it's global and make sure it is included before this code is ran.
The proper fix (IMHO) is to look at using a framework (Laravel, Lumen, CodeIgniter, Yii, there are many - take your pick) and read up on the topics of dependency injection, autoloading and namespacing. Think about why global variable declarations make for unmaintainable code.
If you're really reluctant to go with a full framework, at the very least have a look at some database-abstraction libraries like doctrine (and it's sub-library dbal) which can easily be auto-loaded into your project via composer.
As Sascha already pointed out, $conn might be either not defined at all, or it's not an object (hence the error message).
From the code sample you have provided, it's actually a bit hard to tell what kind of connection object you might be using, but I think it's save to say that in your case it might be either PDO or mysqli.
For the sake of simplicity, let's stick with mysqli. A working code sample based on mysqli would look like this (shortened example taken from the docs cited above):
$conn = new mysqli("localhost", "my_user", "my_password", "world");
$result = $conn->query($sql);
Though you really should go for so-called prepared statements, as your code right now is prone to SQL injection as wally already stated.
I would have linked wally's answer and provide you with a link to the PHP docs relating to prepared statements, but apparently, my lack of reputation points don't allow me to, so just do a quick Google search for PHP & prepared statements.
The database connection file has to be added at the beginning of the file.
The present format is easy.
<?php $mysqli = new mysqli("localhost", "Userid", "password", "database name"); if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; }
?>
At the beginningļ¼š
require 'db.php';
$conn = new db();

MySQLi - property access is not allowed yet [duplicate]

This question already has answers here:
mysqli + xdebug breakpoint after closing statement result in many warnings
(6 answers)
Closed 2 years ago.
Im' getting this warning "property access is not allowed yet" when trying to close mysqli connection. WHY?
$mysqli = new mysqli ( $database ["dbUri"], $database ["dbUserName"], $database ["dbPassword"], $database ["dbSchema"], $database ["dbPort"] );
$mysqli->autocommit(FALSE);
$con = $mysqli;
$rowsAffected = /* completes insert using $con */;
if ($rowsAffected==0) {
throw new Exception("Insert of new record failed");
}
$insertId = $con->insert_id;
$con->commit();
$con->close();
BTW, the insert is successful and I have the correct value in $insertId. Commit works well too, but it's the close that triggers the warning.
I hid the code in /* completes insert using $con */ section as it is long and irrelevant (the sql works). So unless you think it is relevant I only included the rest.
I looked at similar questions but other posts refer to the connection not being established. However, my connection works. PLEASE see the point about "insert is successful".
As far as my analysis of this intermittent problem has concluded (at least in my case), this is some kind of bug in either the mysqli extension or the PHP debugger (XDebug), since it only happens when I breakpoint/single-step the program, but not when simply running the same code without breakpointing/single-stepping any code before the page rendering has completed.
Does it still happen for you if you don't breakpoint or single-step any code before the page rendering is complete?

Using PHP in XAMPP: can create db but cannot create tables

I'm a beginner programmer, and I've installed XAMPP with the intention of learning a bit of PHP. I have a working knowledge of SQL.
I've been following the PHP tutorial at w3schools. The problem I am currently having is this: I'm using the script here to create a database and a table within it. What I'm using is almost verbatim, except I've replaced the user with "root" and I've deleted the password.
After running the script through the browser, the database my_db appears in the datafile for mysql in XAMPP.
However, there is no sign of a table, and when I try to select the table, I get
Table 'my_db.persons' doesn't exist
What is going on? Is there something wrong with the code I took verbatim, or is it something with permissions?
It's weird that the database is created but not a table...
What has happenned is that your "CREATE TABLE" will have failed.
Get into the habit of checking for errors after EVERY mySQL query (in your code): there are some times you can ignore errors, but it's rare. So program alonghte lines of:
Create query: $sql =
Set parameters: $aParams = (or bind paramters)
Execute query
If errors
If debug: Show error and query
If live: Log error and query
I'm not giving the solution is code, as one problem with the tutorial you follow is that it uses mysql_() functions that are going to be depreciated shortly. You should use PDO (PHP Database Objects) or mysqli() functions otherwise your code will not work in a few releases time.
With PDO, you can set error handling to use exceptions, and you wrap every call in try {} catch {} and this makes the habit of catching and reporting errors very easy.
$sql = 'CREATE TABLE....';
$aParams = array(
':param_name' => $param_value,
':param_name2' => $param_value2
);
try {
$stmnt = $db->prepare($sql);
$stmnt->execute($aParams);
$stmnt = null;
} catch (Exception $e) {
// Error log here; $e contins line of error and the actual error, you have $sql and $aParams
LogDBError($e, $sql, $aParams);
}

JQGrid and Microsoft SQL Server

I am trying to get this awesome JQuery plugin working with SQL Server. I have setup PDO as I use it elsewhere (with MS Access) - I think I have it setup with sql server (2008) now. Here is how I connect:
define('DB_DSN',"odbc:Driver={SQL Server};Server=MyInstance;Database=table1;");
define('DB_USER', '');
define('DB_PASSWORD', '');
// include the jqGrid Class
require_once "php/jqGrid.php";
// include the PDO driver class
require_once "php/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
$tsql = 'SELECT * FROM Trt';
// Write the SQL Query
$grid->SelectCommand = $tsql;
// set the ouput format to json
$grid->dataType = 'json';
// Let the grid create the model
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('report-creator.php');
// Set grid caption using the option caption
$grid->setGridOptions(array(
"caption"=>"Report",
"rowNum"=>10,
"sortname"=>"OrderID",
"hoverrows"=>true,
"rowList"=>array(10,20,50),
));
$grid->toolbarfilter = true;
$grid->setFilterOptions(array("stringResult"=>true));
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;
I get this error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM001]: Driver
does not support this function: driver doesn't support meta data' in
C:\wamp\www\webs\tgd\edh\php\jqGridPdo.php(1) : eval()'d code:1 Stack trace: #0
C:\wamp\www\webs\tgd\edh\php\jqGridPdo.php(1) : eval()'d code(1): PDOStatement-
>getColumnMeta(0) #1 C:\wamp\www\webs\tgd\edh\php\jqGrid.php(1) : eval()'d code(7):
jqGridDB->getColumnMeta(0, Object(PDOStatement)) #2 C:\wamp\www\webs\tgd\Front- End\report-
creator.php(31): jqGridRender->setColModel() #3 C:\wamp\www\webs\tgd\Front- End\view-
report.php(123): require_once('C:\wamp\www\web...') #4 {main} thrown in
C:\wamp\www\webs\tgd\edh\php\jqGridPdo.php(1) : eval()'d code on line 1
I appreciate any help - I just can't understand the problem. I have narrowed it down to this function: $grid->SelectCommand - if I remove this no error occurs but I can not do without this as this is where my t-sql to query the database goes.
I am able to query SQL server mysql using PDQ->Query('SELECT * FROM table1').
Thanks all
Please note: I have edited table name, file paths for privacy. :)
Looks like jqGrid uses getColumnMeta function to enumerate columns, but this function may be unsupported by several drivers - in this case, your MSSQL driver.
See examples in the link above to test if getColumnMeta works.
I work for Trirand (the company that developers jqGrid - ASP.NET team) and unfortunately cannot help with PHP much, but I can suggest posting the very same question in our own forums as well:
http://www.trirand.net/forum/
there you will find a PHP jqGrid forum and you will get a timely response from our PHP team. Meanwhile I will write them an email and ask them to help here as well, but just in case posting there will guarantee you an answer.
Cheers,
Rumen Stankov
Trirand Inc
I couldn't solve this so I switched over to another class which was written by the JQGrid guys that makes use of the PHP driver from Microsoft. This solved my problem, hope it helps someone.

Categories