Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
C#
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)
C#
$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
Updated 27-Jul-12 5:29am
v2

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 {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
 
Share this answer
 
use query
SQL
"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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900