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';
?>
2
  • 2
    You are using an obsolete database API and should use a modern replacement. Commented Apr 3, 2013 at 18:20
  • 2
    You are assigning the mysql_select_db() function $db_selct, but then checking $db_selected. Commented Apr 3, 2013 at 18:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.