By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

Connecting a MySQL database & creating & inserting values in a table via a php script

P: 17
when i try to connect php script to MySQL for creating & inserting values in a table i get an error msg: Error creating database: Can't create database 'mi_db'; database existsAccess denied for user ''@'localhost' to database 'mi_db'. I am running the php via a wamp server...root directory - www and my database name is mi_db. Please suggest possible ways to resolve....
Dec 28 '13 #1

✓ answered by Luuk

Line#14 should be:
Expand|Select|Wrap|Line Numbers
  1. $db_selected = mysqli_select_db($conn, 'mi_db';
see: http://www.php.net/manual/en/mysqli.select-db.php

Because of this error, the database is not selected, and the table cannot be created.

Share this Question
Share on Google+
5 Replies


Expert 100+
P: 1,035
Technically the topic 'mysql' is wrong, it has more to do with 'php', especially when you write that the database exists.

Can you post your PHP code here? (use '[code]' tags..)
Dec 28 '13 #2

P: 17
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  // Connects to your Database 
  3.  $con = mysqli_connect("localhost", "root", "mysql@777") or die(mysqli_error());
  4. // Create Database 
  5.  $sql="CREATE DATABASE mi_db";
  6.  if (mysqli_query($con,$sql))
  7.   {
  8.   echo "Database mi_db created successfully";
  9.   }
  10.   else
  11.   {
  12.   echo "Error creating database: " . mysqli_error($con);
  13.   } 
  14.  $db_selected = mysqli_select_db('mi_db', $con);
  15. if (!$db_selected) {
  16.     die ('Can\'t use mi_db : ' . mysqli_error());
  17. }
  18. // Create table
  19. $sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT(11))";
  20. // Execute query
  21. if (mysqli_query($con,$sql))
  22.   {
  23.   echo "Table persons created successfully";
  24.   }
  25. else
  26.   {
  27.   echo "Error creating table: " . mysqli_error($con);
  28.   } 
  29.  
  30.  
Dec 29 '13 #3

P: 17
There is no problem creating database mi_db but when I try to select the mi_db to create a table and to insert values into it..i get that error msg..please suggest
Dec 29 '13 #4

Expert 100+
P: 1,035
Line#14 should be:
Expand|Select|Wrap|Line Numbers
  1. $db_selected = mysqli_select_db($conn, 'mi_db';
see: http://www.php.net/manual/en/mysqli.select-db.php

Because of this error, the database is not selected, and the table cannot be created.
Dec 29 '13 #5

P: 17
Great..thanks for the answer..
Dec 29 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.