Error while passing query to PDO - php

Whenever I pass a query after a PDO connection the page returns an 500 error. I don't know whats causing the issue. I am using LAMP server. Here is the code:
$user = 'root';
$database = 'mysql';
$password = 'root';
$dbname = 'pdotest';
$host = 'localhost';
$dsn = $database . ":" . $host . ";dbname=" . $dbname;
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
);
$pdo = new PDO($dsn, $user, $password, $opt) or die('Can\'t establish connection');
// This is where it gives 500 error
$stmt = $pdo->query("SELECT * FROM pdo");

Seems like I made a silly mistake. Just missed a host in dsn. Changing dsn from:
$dsn = $database . ":" . $host . ";dbname=" . $dbname;
To:
$dsn = $database . ":host=" . $host . ";dbname=" . $dbname;
solved my problem.

Related

What will be the hostname for ec2 instance phpmyadmin

I have setup an instance and now I want to connect my website with database. What would be the hostname If I want to connect to phpmyadmin. Will hostname be localhost?.I am using a following code:-
class Database{
// specify your own database credentials
private $host = "localhost";
private $db_name = "PHPLearning";
private $username = "root";
private $password = "";
public $conn;
// get the database connection
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
your code is correct. but it not work then change host from localhost to 127.0.0.1

Unknown host Connection Failure in PDO MySQL

Connection output failed I already change the quotations but still it failed
This is my code in config.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "db_airlines'";
try{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$db = new PDO('mysql:host= ' . $host . ';dbname=' . $db_name . ';charset=utf8', $username, $password);
echo $host;
}
catch(Exception $e){
echo "Failed to connect to database";
} ?>
failed output prompts: Warning: PDO::__construct():
php_network_getaddresses: getaddrinfo failed: No such host is known
but if I change it to $db = new PDO('mysql:host=localhost'; $username, $password); // this works.
Why is that?
$db = new PDO('mysql:host=' . $host . ';dbname=' . $db_name . ';charset=utf8', $username, $password);
The Space before your host name is causing this issue.

PHP - PDO SQLSRV - Connection string is not valid [87]

I´m trying to connect to a SQL Server 2008 R2 via PHP. I used the following code:
try {
$server = "servername.do.ma.in\\MSSQLSERVER";
$user = "username";
$pass = "password";
$db = "databasename";
$dbh= new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Verbindung fehlgeschlagen: ' . $e->getMessage();
}
But when i run this code i get the following error:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87].
Does someone know this error and know what to do?
Thanks in advance.
Solution:
The Port was not defined. The following code worked for me.
I´ve added the Port 1433 to the $server Variable.
$server = "servername.do.ma.in, 1433";
$user = "username";
$pass = "password";
$db = "databasename";
$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);
This will work:
$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);
If it's still not valid, there's something wrong with your connection variables.
EDIT:
This looks similar to your problem:
http://laravel.io/forum/01-20-2015-sql-server-2012-db-connection-with-laravel-42

Open Shift Mysql data connection using php

I am fresher for Open shift.
Here is my problem in Open shift.
I completed creating application with php, Mysql and PhpMyAdmin
Then I went to phpMyAdmin and created a new database 'Students_list'
Now I am unable to connect to my database with the following code in php pages.
$db_host = $_ENV['127.12.123.2']; //sample host
$db_user = $_ENV['adminnxUpXA6'];
$db_pass = $_ENV['lyPfbtHYpDFcU'];
$db_name = $_ENV['students_list']; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
May I please know where I am comitting the error.
Try this instead:
$db_host = getenv('OPENSHIFT_MYSQL_DB_HOST'); //sample host
$db_user = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
$db_pass = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
$db_name = 'students_list'; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
You are using $_ENV, which contains environment variables from the operating system. Most probably what you wanted to do is this:
$db_host = 'ip';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'students_list';
The structure for the mysqli connection string is this:
mysqli(Hostname,Username,Password,DatabaseName);

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

<?php
$dbtype = 'mysql';
$mysql_host = "localhost";
$mysql_database = "mydb";
$mysql_user = "root";
$mysql_password = "";
try {
$db = new PDO ( $dbtype . ':host=' . $mysql_host . ';dbname=' . $mysql_database, $mysql_user, $mysql_password, array (PDO::ATTR_PERSISTENT => true ) );
return $db;
} catch ( PDOException $e ) {
return false;
}
?>
Above are my db.php
<?php
require db.php";
...
global $db;
$stmt = $db->prepare ( "INSERT INTO quote (title, contactname) VALUES (:a, :b);" );
I actually able to run it properly under my IDE (ZendStudo wamp server) but once i upload to host server i got this error. Anybody facing the same problem before can guide me how to fix?
There can me many reasons, but one obivious is that is you had a PDOException, then $db == false, which would explain the error message.
Try to check if $db is correctly set as a PDO or false.
PDO does not throw exceptions unless you explicitly instruct it to do so:
$db = new PDO(...);
$db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Categories