1

please, could someone help me what is my mistake? Creating a database works well but when I create a table it makes this SQLSTATE error[3D000]: Invalid catalog name: 1046 No database selected Here are the codes:

<? php
$server = 'localhost';
$login = 'root';
$password = '';
try{
  $connexion = new PDO('mysql:host = $server; dbname=test', $login, $password);
  $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $connexion->exec("CREATE TABLE utilisateur(nom varchar(50))");
  echo 'Réussi';
}
catch(PDOException $e){
  echo 'Echec de la connexion : '.$e->getMessage();
}
?>

1 Answers1

0

Just like the error says:

'No database selected'

Before you can create a table you must select which database it's being created in. If you're creating the table manually you'd login to your mysql server, and then in mysql server you'd type this:

USE dbname; 

Since you're likely looking to create the table within php you should already have logged into the mysql server under the database you want:

$connexion = new PDO('mysql:host = $server; dbname=test', $login, $password);

If that database does not exist already you need to create it using this command:

CREATE DATABASE dbname;
Kwright02
  • 777
  • 3
  • 21