Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
hi..
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.
 
1)
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");
----
--?>
 
2)
$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
if (mysql_query("CREATE DATABASE customer",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }
mysql_select_db("customer", $con);
 
mysql_close($con);
?>
 
please help me out.
thanx in advance..
Posted 27-Jul-12 4:51am
Edited 27-Jul-12 5:29am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 19

use query
"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName'"
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 20

Hi there Smile | :)
 
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 Wink | ;) )
 
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 {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Richard Deeming 135
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2014
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