Error creating a Mysqli database via PHP on Wampserver

Asked

Viewed 1,909 times

4

I’m having trouble creating the database, on the net I found little content on, I only find how to build database by phpMyAdmin, but it is via script that I want to create.

I have the following code:

<?php

$mysqli = new mysqli("localhost","root","");

if($mysqli-> connect_error){
    echo "Não foi possível conectar.";
}

$create_db = "CREATE DATABASE 'my_db'";

if($mysqli->query($create_db)){
    echo "conexão bem sucedida";
}
else{
    echo "falha na conexão";
}

?>

Wampserver does not point out syntax errors. I always have the answer "connection failure", which is wrong?

  • 5

    You will actually access as a user root and no password? This is the root default? Does he really have the privilege to do anything in Mysql? This my_db does not exist? Tried to change the ' for \``? Você entende que aparece connection failureporque você mandou escrever quando a operação dá um erro? Então precisa ver qual o erro. Já experimentou usarmysqli::$error` to see what error message Mysql is returning?

  • Don’t worry about it! If you use charset=utf-8 on your page, even if in the database the accents do not appear on the page you call the data will appear the accents. Try to enter the data in the same way always so as not to give conflict. If by form always by form.

3 answers

9


Well, hitting the eye I saw some problems in your query.

  1. No need to put the name of the bank in simple quotes.
  2. If the database already exists, it will return an error as expected. You need to give a DROP otherwise it will not be possible to create it (don’t do this in production) or add IF NOT EXISTS in your query to avoid an error if the database is already created.

I changed your code by placing complete error messages when running the script.

<?php

// MySQLi usando try catch
mysqli_report(MYSQLI_REPORT_STRICT); 

try 
{
    $mysqli = new mysqli("localhost","root","");

    $mysqli->query("CREATE DATABASE IF NOT EXISTS my_db;");

    echo "Database criada com sucesso!";
} 
catch (Exception $e) 
{
    echo "Alguma coisa de errada aconteceu. Detalhes: " . $e->message;
}
  • Very good answer, and really Drop is a little dangerous, just a doubt, someone knows how to set the character type, in the case UTF-8 to the database not to have trouble with accents

  • vlw, I’ll research more on this

  • @leandro17br the answers of this question can help you

  • For the whole table: CREATE TABLE teste ...&#xA; id VARCHAR(45) ... DEFAULT CHARACTER SET = utf8

  • 1

    I found it curious that someone denied the other two answers (mine and another one already deleted) but not this one that makes use of exceptions in a bad way. It swallows all possible exceptions even if the desire is only to take the exception generated by Mysqli. Not that I think this deserves a negativation (after all it works, the only problem is that it is teaching a bad practice to those who are laymen in the subject), but it was incoherent on the part of those who negatived the others.

  • 1

    @bigown agree that the exception could be more specific, but as I do not use often mysqli I opted for Generica. My reply was also negative only that the downvote was withdrawn. On record, I had already voted positively in his reply: https://imageshack.com/i/pbaFPUnxp

  • 1

    @gmsantos I know I behavior on the site, you usually have coherence, no need to worry. And to make it clear, I did not deny anyone here, not even for a few minutes :) The exception question: it is not a big problem in simple examples, but it is always good to have this caveat.

Show 2 more comments

8

I created this and it worked. If you do the same and it doesn’t work, you have some problem with your installation or Mysql configuration. Just viewing the error provided by Mysql to be sure. But possibly this way not even happen the error. There is possibility of just being a syntax problem, I did not use the apostrophe in the name in the database.

<?php

$mysqli = new mysqli("localhost","root","uma_senha_aqui");

if($mysqli-> connect_error){
    echo "Não foi possível conectar.";
}

$create_db = "CREATE DATABASE IF NOT EXISTS my_db";

if($mysqli->query($create_db)){
    echo "criação ok";
}
else{
    echo "falha na criação:" . $mysqli->error;
}

?>

I put in the Github for future reference.

I had the $mysqli->error to get information to work. When you just have write that failed, you don’t know why it failed and it gets harder to solve any problem.

  • 5

    Those who have denied certainly know that there is something wrong in the answer. They could say what is wrong. If so I correct or even delete the answer.

-1

Just take out the simple quotes that will work normally, your code is coreto, with the exception of these quotes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.