Page 1 of 1

Why can't I connect?

Posted: Thu Jul 24, 2008 11:47 am
by Chalks
I'm going crazy here. For some reason I can not connect to a new mysql database, and it's really aggravating. :banghead:


Here's what I'm trying to do:

Code: Select all

 
<?php
include 'databaser.php';
$d = new databaser;
 
$d->connect("localhost", "chalks_tester", "*******");
if($d->create("user", "name varchar(30), pass varchar(100)"))
  echo "yay";
else
  echo mysql_error();
?>
here's the relevant parts of databaser.php:

Code: Select all

<?php
class databaser {
private $db;
function connect($host="localhost", $user="username", $pass="pass")
{
  $this->db = mysql_connect($host, $user, $pass);
  mysql_select_db($this->db);
}
 
function create($table, $data)
{
  $table = mysql_real_escape_string($table);
  $data = mysql_real_escape_string($data);
  $sql = "CREATE TABLE IF NOT EXISTS $table (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), $data)";
  if($result = mysql_query($sql))
    return true;
  return false;
}
?>
When I run this, I get the error "No database selected".


if I try to just cut out the class and do it this way:

Code: Select all

<?php
$db = mysql_connect("localhost", "chalks_tester", "tester") or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
?>
I get the error "Access denied for user 'chalks_tester'@'localhost' to database 'Resource id #2'"
Which doesn't make sense because I KNOW that's the right username/password.


I know for a fact that I have the right username and password (I checked, rechecked, and triple checked).
I'm pretty sure I'm using the class function correctly.

Why in the world isn't this working? :(




EDIT: haha, I'm a moron. :( should be "mysql_select_db("database name"). Man.

Re: Why can't I connect?

Posted: Thu Jul 24, 2008 12:43 pm
by Dynamis
Yep

Code: Select all

mysql_select_db(DB_NAME, connection_reference)