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....
Connecting a MySQL database & creating & inserting values in a table via a php script
Collapse
X
-
Tags: None
-
Code:<?php // Connects to your Database $con = mysqli_connect("localhost", "root", "mysql@777") or die(mysqli_error()); // Create Database $sql="CREATE DATABASE mi_db"; if (mysqli_query($con,$sql)) { echo "Database mi_db created successfully"; } else { echo "Error creating database: " . mysqli_error($con); } $db_selected = mysqli_select_db('mi_db', $con); if (!$db_selected) { die ('Can\'t use mi_db : ' . mysqli_error()); } // Create table $sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT(11))"; // Execute query if (mysqli_query($con,$sql)) { echo "Table persons created successfully"; } else { echo "Error creating table: " . mysqli_error($con); }
Comment
-
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 suggestComment
-
Line#14 should be:
Code:$db_selected = mysqli_select_db($conn, 'mi_db';
Because of this error, the database is not selected, and the table cannot be created.Comment
-
Comment