Jump to content

problem storing array into mysql


Vartan

Recommended Posts

/* connect to MYSQL*/
$sqllink = mysqli_connect($mysql_host, $mysql_user, $mysql_pass);
if (!sqllink)
{
	echo "Unable to connec to Database";
	exit();
} else {echo "Connection OK<BR>";}

/* set char set*/
if (!mysqli_set_charset($sqllink, 'utf8'))
{
	echo "Unable to set database connection encoding.";
	exit();
} else {echo "Charset OK<BR>";}

/* select database */
if (!mysqli_select_db ($sqllink, $mysql_db))
{
	echo "unable to select database.";
	exit();
} else {echo "Database Ok<BR>";}


$sqltask = 'CREATE TABLE test (
						   id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
						   field1 text
						   ) DEFAULT CHARACTER SET utf8';
if (!mysqli_query($sqllink, $sqltask))
{
	echo "Error: ".mysqli_error($sqllink);
} 

$character['name'] = "Zek";
$character['age'] = 25;

$character = serialize($character);
$sqltask = 'INSERT INTO test VALUES($character)';

if (!mysqli_query ($sqllink, $sqltask))
{
	echo "mysqli_error($sqllink)";
	exit();
}
echo $sqltask;
?>

 

i get this error

Catchable fatal error: Object of class mysqli could not be converted to string in E:\xampp\htdocs\Dark Heresy\char_sheet.php on line 55

 

could any one clarify how do i pass an array into a Database?

 

thank you

-Vartan

Link to comment
https://forums.phpfreaks.com/topic/192850-problem-storing-array-into-mysql/
Share on other sites

Your not storing an array. Your storing a string representing an array (because it has been serialized). Not the approuch I would take, but anyway....

 

Firstly, variables are not interpolated within single quotes & secondly, string values need to be surrounded by quotes in sql.

 

$sqltask = "INSERT INTO test VALUES('$character')";

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.