Forum Moderators: coopster

Message Too Old, No Replies

connect mysql with php

         

yllai

1:16 am on Nov 25, 2007 (gmt 0)

10+ Year Member



Hi all,

I have hosted a webser (with mysql). In my mysql, I have create a database named 'member' and password is password. My host is example.com, port: 3306

So I tried connect my DB with the below script:
mysql_connect('example.com:3306', 'member', 'password');
if (!$link) {
die('Database Could not connect: ' . mysql_error());
} else{
echo 'Database Connected successfully';
}

I receive the message 'Database Could not connect'
Is it any wrong with my script? Anyone can help?

[edited by: eelixduppy at 3:44 am (utc) on Nov. 25, 2007]
[edit reason] removed specifics [/edit]

php4U

2:01 am on Nov 25, 2007 (gmt 0)

10+ Year Member



You can use something like the following. The first part will set all your info for your database, and the second part will select the database you are working with. It does look like you are missing your database username as well. You may need to check with your hosting company if you don't know what it is.

$host="example.com:3306"; // Host name
$username="your_username"; // Mysql username
$password="password"; // Mysql password
$db_name="member"; // Database name

$connection = mysql_connect("$host","$username","$password")or die("cannot connect");
$db = mysql_select_db("$db_name")or die("cannot select DB");

You can either put your direct values in place of the variables like you were doing with your code, or you can use it like this.

[edited by: eelixduppy at 3:45 am (utc) on Nov. 25, 2007]
[edit reason] removed specifics [/edit]

erased

10:53 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



I don't see where you set $link. $link should be the returned value of the mysql_connect function:


$link = mysql_connect('example.com:3306', 'member', 'password');
if (!$link) {
die('Database Could not connect: ' . mysql_error());
} else{
echo 'Database Connected successfully';
}

#Onto select the database with mysql_select_db()...