Jump to content

can't connect?


gsquare567

Recommended Posts

Could not connect: Access denied for user 'ODBC'@'localhost' (using password: NO)

 

<?php
$con = mysql_connect("localhost");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>

 

i also tried localhost:3306 and no paramaters but same error =S any1 know how to fix that?

Link to comment
https://forums.phpfreaks.com/topic/48870-cant-connect/
Share on other sites

<?php

    $dbhost   = 'localhost';
    $dbname   = 'db';
    $dbusername   = 'username';
    $dbuserpass = 'password';    
   
mysql_select_db($dbname) or die('Cannot select database');

$con = mysql_connect ($dbhost, $dbusername, $dbuserpass);

if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>

this should work

 

Link to comment
https://forums.phpfreaks.com/topic/48870-cant-connect/#findComment-239523
Share on other sites

First, since the original poster is a newbie, I wouldn't recommend using PHP to create new databases and tables.  phpMyAdmin is far simpler in that regard.

 

Second, a database connection script typically looks like the following:

<?php

/* dbconnect.php -- database connection script */

DEFINE ('HOST', 'hostName');
DEFINE ('USER', 'userName');
DEFINE ('PASSWORD', 'password');
DEFINE ('MYDB', 'dbName');

$dbc = mysql_connect(HOST, USER, PASSWORD) or DIE('Could not connect to the database: ' . mysql_error());
mysql_select_db(MYDB, $dbc) or DIE('Could not select the database: ' . mysql_error());

?>

 

For security reasons, you should stick this script outside of your public_html folder.  You can access it by simply doing the following:

require('../dbconnect.php');

 

You should only include it when you're going to be accessing the database.

Link to comment
https://forums.phpfreaks.com/topic/48870-cant-connect/#findComment-239914
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.