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
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
Share on other sites

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.