SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?php
        
    try
        {
            
    $pdo = new PDO('mysql:host=localhost;dbname=ijdb''ijdbuser''mypassword');
            
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::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?

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,189
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    I would guess PDO is not installed or activated.


    I found this on the web somewhere:
    PHP Code:
    <?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/>';
    }

  3. #3
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    I would guess PDO is not installed or activated.


    I found this on the web somewhere:
    PHP Code:
    <?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

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,189
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    That is a supprise; I wonder if the mysql driver is missing or not setup.

  5. #5
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,189
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    I am not sure as I have never had to do it but you are using Mysql on this line not mysqli:

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

  8. #8
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I use mysqli?

  9. #9
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I run this script:

    PHP Code:
    <?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 "\n";
                        
                        
    $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 "\n";
                        
                    
    ?>
    I get the following result:

    Localhost via UNIX socket 127.0.0.1 via TCP/IP

  10. #10
    SitePoint Member
    Join Date
    Feb 2013
    Location
    new delhi
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  11. #11
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,903
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheCop View Post
    If I run this script:

    PHP Code:
    <?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 "\n";
                        
                        
    $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 "\n";
                        
                    
    ?>
    I get the following result:

    Localhost via UNIX socket 127.0.0.1 via TCP/IP
    That proves that you're able to connect to the mysql server ok.

    To get PDO going:


    1. Run this script
      PHP Code:
      <?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
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  12. #12
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pssingh1001 View Post
    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
    Thanks, but I need to connect to the database with PDO.

  13. #13
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    That proves that you're able to connect to the mysql server ok.

    To get PDO going:


    1. Run this script
      PHP Code:
      <?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 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 to you, I have already enabled PDO.
    The strange thing is this.

  14. #14
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bro, use mysqli_connect. Also check your phpinfo() to check whether PDO is on or not.

  15. #15
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by serverack View Post
    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 Code:
    <?php
        phpinfo
    ();
    pdo.png


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •