Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.