Call to a member function bindParam() on a non-object - php

I'm working a log-in script that I found on the web. (http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/)
As I was trying to make it work, I stumbled upon this error:
bool(false)
Fatal error: Call to a member function bindParam() on a non-object in /srv/disk3/1446018/www/askmephilosophy.co.nf/session.php on line 25
My code:
<?php
include('config.php');
// Establishing the connection:
$MyConnection = mysqli_connect('hostname', 'user', 'pass', 'database')
or die('An error has occured when you were trying to login. You can return to the main page
by clicking: here. <br />
If this error is consistent, please contact us.');
// Information provide by the user:
$username = $_POST['username'];
$password = $_POST['password']; // Text version.
$StatementHandle = $MyConnection->prepare('
SELECT
hash
FROM Users
WHERE
username = :username
LIMIT 1
');
var_dump($StatementHandle);
$StatementHandle->bindParam(':username', $username);
$StatementHandle->execute();
$user = $StatementHandle->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash:
if(crypt($password, $user->hash) == $user->hash) {
echo 'You are logged in. Well, not actually. We only just confirmed that
our system works. This service\'ll cost you $1';
}
?>

your connection object is mysqli, you should use PDO to connect mysql like
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

Related

Dreamhost php sql error 'could not find driver'

I have made numerous attempts, most returning the odd error of 'could not find driver' when using PDO. I've confirmed multiple times with customer support that my credentials are correct. I've gone over the code looking for bugs, and found none. I built some simple tests, two examples below, one returning the error of 'Call to undefined function mysqli_connect()', the other returning the aforementioned ''could not find driver'.
I've been reading and googling and have yet to find a solution which is able to provide a working connection. Hoping for guidance on how to proceed
test 1: produces 'driver not found' error
$host = 'notmyHOST';
$db = 'notmyDATABASE';
$password = 'notmyPASSWORD';
$user = 'notmyUSERNAME';
$dsn = "mysqli:host=$host;dbname=$db;charset=UTF8";
try {
$pdo = new PDO($dsn, $user, $password);
if ($pdo) {echo "Connected to the $db database successfully!";}
} catch (PDOException $e) {
echo $e->getMessage();
}
//test 2: produces 'driver not found' error
//This test is a straight line-for-line copy of the example connect code
//provided by Dreamhosts knowledge-base
$hostname = "mysql.example.com"; //hostname created when creating the database
$username = "yourusername"; //username specified for database
$password = "yourpassword"; //password specified for database access
$database = "databasename"; //database name specified at creation
$link = mysqli_connect($hostname, $username, $password, $database);
if (mysqli_connect_errno()) {
die("Connect failed: %s\n" + mysqli_connect_error());
exit();
}
/*
test 3: produces 'Fatal error: Uncaught Error: Class "mysqli" not found in
/home/*****/*****/a___connectTEST4.php:25 Stack trace: #0 {main} thrown in
/home/*****/*****/a___connectTEST4.php on line 25' error
*/
$hostname = 'myHOST';
$database = 'myDATABASE';
$password = 'myPASSWORDS';
$username = 'myUSERNAME';
// Create connection
$conn = new mysqli($hostname, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
To recap, I'mooking for advice/solution/how to proceed to resolve connection error in any of the tests provided. In all tests I checked all credentials multiple times, they are correct; Haven't found a solution which maps to my problem on Stack Overflow, but I've looked at similar issues without success.

PHP PDO Insert Query Produces Fatal Error [duplicate]

I'm working a log-in script that I found on the web. (http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/)
As I was trying to make it work, I stumbled upon this error:
bool(false)
Fatal error: Call to a member function bindParam() on a non-object in /srv/disk3/1446018/www/askmephilosophy.co.nf/session.php on line 25
My code:
<?php
include('config.php');
// Establishing the connection:
$MyConnection = mysqli_connect('hostname', 'user', 'pass', 'database')
or die('An error has occured when you were trying to login. You can return to the main page
by clicking: here. <br />
If this error is consistent, please contact us.');
// Information provide by the user:
$username = $_POST['username'];
$password = $_POST['password']; // Text version.
$StatementHandle = $MyConnection->prepare('
SELECT
hash
FROM Users
WHERE
username = :username
LIMIT 1
');
var_dump($StatementHandle);
$StatementHandle->bindParam(':username', $username);
$StatementHandle->execute();
$user = $StatementHandle->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash:
if(crypt($password, $user->hash) == $user->hash) {
echo 'You are logged in. Well, not actually. We only just confirmed that
our system works. This service\'ll cost you $1';
}
?>
your connection object is mysqli, you should use PDO to connect mysql like
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

PDO Error I don't know how to fix

I have this error
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in /srv/users/wiput/apps/gallery/public/auth.php:56 Stack trace: #0 /srv/users/wiput/apps/gallery/public/auth.php(56): PDOStatement->execute() #1 {main} thrown in /srv/users/wiput/apps/gallery/public/auth.php on line 56
in con.inc.php
<?
$db_server = "localhost";
$db_user = "gallery";
$db_password = "<censored>";
$db_name = "gallery";
$conn = new PDO("mysql:server=$db_server;Database=$db_name",$db_user,$db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
in auth.php
<?
ob_start();
session_start();
//Global Variable
$username = $_REQUEST["username"];
$password = $_REQUEST["password"];
//Convert to MD5
$md5_pw = md5($password);
//Check Blank form
if($username == '')
{
$_SESSION["error"] = 2;
header("location:index.php");
}
elseif($password == '')
{
$_SESSION["error"] = 3;
header("location:index.php");
}
else
{
//Connect file
require("con.inc.php");
//Check data
$sql = "SELECT * FROM member WHERE username= :username AND password= :md5_pw ";
$result = $conn->prepare($sql);
$result->bindValue(':username', $username);
$result->bindValue(':md5_pw', $md5_pw);
$result->execute();
$data = $result->fetchAll( PDO::FETCH_ASSOC );
if ($data !== false)
{
echo 'Hi! ', $data['firstname'];
}
else
{
$_SESSION["error"] = 1;
header("location:index.php");
}
}
?>
I use serverpilot web server with PHP 5.6.
If any one can please fix it.
Thank you :)
As the error says, you don't have an active database selected. The reason is that your names in the DSN string is way off. In particular, Database should be dbname and server should be host (while the current value works because it defaults to localhost, probably - the dbname is what is giving you the error). Be sure to use the actual format and don't invent your own names.
See PDO_MYSQL DSN for the correct format.
To add more to this, this is because your database could not selected.
It could be various reasons.
Try this: $conn = new PDO("mysql:host=$db_server;dbname=$db_name",$db_user,$db_password);
rather than $conn = new PDO("mysql:server=$db_server;Database=$db_name",$db_user,$db_password); and see if it works as server is suppose to be host, and database is suppose to be dbname.
I usually connect to the database by doing this $conn ->exec('USE gallery;');

Call to a member function query() on a non-object on trying to read data

Hi I have been learning php from this book PHP Solutions Dynamic Web Design Made Easy and gotten to the part where I have to work with mysqli api for databases.After writing a connection function and running the script I get this error:
This is my code:
function dbConnect($usertype , $connectionType = 'mysqli'){
$host = 'localhost';
$db = 'phpsols';
if($usertype == 'read'){
$user = 'psread';
$pwd = 'Aleczandru1989';
}elseif($usertype == 'write'){
$user = 'aleczandru';
$pwd = 'Aleczandru1989';
}else{
exit('Unrecognized type');
}
if($connectionType == 'mysqli'){
return new mysqli($host , $user , $pwd , $db) or die ('Cannot open database');
}else{
try{
return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
} catch (PDOException $e){
echo 'Cannot connect to database';
exit;
}
}
}
$conn = dbConnect('read');
$sql = 'SELECT * FROM images';
$result = $conn->query($sql) or die(mysqli_error()); //Line 5
$numRows = $result->num_rows;
Line 5 in this case refers to $result = $conn->query($sql) or die(mysqli_error());.
What Am I doing wrong here?
The $conn object you are attempting to create with dbConnect('read'); fails. If you would do a var_dump($conn); it probably shows it is not what you aspect it to be. The error is actually describing what is wrong. You try to access the query function with '->query(..' on $conn. But $conn has to be an object reference that actually has the query function. The points where this object will be created are:
return new mysqli($host , $user , $pwd , $db)
and
return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
Since you are showing a different error then
or die ('Cannot open database');
My guess it is actually gong wrong at
return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
And you will catch the exception. But the echo statement is not visible anymore due to the fatal error. You will have to do some debugging there!
I have no experience with PDO, but construction of the object seems ok. (but can this help you out: http://nl1.php.net/manual/en/class.pdo.php#84751) If the construction is ok, than check if your database engine is actually running :) ?

report error Call to a member function bindParam() on a non-object on centos ,but ok on macOS [duplicate]

I'm working a log-in script that I found on the web. (http://alias.io/2010/01/store-passwords-safely-with-php-and-mysql/)
As I was trying to make it work, I stumbled upon this error:
bool(false)
Fatal error: Call to a member function bindParam() on a non-object in /srv/disk3/1446018/www/askmephilosophy.co.nf/session.php on line 25
My code:
<?php
include('config.php');
// Establishing the connection:
$MyConnection = mysqli_connect('hostname', 'user', 'pass', 'database')
or die('An error has occured when you were trying to login. You can return to the main page
by clicking: here. <br />
If this error is consistent, please contact us.');
// Information provide by the user:
$username = $_POST['username'];
$password = $_POST['password']; // Text version.
$StatementHandle = $MyConnection->prepare('
SELECT
hash
FROM Users
WHERE
username = :username
LIMIT 1
');
var_dump($StatementHandle);
$StatementHandle->bindParam(':username', $username);
$StatementHandle->execute();
$user = $StatementHandle->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash:
if(crypt($password, $user->hash) == $user->hash) {
echo 'You are logged in. Well, not actually. We only just confirmed that
our system works. This service\'ll cost you $1';
}
?>
your connection object is mysqli, you should use PDO to connect mysql like
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

Categories