Jump to content


Photo

Class for connecting to mysql


  • Please log in to reply
6 replies to this topic

#1 sngskunk

sngskunk
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 June 2006 - 07:37 PM

Im trying to make a class to connect to my mysql database but for some reason its not working cand you tell me what i need to do.

<?php

class DB_Connect {
  
  var $server;
  var $username;
  var $password;
  var $database;
  
  function db_connect() {
    
    $dbserver = $this->server;
    $dbuser = $this->username;
    $dbpass = $this->password;
    $db = $this->database;
    
    $connect = mysql_connect($dbserver, $dbuser, $dbpass);
    mysql_select_db($db, $connect);    
    
  }
}

$db = new DB_Connect;
$db->server = "localhost";
$db->username = "user";
$db->password = "pass";
$db->database = "database";
$db->db_connect();

?>

I get this error:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files (x86)\xampp\htdocs\portal\new\test.php on line 17

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Program Files (x86)\xampp\htdocs\portal\new\test.php on line 18

But the server, username, password, and database are correct. Help???

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 08 June 2006 - 12:19 AM

I think you need a constructor function that will assign the values to your class variables.

The constructor function in php 4 must be the same name as the class "function DB_Connect()"

in that function assign the values of the $server.......

NoGray.com


#3 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 08 June 2006 - 12:30 AM

Or you can use: ___constructor()

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 08 June 2006 - 12:58 AM

Although the code could be better, it should work. Constructors are not needed, but they may be useful.

Actually it's the constructor that is causing the problem.
The constructor is called as soon as the class is initiated, not allowing you to set the server, username, etc.

Try to rename the method to "connect" and it should work.
~ D Kuang

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 08 June 2006 - 02:45 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Actually it's the constructor that is causing the problem.[/quote]
There is no constructor in the code posted, php is case sensitive.

The fact is this class is working fine. You seem to have a permissions problem. Are you sure the username and password are correct?

#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 08 June 2006 - 02:51 AM

Oddly enough, it works here as constructor.

OS related maybe?
~ D Kuang

#7 sngskunk

sngskunk
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 08 June 2006 - 03:28 AM

Okay i changed $server to $address and db_connect() to connect().

What would be the best way to write this poirot (any help is great).

Thanks for the help!!!!!!!!!!!!!!!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users