i have written a simple class for OOP in mysql and php and when i tested if even if the values that i used also in my normal connection process is the same the mysql won't connect me to it.. this is my class:

<?php

class DBlink{
	protected $link,$dbname,$uname,$passw,$db,$hname;
	
	function __construct($u='root',$p='',$d='web',$h='localhost'){
	    $this->uname = $u;
	    $this->passw = $p;
	    $this->db = $d;	    
	    $this->hname = $h;
		$this->link=mysql_connect($hname,$uname,$passw);
		$this->db = mysql_select_db($this->db);
	}
	
	function nonquery(){
		echo '<p>your name is ' . $this->hname .' '.$this->uname .' '. $this->passw .'</p>';	
	}
}

$a = new DBlink();
$a->nonquery();

unset($a);

?>

anyone who has an idea my it gives me an ODBC error?

anyone who knows what the scripts problem is?

is it throwing errors or something?

The error was with the protected variables that i included in the connection comand, i didn't put "$this->".. i changed it now to this and still im having problems with the function for queries..

<?php

class DBlink{
	protected $conn,$dbname,$uname,$passw,$db,$hname;
	
	function __construct($u='root',$p='',$h='localhost'){
	    $this->uname = $u;
	    $this->passw = $p;
	    $this->hname = $h;
	    $this->conn=mysql_connect($this->hname,$this->uname,$this->passw);
	}
	
	function seldb($d){
		$this->db = mysql_select_db($d);
	}
	
	function exequery($query){
		if (!$conn) {
        	die('Could not connect: ' . mysql_error());
    	}
    	$results = mysql_query($query, $conn);
    	mysql_close($conn);
    	return $results; 
	}
}

$a = new DBlink();
$s = $a->exequery();

unset($a);

i forgot the query within the function.. silly me..:)

Now i tried to execute a simple query but its not doing it even though, there are no errors

<?php

class DBlink{
	protected $conn,$dbname,$uname,$passw,$db,$hname;
	
	function __construct($u='root',$p='',$h='localhost'){
	    $this->uname = $u;
	    $this->passw = $p;
	    $this->hname = $h;
	    $this->conn=mysql_connect($this->hname,$this->uname,$this->passw);
	}
	
	function seldb($d){
		$this->db = mysql_select_db($d);
	}
	
	function exequery($query){
		if (!$this->conn) {
        	die('Could not connect: ' . mysql_error());
    	}
    	$results = mysql_query($query, $this->conn);
    	//mysql_close($this->conn);
    	return $results; 
	}
}

$a = new DBlink();
$s = $a->seldb("web");
$s = $a->exequery("delete * from employee");

unset($a);

?>

but its not doing it

Which part of the script isn't working ? Executing the delete query or establishing the connection ?

i think the connection is working. i used a delete query to know if the query function is working or not but when i checked the records nothing is deleted.

Return some value on execution of the delete query. Check if its returning the right value.

i added

if (mysql_num_rows($c) > 0) {
		echo "query success";
	}

to

$a = new DBlink();
$b = $a->seldb("web");
$c = $a->exequery("delete from employee");

if (mysql_num_rows($c) > 0) {
		echo "query success";
	}
	
unset($a);

And its giving me the error on the mysql_num_rows command

Dude, you have to be specific on the errors you get. What do you mean by 'error on mysql_num_row' ? What is the error ?

Try this.

unction exequery($query){
if (!$this->conn) {
die('Could not connect: ' . mysql_error());
}
echo $query;
exit;
$results = mysql_query($query) or die(mysql_error());
//mysql_close($this->conn);
return $results;
}

Check if its printing the query. Also in the class, check if database connection is established.

Thanks nav33n.. I think it's working now.. I change the the function query not to return anything since it's a delete query. did that caused the error? "Warning:mysql_num_rows(): supplied argument is not a valid mysql result resource in .... line 42

Thanks nav33n.. I think it's working now.. I change the the function query not to return anything since it's a delete query. did that caused the error? "Warning:mysql_num_rows(): supplied argument is not a valid mysql result resource in .... line 42

Ah! You are right ! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.