1

Hey fellas No idea what i'm doing wrong already tried all suggested idea on stackoverflow still not resolving error no database selected.

code:

<?php    
  include 'db_connect.php';    
  // create client table

    try {    
          $sql = 'CREATE TABLE client (
                  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                  name VARCHAR(40) NOT NULL,
                  phone INT NOT NULL,
                  email VARCHAR(60) NOT NULL,
                  address TEXT
          ) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB';

          $pdo->exec($sql);

  }
  catch(PDOException $e) {    
          $error = 'Error creating customer table: '.$e->getMessage();    
          include 'error.html.php';    
          exit();    
  }

  $output = "Customer table created succesfuly";    
  include 'output.php';   
?>

db_connect.php code:

<?php   
  $host = "localhost";
  $db = "hotelDB";
  $dbuser = "hotelAuth";
  $password = "+_90-w4903nsdkfn";    

  // connecting to DB using PDO

  try {    
      $pdo = new PDO("mysql:host = {$host}; dbname = {$db}", "{$dbuser}", "{$password}");    
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
      $pdo->exec('SET NAMES "utf8"');    
  }
  catch(PDOException $e) {    
      $error = "Error Occured While Connecting TO Database: ".$e->getMessage();    
      include 'error.html.php';    
      exit();    
  }    

  // On successful connection include booking form.    
    $ack = "Secure Connection established.";    
?>

you can see error in image. please help me resolve this. enter image description here

5
  • Note that it's important to follow the proper format - no fancy spaces or other decorations have to be used. Commented Feb 9, 2016 at 19:12
  • The same goes for the user and password which have to be addressed as just $dbuser, $password. Commented Feb 9, 2016 at 19:15
  • as of the include 'error.html.php'; - you should be doing it inthe error handler but not in the catch block. Commented Feb 9, 2016 at 19:16
  • 1
    If it isn't clear from earlier comments, you cannot have whitespace in the PDO DSN connection string. It must be "mysql:host=$host;dbname=$db". PDO stops parsing it at the first space and uses defaults, and that's what your problem is. Commented Feb 9, 2016 at 20:38
  • Note that the "{$dbuser}" and "{$password}" are unnecessarily quoted. Those are just method parameters, and can be passed as $dbuser, $password without any quotes. Commented Feb 9, 2016 at 20:39

1 Answer 1

1

It looks like your connection to your database is wrong, since the error message of no database selected where to create your table to. Here is how to connect with PDO:

<?php

    $dbuser = "hotelAuth";
    $password = "+_90-w4903nsdkfn";

    // connecting to DB using PDO
    try{

            $dbPDO = new PDO('mysql:host=localhost;dbname=hotelDB', $dbuser, $password);
            $dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $pdo->exec('SET NAMES "utf8"');
            echo "Connection was successful";

    } catch(PDOException $e){

            print "Error!: " . $e->getMessage() . "<br />";
            die();

    }

    // On successful connection include booking form.   
    $ack = "Secure Connection established.";

?>

Try that and see if it works for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.