0

Hi all,

I'm trying to insert an array (which I've decoded from a JSON object) into a mysql database (for the first time!) and keep coming back with a "Could not connect - Column count doesn't match value count at row 1" error message.

My server and database connections are working, but I do know that not all of the identified variables are populated with values in this record. Could that be the issue, and how do I get around it in this case?

Here's my code - it's dealing with the data at http://graph.facebook.com/131822850253006. I'm not sure whether the $event2 step is necessary. Any advice anyone could offer would be greatly appreciated! I'm sure I'm making a rookie error, but I've been looking for examples but can't seem to find what I need.

// start JSON decode

	$json = json_decode(file_get_contents('http://graph.facebook.com/131822850253006'));

	// define event array

	$event1 = array(
	'id' => $json->id,
	'owner_name' => $json->owner->name,
	'owner_cat' => $json->owner->category,
	'owner_id' => $json->owner->id,
	'event_name' => $json->name,
	'event_description' => $json->description,
	'start_time' => $json->start_time,
	'end_time' => $json->end_time,
	'location' => $json->location,
	'venue_street' => $json->venue->street,
	'venue_city' => $json->venue->city,
	'venue_state' => $json->venue->state,
	'venue_zip' => $json->venue->zip,
	'venue_country' => $json->venue->country,
	'venue_lat' => $json->venue->latitude,
	'venue_long' => $json->venue->longitude,
	'venue_id' => $json->venue->id,
	'privacy' => $json->privacy,
	'updated_time' => $json->updated_time,
	);

	print_r($event1);


$event2 = array(
	$event1[id],
	$event1[owner_name],
	$event1[owner_cat],
	$event1[owner_id],
	$event1[event_name],
	$event1[event_description],
	$event1[start_time],
	$event1[end_time],
	$event1[location],
	$event1[venue_street],
	$event1[venue_city],
	$event1[venue_state],
	$event1[venue_zip],
	$event1[venue_country],
	$event1[venue_lat],
	$event1[venue_long],
	$event1[venue_id],
	$event1[privacy],
	$event1[updated_time]
	);

	print_r($event2);


	// connect to database

	$database_connect = mysql_select_db('MY DATABASE', $connect);
	if (!$database_connect)
 	{
	die('Could not connect - database connection failed: ' . mysql_error());
	}


// Insert $event array into database

	$db_insert = mysql_query("INSERT INTO events (id, owner_name, owner_cat, owner_id, event_name, event_description, start_time, end_time, location, venue_street, venue_city, venue_state, venue_zip, venue_country, venue_lat, venue_long, venue_id, privacy, updated_time)
	VALUES ($event2)");

	if (!$db_insert)
	{
	die('Could not connect - event data insert failed: ' . mysql_error());
	}

Edited by JamesPublic: n/a

2
Contributors
2
Replies
8
Views
6 Years
Discussion Span
Last Post by JamesPublic
1
<?php	// start JSON decode

	$json = json_decode(file_get_contents('http://graph.facebook.com/131822850253006'));

	// define event array

	$event1 = array(
	'id' => $json->id,
	'owner_name' => $json->owner->name,
	'owner_cat' => $json->owner->category,
	'owner_id' => $json->owner->id,
	'event_name' => $json->name,
	'event_description' => $json->description,
	'start_time' => $json->start_time,
	'end_time' => $json->end_time,
	'location' => $json->location,
	'venue_street' => $json->venue->street,
	'venue_city' => $json->venue->city,
	'venue_state' => $json->venue->state,
	'venue_zip' => $json->venue->zip,
	'venue_country' => $json->venue->country,
	'venue_lat' => $json->venue->latitude,
	'venue_long' => $json->venue->longitude,
	'venue_id' => $json->venue->id,
	'privacy' => $json->privacy,
	'updated_time' => $json->updated_time,
	);



$event2 = '"'.implode(array(
	$event1['id'],
	$event1['owner_name'],
	$event1['owner_cat'],
	$event1['owner_id'],
	$event1['event_name'],
	$event1['event_description'],
	$event1['start_time'],
	$event1['end_time'],
	$event1['location'],
	$event1['venue_street'],
	$event1['venue_city'],
	$event1['venue_state'],
	$event1['venue_zip'],
	$event1['venue_country'],
	$event1['venue_lat'],
	$event1['venue_long'],
	$event1['venue_id'],
	$event1['privacy'],
	$event1['updated_time']
	),'","').'"';


	// connect to database

	$database_connect = mysql_select_db('MY DATABASE', $connect);
	if (!$database_connect)
 	{
	die('Could not connect - database connection failed: ' . mysql_error());
	}


// Insert $event array into database

	$db_insert = mysql_query("INSERT INTO events (id, owner_name, owner_cat, owner_id, event_name, event_description, start_time, end_time, location, venue_street, venue_city, venue_state, venue_zip, venue_country, venue_lat, venue_long, venue_id, privacy, updated_time)
	VALUES ($event2)");

	if (!$db_insert)
	{
	die('Could not connect - event data insert failed: ' . mysql_error());
	}
Votes + Comments
Your solution works perfectly cwarn - thank you, I'm off to read more about the implode function! :)
0

You champion cwarn23, that implode function works perfectly! I'm off to read more about it. Thank you - your "occupation" ("Genius") is well deserved :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.