Jump to content

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;
    }
  }
}

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.