My file corrupt when using db connection - php

I have a download script which get id of file and search in database and find it's name. But when I include my db connection the files get corrupted on download.
when I comment my db connection and give file name manually file downloading work fine.
I test my db connection and there wasn't any excpetion or any html output , what do you think my problem is?
<?php
session_start();
try{
$db= new PDO("mysql:host=localhost;dbname=dbname","user","pass");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$db->exec("SET NAMES 'utf8'");
}catch (Exception $e){
//echo "something wrong in db.php";
echo $e->getMessage();
exit;
}
?>
I run my code on windows server IIS, if it does matter

Just try with this code..
error_reporting(E_ALL);
ini_set('display_errors','1');
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
It will return your error.

after few days struggling and headache finaly problem resolved by deleting try catch block

Related

How can I check PDO connection?

Here is my code:
<?php
try{
// connect to database and select database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "spy";
$dbh_conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$dbh_conn->exec("set names utf8");
$dbh_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
My code works as well. But I seen a similar code which checks the connection like this:
if ( !$dbh_conn ){
// Something Went Wrong
} else {
// All Fine
}
Well do I need to this ^ condition? Or using try catch is enough to check db connection?
It depends on what you set for PDO::ATTR_ERRMODE. In your case, it's set as PDO::ERRMODE_EXCEPTION, so an error will throw an Exception.
$dbh_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
This error mode however is not applied when you connect (i.e., when __constructor() is called in PDO), since a PDOException is always thrown:
PDO::__construct() will always throw a PDOException if the connection fails regardless of which PDO::ATTR_ERRMODE is currently set. Uncaught Exceptions are fatal.
You can read about the different modes here.

Invalid data source names - PDO

So I've had this code running for PDO connection to my database.
Since last couple of hours, I'm getting a weird error called "invalid data source name".
I've searched quite a bit but I'm getting no solutions for it. What might be the reason?
Connection Code
<?php
$connectionString = 'mysqlhost=127.0.0.1;dbname=cdm';
try
{
$conn = new PDO($connectionString, 'root', 'PASS1234');
$conn->setAttribute(PDOATTR_ERRMODE, PDOERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
var_dump($conn);
?>
Output
invalid data source nameNULL
Your entire code is totally wrong.
first fix :
$connectionString = 'mysqlhost=127.0.0.1;dbname=cdm'; to $connectionString = 'mysql:host=127.0.0.1;dbname=dgsa';
Then change the following :
$conn->setAttribute(PDOATTR_ERRMODE, PDOERRMODE_EXCEPTION);
To :
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Full correct code :
<?php
$connectionString = 'mysql:host=127.0.0.1;dbname=dgsa';
try
{
$conn = new PDO($connectionString, 'root', 'PASS1234');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
var_dump($conn);
?>
I think you need to change the variable to this :
$connectionString = 'mysql:host=127.0.0.1;dbname=cdm';
I think it's just a typo
$connectionString = 'mysqlhost=127.0.0.1;dbname=cdm';
should be
$connectionString = 'mysql:host=127.0.0.1;dbname=cdm'; ( with : )

PDO MySQL connection troubleshooting

I'm running the following very basic PDO:MySql connection:
<?php
$host = '127.0.0.1:8888';
$db = 'communities';
$user = 'root';
$pass = 'yY5MF)q/DCzc';
// Create connection
try {
$conn = new PDO('mysqli:host=$host;dbname=$db', $user, $pass);
foreach($conn->query('SELECT * from city_detail') as $row) {
print_r($row);
}
$conn = null;
// Check connection
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
echo "Connected successfully";
?>
I've modified php.ini to allow for extension=php_pdo_mysql.dll with no effect. Is there something else that I'm missing. I am using PHP7.0.3. The error message output is Error!: could not find driver Kudos!
You need to use double quotes to get variable data inside string:
$conn = new PDO("mysqli:host=$host;dbname=$db", $user, $pass);
The problem was using msqli instead of mysql.

Can't Link To MySQL database

I am learning MySQL/PHP and I cannot figure out how to connect to MySQL on my localhost. I have written a short bit of code and I included root as my user name and root as my password because I have not set these elements yet (as far as I know). I feel that perhaps I am missing something in regards to the username/password combination. However, I feel that this should not be an issue because I have not tampered with the default conditions.
I need some help.
My code is below:
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=shirts4max;port=3306", "root", "root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf-8'");
} catch (Exception $e) {
echo "Could not connect to the database.";var_dump($e);
exit;
}
I am only seeing the error message on my page:
"Could not connect to the database."
Thank for reading. Please help me Obiwan.
Show the real error. Avoid using root except for maintenance.
From the PDO Manual page here, modified for you
<?php
$dsn = 'mysql:dbname=shirts4max;host=localhost';
$user = 'root';
$password = 'root';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Have you tried connecting via command line using: mysql -uroot -proot -hlocalhost -p3306? If you can't connect that way, the PDO connection won't go through either. Playing with the connection info via command line gives you a nice easy way to find what works, then include it in your code. Also, if you've not set the user/pw, it's possible there isn't one currently, so you wouldn't need those parameters at all.

MAMP localhost not Responding

I am trying to run php file using MAMP on my mac system having maverick operating system.
it was working good before but suddenly it stop responding.
when I run html file it work fine even when I run any .php file with out database PDO it work fine but when I try to run php file with PDO database connection, localhost does work.
any concrete suggestions. welcome !
<?php
try
{
$host ="localhost";
$db ="ijdb";
$user="ijdbuser";
$pwd = "ijdbuser";
$pdo = new PDO('mysql:host=$host; dbname = $db', $user, $pwd);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
$output = 'Database connection established.';
include 'output.html.php';
There are a few reasons as to why it's failing.
1) Variables do not get parsed in singles quotes
2) You have spaces; there should not be any.
'mysql:host=$host; dbname = $db'
//^ ^ ^
needs to read as:
"mysql:host=$host;dbname=$db"
^ ^
You may also want to use:
$pdo->exec("SET NAMES utf8");
Nota:
You should also modify your catch{...} to read as:
catch(PDOException $e) {
print $e->getMessage();
}
to get the real reason as to why it's failing.

Categories