Help with converting from using mysql to using mysqli_query

hello everyone

i am fairly new to php and welcome the help that you guys give to new comers.

i have two questions;

QUESTION ONE

i am trying to convert from using mysql to mysqli

the FUNCTION below works perfectly when used with mysql. but when i converted it to mysqli i keep getting the following error message;

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\database.com\cms\db_fns.php on line 123

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\database.com\cms\db_fns.php on line 125

i enlose the function



//[COLOR="#0000FF"]fuctions in mysql that work[/COLOR]


// first connect to database.


 function db_connect()
  {
		$connection = mysql_connect('localhost', 'database', 'password');
		
		if(!$connection)
    {
	    return false;
    }
    if(!mysql_select_db('database'))
    {
	    return false;
    }
		
		return  $connection;
  }

// second, extract information from database 


	function find_user($id)
	{
	   db_connect();
		
		$query =  sprintf("SELECT 
                         DOB_D, 
                         DOB_D, 
                         DOB_D, 
                         health, 
                         weight, 
                         height ,
			number_smoked
			 FROM 
			userr_details
			WHERE 
			user_id = '%s'	 
			   ", mysql_real_escape_string($id)
						);

		$result = mysql_query($query);
		
	  $row = mysql_fetch_array($result);
		
		print_r( $row);
	
	}
	
	echo find_user('7');
	
		

// [U][B]mysqli fuction[/B][/U]; 

// same fuctions as above, but now attempting to use mysqli rather than mysql


// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$dbc) {
	trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
}


// (not sure if this is correct procedure but now convert connection to a fuction- so that it can then be used within a function. 





  function db_connect()
  {
		global $dbc;
  	  
  	  $connection = $dbc;
		
if(!$connection)
    {
	    return false;
    }
    
    
    if($connection)
    {
	    
		return  $connection;
  }
	
  }
 

// below is the 'target' function- i am now using[COLOR="#FF0000"] mysqli[/COLOR] rather than[COLOR="#FF0000"] mysql //[/COLOR],  to extract the data from the database.- it no lonver works  it no longer works 



	function find_user($id)
	{
	  db_connect();
	
	  $query =  sprintf("SELECT 
                         DOB_D, 
                         DOB_D, 
                         DOB_D, 
                         health, 
                         weight, 
                         height ,
			number_smoked
			 FROM 
			user_details
			WHERE 
			user_id = '%s'	 
			   ",($id)
						);

		$result = mysqli_query($query);
		
	  $row = mysqli_fetch_array($result);
		
		print_r( $row);
	
	}
	
	echo find_user('7');

// [COLOR="#FF0000"]  THE ERROR MESSAGES [/COLOR]

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\\wamp\\www\\database.com\\cms\\db_fns.php on line 123

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\\wamp\\www\\database.com\\cms\\db_fns.php on line 125





QUESTION TWO

I use the function below to sort out an array of data that is extracted from the database. my problem however is that the fucntion is designed for mysql. however, i cant seem to locat the equivalent

mysql_fetch_array function.

does anyone have any ideas how i can convert this fucntion to mysqli



 

	
function db_result_to_array($result)
	{
		db_connect();
		
	  $res_array = array();
		
		for ($count=0;  $row = mysql_fetch_array($result) ; $count++)
		{
		 
			 $res_array[$count] = $row;
		}
		
		return $res_array;
	}
	



thank you very much everyone for your help

warm regards

Andreea 115

You posted this twice (a forum hickup maybe). I already answered you in the other thread: http://www.sitepoint.com/forums/showthread.php?794391-help-with-converting-from-using-mysql-to-using-mysqli_query.

I’m closing this one.