0

I am very new to PHP so this is my first attempt to connect to a mysql database through a php file and I am getting this message. I dont know how much anyone can help me with this or if at least someone can guide me to a right direction

Can not use : soum_email1:

And my php looks like this

<?php
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!link){
    die('could not connect:' . mysql_error());
}

$db_selct = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
    die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
?>
soum
  • 1,131
  • 3
  • 20
  • 47
  • 2
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Apr 03 '13 at 18:20
  • 2
    You are assigning the `mysql_select_db()` function `$db_selct`, but then checking `$db_selected`. – BenM Apr 03 '13 at 18:20

2 Answers2

1

You are assigning the mysql_select_db() function $db_selct, but then checking $db_selected (which with the code you've posted is always falsey.

Also, link should be $link (on line 9).

Your code should be:

define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link){
    die('could not connect:' . mysql_error());
}

$db_selct = mysql_select_db(DB_NAME, $link);

if(!$db_selct){
    die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';

You should note though that the mysql_* family of functions are now deprecated, and you should consider using MySQLi or PDO.

BenM
  • 52,573
  • 26
  • 113
  • 168
  • @Ben-- this is amazing...you just did the magic. I see the mistakes and thanks for pointing out...I just made a successful connection. Thanks man – soum Apr 03 '13 at 18:28
0

First of all: Please use the pdo or mysqli database like Quentin wrote in the first comment.

Further you should name your variables right,

$db_selct = mysql_select_db(DB_NAME, $link);

and

 if(!$db_selected){
    die('Can not use : ' . DB_NAME . ':' .mysql_error());
}

have different variable names.

chill0r
  • 1,127
  • 2
  • 10
  • 26