Jump to content

MySqli Query


arunpatal

Recommended Posts

Hi, I am trying to create a table via foreach loop

$mysqli = "";
		$mysqli .= "mysqli_query($new_con,'CREATE TABLE IF NOT EXISTS $main_table(";
		
		
		foreach ($fields as $field_name => $field_value):
			@$field_type = @$type[$field_name];
			
		$mysqli .= "$field_value $field_type,"."<br>";
		
		endforeach;
		
		$mysqli .= "$fields[0] INT AUTO_INCREMENT PRIMARY KEY";
		$mysqli .= ")')";
		
		echo $mysqli;

But i am getting error

 

 Catchable fatal error: Object of class mysqli could not be converted to string in C:\wamp\www\My_Site\install\function.php on line 97

 

This is 97 line => $mysqli .= "mysqli_query($new_con,'CREATE TABLE IF NOT EXISTS $main_table(";

 

This all code is inside function

Link to comment
https://forums.phpfreaks.com/topic/286412-mysqli-query/
Share on other sites

inside of a double-quoted string, php variables, in this case your $new_con mysqli instance, are replaced with their content and an instance of mysqli has no meaning inside of a string and cannot be converted to a string.

the only part you should be building in the $mysqli variable is the actual (my)sql query statement. the code that is going to run that query would be separate and would use the resulting query statement that would be in the $mysqli variable -

// build the (my)sql query statement
$mysqli = "CREATE TABLE IF NOT EXISTS $main_table .....";
// run the query
mysqli_query($new_con,$mysqli);

 

Link to comment
https://forums.phpfreaks.com/topic/286412-mysqli-query/#findComment-1470065
Share on other sites

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.