I can not connect to mySQL database

Hi, I apologize in advance for my English.
I bought the fifth edition of the Italian version of the book by Kevin Yank “Build Your Own Website Using PHP And MySQL Database”.
I followed the guide in the appendix to manually install in Linux, I use Ubuntu 12.10, MySQL and PHP.
Understood to 4 explains how to connect to a database, created in advance, via PDO.
Shows the result of the following script:


<?php
	try
	{
		$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$pdo->exec('SET NAMES "utf-8"');
	}
	
	catch (PDOException $e)
	{
		$output = 'Impossibile connettersi al server di database: ' .
		$e->getMessage();
		include 'output.html.php';
		exit();	
	}
	
	$output = 'Connessione al database stabilita.';
	include 'output.html.php';

I get the following error:
Impossibile connettersi al server di database: could not find driver

How can I fix?

I would guess PDO is not installed or activated.

I found this on the web somewhere:


<?php
// Is PDO installed?
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO is unavailable<br/>';
}
elseif (defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO is available<br/>';
}

Running the script posted by you visualize this:
PDO is available

That is a supprise; I wonder if the mysql driver is missing or not setup.

To configure PHP I used the following command:

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli=/usr/local/mysql/bin/mysql_config

It is possible that I should recompile using

–with-mysql=/usr/local/mysql/bin/mysql_config

instead

–with-mysqli=/usr/local/mysql/bin/mysql_config

Could be a problem of phpMyAdmin

I just downloaded and extracted the tar.gz file from the site http://www.phpmyadmin.net/home_page/downloads.php and I extracted the directory on my localhost.

I am not sure as I have never had to do it but you are using Mysql on this line not mysqli:

$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword'); 

How do I use mysqli?

If I run this script:


<?php
				
					$mysqli = new mysqli("localhost", "ijdbuser", "mypassword", "ijdb");
					if ($mysqli->connect_errno) {
					    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
					}
					echo $mysqli->host_info . "\
";
					
					$mysqli = new mysqli("127.0.0.1", "ijdbuser", "mypassword", "ijdb", 3306);
					if ($mysqli->connect_errno) {
					    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
					}
					
					echo $mysqli->host_info . "\
";
					
				?>

I get the following result:

Localhost via UNIX socket 127.0.0.1 via TCP/IP

Hi All;

you can try this code…

<?php
// Create connection
$con=mysqli_connect(“example.com”,“peter”,“abc123”,“my_db”);

// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

thanks

That proves that you’re able to connect to the mysql server ok.

To get PDO going:

  1. Run this script
    php <?php phpinfo(); ?>
  2. With the output from the script in step 1 look for the line “Loaded Configuration File”
  3. Open up the file called php.ini that is located at the location given in “Loaded Configuration File”
  4. Find a line that reads
    > extension=php_pdo_mysql.dll
    there is probably a ; at the start of that line, remove the ; from the start of that line and save the file.
  5. Restart the server (whenever you make configuration changes to a server as a rule of thumb the server will normally need to be restarted for the changes to take affect).

You should now find that PDO is available for use

Thanks, but I need to connect to the database with PDO.

Thanks to you, I have already enabled PDO.
The strange thing is this.

Bro, use mysqli_connect. Also check your phpinfo() to check whether PDO is on or not.

This is the part related to PDO running:


<?php
	phpinfo();