Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php

class database {

	public $sql = '';
	public $query = '';
	public $con;
	function __construct(){
		self::db();
	}	
	
	static function db()
	{	
		
		if($_SERVER['HTTP_HOST'] == 'localhost'){
	
			$host = 'localhost';
			$username = 'root';
			$pass = '';
			$db = 'alltoitskin';
		
		}else{
			
			$host = 'alltoitskin.db.7649825.hostedresource.com';
			$username = 'alltoitskin';
			$pass = 'Skinlogica123';
			$db ='alltoitskin';
		}
		
		$con=mysqli_connect($host, $username ,$pass)or die("mysql not connected");				
		mysqli_select_db($con,$db) or die('Invalid database selected');


		
	}
	
	function result_array($query){
			
			$this->query = $query;
			self::db();
									
		$result = array();
		$Q=mysqli_query($con,$query);
		
		if($Q && mysqli_num_rows($Q) > 0){
			while($tmp = mysqli_fetch_assoc($Q)){
				$result[] = $tmp;
			}
		}		
		return $result;
		
	}
	
	
	function result_row($query){
			
			$this->query = $query;
			self::db();		
		
		$result = array();
		//$Q=mysql_query($query);
		$Q=mysqli_query($con,$query);
		
		if($Q && mysqli_num_rows($Q) > 0){
			$tmp = mysqli_fetch_assoc($Q);
			return $tmp;			
		}
		
		return $result;
		
	}
	
	
	function insert($data = array(), $table = ''){		
		$t = sizeof($data);		
		if(empty($data) || $table == ''){
			return false;
		}
			
		$this->sql = 'INSERT INTO '.$table.' SET ' ;
		$i = 1;
		foreach($data as $k => $v){
			$this->sql .= $k." = '".$v."' ";
			
			if($i < $t ){
				$this->sql .= ' , ';
			}
			$i++;			
		}
		
		//echo $this->sql;
		
		$Q = mysql_query($this->sql);
				
		if($Q){			
			return mysql_insert_id();
		}
		
		return false;			
	}
	
	
	function num_rows($query){
	
		$this->query = $query;
		self::db();
		
		$result = array();
		$Q=mysql_query($query);
		
		if($Q){
			return mysql_num_rows($Q);
		}
		return 0;
		
	}
	
	function update($query){
		
		$this->query = $query;
		$Q = 	mysql_query($query);
		
		if($Q && mysql_affected_rows() > 0 ){
			return true;
		}
		
		return false;
	}
	
	function last_query(){
		return $this->query;
	}
	

}
?>


What I have tried:

Notice: Undefined variable: con in C:\xampp\htdocs\grip-engineering\classes\database.php on line 62

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\grip-engineering\classes\database.php on line 62
Posted
Updated 18-Apr-17 14:25pm
Comments
Nick_3141592654 18-Apr-17 7:57am    
Next time you post such a question please identify the specific line number in your source where the error occurs. Expecting people to count lines to find where #62 is, is asking rather a lot!

Anyway, I did count lines and the answer is in your question:

mysqli_query() expects parameter 1 to be mysqli, null given

The null variable in question is $con. This means that your DB connection was not established.

I would advise you to get a debugger (Netbeans works well) and set a breakpoint in your db() function. For server-side PHP development a debugger is an essential tool so I strongly encourage you to make the investment to learn how to use one, if you don't already know. Even if you can find an easy answer to this issue you need to be able to debug effectively yourself.
Member 13135459 18-Apr-17 8:22am    
i removed static keyword but it is giving the same error
ZurdoDev 18-Apr-17 8:05am    
Also post an appropriate title.

Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 
You have declared your function db() as static. Static functions have no access to member variables or non-static member functions. So the variable $con used within the static function is not the class member variable but a local variable and the class member variable stays unitialised.

Solution: Remove the static keyword.

[EDIT]
You should also declare the variables in your db() function at function level scope and not within conditional scope and check if the connection was successful:
PHP
$host = 'alltoitskin.db.7649825.hostedresource.com';
$username = 'alltoitskin';
$pass = 'Skinlogica123';
$db ='alltoitskin';
if($_SERVER['HTTP_HOST'] == 'localhost'){
    $host = 'localhost';
    $username = 'root';
    $pass = '';
    $db = 'alltoitskin';
}
[/EDIT]
 
Share this answer
 
v2
Comments
Member 13135459 18-Apr-17 8:23am    
i removed static keyword but it is giving the same error
Jochen Arndt 18-Apr-17 8:30am    
Then the connection might have failed ($con is false).
Add a check fro that to your code.
And declare the used variables in db() within function scope:

$host = 'alltoitskin.db.7649825.hostedresource.com';
$username = 'alltoitskin';
$pass = 'Skinlogica123';
$db ='alltoitskin';
if($_SERVER['HTTP_HOST'] == 'localhost'){
/* set for localhost here */
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900