468,512 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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

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.

5 1401
Luuk
1,043 Expert 1GB
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
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
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
Luuk
1,043 Expert 1GB
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
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.

Similar topics

9 posts views Thread by Bunty | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.