Jump to content

[SOLVED] oop mysql problem


skkeeper

Recommended Posts

i'm just starting in php and oop, but im writting a little class to use in all my mysql querys.

 

Unfortanelly im not very sucessfull until now, here is why

 

My class

class mysql_connection
{
var $server = 'localhost';
var $username = 'root';
var $password = '';
var $database = 'test';

function execute($query)
{
	//Open Connection
	$link = mysql_connect($server,$username,$password);

	mysql_select_db($database);

	//Execute query
	echo(mysql_error($link).mysql_errno($link));
	$data = mysql_query($query,$link);

	//Close connection
	mysql_close($link);
}
}

 

My test file

include('oop_mysql.php');

$db = new mysql_connection;

$db->execute("INSERT INTO test_table VALUES('teste','teste2')");

 

Im getting the following error

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\oop_mysql.php on line 18

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\oop_mysql.php on line 20

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\wamp\www\oop_mysql.php on line 20

Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 23

Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 23

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 24

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 27

 

I'm completely stuck, im using wamp with all default configs i cant figure it out why is using the name ODBC instead of root just like i declare

 

regards and merry xmas

Link to comment
https://forums.phpfreaks.com/topic/83170-solved-oop-mysql-problem/
Share on other sites

You might want to take alook at how properties are accessed within classes in the oop section of the manual.

 

Also, your class has no error handling. I would set it up more like (assuming your using php4)....

 

<?php

class mysql_connection {
  var $server = 'localhost';
  var $username = 'root';
  var $password = '';
  var $database = 'test';
  var $link;
  var $err;

  function mysql_connection() {
    $link = mysql_connect($this->server,$this->username,$this->password);
    mysql_select_db($this->database);
  }

  function execute($query) {
    if ($data = mysql_query($query,$link)) {
      return $data;
    } else {
      $this->err = mysql_error();
    }
  }

  function geterr() {
    if ($this->err) {
      return $this->err;
    }
  }
}

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.