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;');
Related
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();
}
This code was working before. Now it doesn't when I try to sign in. This is the error which I get.
Fatal error:
Uncaught exception'PDOException' with message 'SQLSTATE[42S02]: Base
table or view not found: 1932 Table 'mhaonlineshop.users' doesn't
exist in engine'........ DO->query('select * FROM t...') #1 {main}
thrown in H:\Aston University\New folder\htdocs\xampp\login.php on
line 23((this line has this code = $result = $db->query($query);))
I would very much appreciate what is going on how it can be resolved. thank you.
<?php
session_start();
include ("db_connect.php");
include("shared.php");
if (isset($_POST['submitted'])){
$username = $_POST['ID'];
$password = $_POST['password'];
$db = new PDO("mysql:dbname=mhaonlineshop;host=localhost", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "select * FROM users where ID= '$username' AND password = '$password'";
$result = $db->query($query);
if ($result -> rowCount()==1){
$firstrow = $result->fetch();
$_SESSION['ID'] = $firstrow[0];
$_SESSION['money'] = $firstrow[6];
$_SESSION['admin'] = $firstrow[5];
if ($_SESSION['admin'] == "0"){
header("Location: mhaonlineshopcatalogue.php");
}else if ($_SESSION['staff'] == "1"){
header("Location: cart.php");
}
} else {
echo "<h1>Please enter correct password</h1>";
}
}
?>
Apart from serious security issues in your code, the above message states that the table users is not present in your database. Did you check the database?
Concerning the security issues: Imagine what would happen, when a user enters
' OR password LIKE '%
as user name or password...
...you should never use unsanitized post var strings like that in a query. (And also not store plain passwords to the database; consider using hashed passwords.)
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();
}
I have a simple project and that is to create a function that will check for mysql and odbc connection. I'm already done in creating the function for mysql, here's my sample code:
function check() {
$serverName = 'localhost';
$userName = 'root';
$password = '123';
$db = 'sample';
$conn = mysql_connect($serverName, $userName, $password);
mysql_select_db($db, $conn);
$trans = 'SELECT * FROM Labels';
$trans_result = mysql_query($trans, $conn);
if(!$trans_result) {
die(mysql_error());
} else {
echo "connected";
}
}
Well this one works for me when checking for the mysql connection. Now, my question is, is it possible to create something like this for checking my odbc data source connection? So that would be like
$conn = odbc_connect("spmuse1","" ,""); # Open connection.
$trans = "SELECT French FROM Labels";
$trans_result = odbc_exec($conn, $trans);
if(!$trans_result) {
echo "error?";
} else {
echo "connected";
}
You know what I mean? When I use this code, I always have 2 this error
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect
Warning: odbc_exec(): supplied argument is not a valid ODBC-Link resource
Please help! Thanks.
First you need to decide vendor of odbc driver, I hope below example will works for you
<?php
// Configure connection parameters
$db_host = "server.mynetwork";
$db_server_name = "Dev_Server";
$db_name = "Dev_Data";
$db_file = 'c:\dbstorage\dev.db';
$db_conn_name = "php_script";
$db_user = "dbuser";
$db_pass = "dbpass";
$connect_string = "Driver={Adaptive Server Anywhere 8.0};".
"CommLinks=tcpip(Host=$db_host);".
"ServerName=$db_server_name;".
"DatabaseName=$db_name;".
"DatabaseFile=$db_file;".
"ConnectionName=$db_conn_name;".
"uid=$db_user;pwd=$db_pass";
// Connect to DB
$conn = odbc_connect($connect_string,'','');
// Query
$qry = "SELECT * FROM my_table";
// Get Result
$trans_result= odbc_exec($conn,$qry);
if(!$trans_result) {
echo "error?";
} else {
echo "connected";
}
?>
I spent several days looking for a simple answer, and came up with this, which works for me:
if (#odbc_connect("DBName","un","pw",SQL_CUR_USE_ODBC) == FALSE){
echo "Database does not exist";
} else {
$connection=odbc_connect("DBName","un","pw",SQL_CUR_USE_ODBC);
echo "Database exists";
}
The # suppresses the basic error if the database does not exist, so the connection try will just return false. Of course if the connection is good, then it creates the connection object.
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();
}