Click here to Skip to main content
12,886,892 members (38,339 online)
Rate this:
Please Sign up or sign in to vote.
See more: PHP MySQL
i have created database in mysql and i am trying to insert data in a table by using php.connection gets created but database doesn't gets detected(not able to select).I have tried to do this in two methods.

define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", "root"); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME","test_db"); // set database name
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");


$con = mysql_connect("localhost","","");
if (!$con)
  die('Could not connect: ' . mysql_error());
if (mysql_query("CREATE DATABASE customer",$con))
  echo "Database created";
  echo "Error creating database: " . mysql_error();
mysql_select_db("customer", $con);

please help me out.
thanx in advance..
Posted 27-Jul-12 4:51am
Updated 27-Jul-12 5:29am
Rate this: bad
Please Sign up or sign in to vote.

Solution 19

use query
to detect Database before creating database if database is EXISTS then why create that database every time you will visit or open that page. I would like to snuggest to use function
Rate this: bad
Please Sign up or sign in to vote.

Solution 20

Hi there :)

First I wanted to inquire if your DB is local or in the cloud (like this cloud database or AWS’ MySQL solution)? (you cannot be sure today ;))

If you are using one of the managed solutions, try looking in their guides or support sections. If you are managing your own local DB, then my recommendation is checking using CREATE database DDL with an [IF NOT EXISTS] argument. So when trying to detect your DB, you should try running the following query:
    [create_specification] ...

Using this script in your query should create a DB in case one does not exist and if a DB exists it should return no error. In such a case, try reconnecting to your DB (after re-checking you got all details right, oh how many times hours of trying to figure out what’s wrong with some script to find it was all due to a typo).

Hope this helped some, Diana

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100