access s9y database

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

access s9y database

Post by Don Chambers »

I had the need to add a batch of rows to one of the s9y tables. php and mysql are not really my thing, but I did accomplish my objective with this:

Code: Select all

<?php

$con = mysql_connect("localhost","my_db_username","my_db_password");
if (!$con)  {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db_name", $con);

for ($i = 1; $i < 2672; $i++) {
	$sql="INSERT INTO s9y_entryproperties (entryid, property, value) VALUES($i, 'ext_body_access', '5^3^2^1')";
	if (!mysql_query($sql,$con)) {
		die('Error: ' . mysql_error());
	}
}

echo "Record(s) added";

mysql_close($con)
?>
Was there a better/easier way to do it? Plug into s9y which already knows things like the server, database user, password, etc?
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: access s9y database

Post by Don Chambers »

I figured it out:

Code: Select all

<?php
include 'serendipity_config.inc.php';
for ($i = 1; $i < 2672; $i++) {
	echo serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entryproperties (entryid, property, value) VALUES($i, 'ext_body_access', '5^3^2^1')");
}

echo '<br /> ' , ($i-1), " record(s) added";
?> 
Each cycle through the loop echos a "1" to the screen. Is there a way to suppress that?
=Don=
onli
Regular
Posts: 3044
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: access s9y database

Post by onli »

Yes, omit the echo before serendipity_db_query.
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: access s9y database

Post by Don Chambers »

onli wrote:Yes, omit the echo before serendipity_db_query.
Any negative associated with that?
=Don=
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: access s9y database

Post by Don Chambers »

Don Chambers wrote:
onli wrote:Yes, omit the echo before serendipity_db_query.
Any negative associated with that?
Works beautifully... I thought removing the "echo" would suppress error messages, but it does not. If I actually wanted to hide error messages, that would have been INSERT IGNORE INTO....
=Don=
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: access s9y database

Post by Timbalu »

But...,
.... Don ....,
you still know you are trying to loop this INSERT INTO statement 2672 times! :shock:
Is that really what you wanted to do? Or is it just a test to kill your system? :wink:
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Don Chambers
Regular
Posts: 3657
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: access s9y database

Post by Don Chambers »

I have a template that fakes plugin functionality (see http://board.s9y.org/viewtopic.php?f=5&t=14984).

It works by storing values for custom properties in the entryproperties table. I added it to an existing site which has 2600+ existing entries. For those entries to have these properties, I either have to edit each entry one at a time, or simply populate the entryproperties table with these values.
=Don=
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: access s9y database

Post by Timbalu »

Yes, I already thought you were trying something like this.
And I remember this mentioned thread as keeping some very interesting possibilities.
Again, I am still waiting for more cool ideas how to use it for common or special problems.

...for your code it might be better to gather the INSERT INTO statement into an array, like
$hold[] = "INSERT INTO...";
and execute the array afterwards once with
serendipity_db_query($hold);
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Post Reply