Gabriel Uhlíř
Gabriel Uhlíř

Reputation: 639

Mysql connect Error

With this code:

mysql_connect("mysql.webzdarma.cz", "octopus74", "*") or die ("Mysql connect Error>"); 
MySQL_Select_DB("octopus74") or die("Cant choose MySql database.");

It results in: "Mysql connect Error"

Upvotes: 1

Views: 18299

Answers (4)

improgrammer
improgrammer

Reputation: 582

Source : http://wallstreetdeveloper.com/php-database-connection/

I found a very useful code for connecting with mysql i posted below:

<?php
//Step-1 : Create a database connection
$connection=mysql_connect(“localhost”,”root”,”root”);
if(!$connection) {
    die(“Database Connection error” . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db(“widget_corp”,$connection);
if(!$db) {
    die(“Database Selection error” . mysql_error());
}
?>
<html>
<head>
<title>Database</title>
</head>
<body>
<?php
 //Step 3 : Perform database Queury
 $result=mysql_query(“select * from subjects”,$connection);
if(!$result) {
    die(“No rows Fetch” . mysql_error());
}

//Step 4 : Use returned data
while($row=mysql_fetch_array($result))
{
     //echo $row[1].” “.$row[2].”<br>”;
    echo $row["menu_name"].” “.$row["position"].”<br>”;
}

?>
</body>
</html>
<?php
//Step 5 : Close Connection
mysql_close($connection);
?>

Upvotes: 4

erKURITA
erKURITA

Reputation: 407

Open up the server's my.cnf and find this line:

#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1

If it's localhost (127.0.0.1) you won't be able to connect to it. Change it to 0.0.0.0 to allow the server to listen for external connections.

On the other hand, if it's 0.0.0.0 and you can't connect, check that:

  • Server is up (no laughing matter, I've seen these cases)
  • Service / Daemon is up
  • Port is open and you're connecting through the right one: it might have been reassigned.

If all else fails ... use fire and call your SysAdmin.

Upvotes: 0

all_by_grace
all_by_grace

Reputation: 2345

first are you sure that your mysql username and password are correct? The syntax for mysql connect is:

mysql_connect('your host server', 'mysql_username', 'mysql_password');

The syntax for mysql select db is:

mysql_select_db ('your_database_name');  

Are you sure that your mysql username and mysql database name is the same : "octopus74".

I would recommend to do in this way:

$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');  
if (!$conn) {  
    die('Not connected : ' . mysql_error());  
}  
// select db  
$db_selected = mysql_select_db('mydbname', $conn);  
if (!$db_selected) {  
    die ('Cannot use database mydbname : ' . mysql_error());  
}  

Upvotes: 2

Marc B
Marc B

Reputation: 360862

Change your die() calls to die(mysql_error()), which will output the ACTUAL reason for the error, which is of far more use than your fixed text.

Upvotes: 6

Related Questions