The Serendipity Handbook

You can now read the (german) handbook here: PDF - https://github.com/s9y/Book (LaTeX source).

Forum-Information

Before posting about errors, make sure that the answer cannot already be found in our FAQ or by searching this forum!
Posting is restricted to registered users (registering is free and simple!) due to recent spam attacks. When having trouble with this board, contact garvin(-at)s9y(-dot)org.

Board index General discussions access s9y database

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
User avatar
Don Chambers
Regular
 
Posts: 3079
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA

Postby Don Chambers » Mon Jan 16, 2012 10:43 pm

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?

User avatar
Don Chambers
Regular
 
Posts: 3079
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA

Postby Don Chambers » Tue Jan 17, 2012 12:10 am

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?

User avatar
onli
Regular
 
Posts: 1071
Joined: Tue Sep 09, 2008 10:04 pm

Postby onli » Tue Jan 17, 2012 1:44 pm

Yes, omit the echo before serendipity_db_query.

User avatar
Don Chambers
Regular
 
Posts: 3079
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA

Postby Don Chambers » Tue Jan 17, 2012 5:14 pm

onli wrote:Yes, omit the echo before serendipity_db_query.


Any negative associated with that?

User avatar
Don Chambers
Regular
 
Posts: 3079
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA

Postby Don Chambers » Tue Jan 17, 2012 8:10 pm

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....

User avatar
Timbalu
Regular
 
Posts: 2692
Joined: Sun May 02, 2004 3:04 pm

Postby Timbalu » Tue Jan 17, 2012 9:07 pm

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

User avatar
Don Chambers
Regular
 
Posts: 3079
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA

Postby Don Chambers » Tue Jan 17, 2012 11:24 pm

I have a template that fakes plugin functionality (see 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.

User avatar
Timbalu
Regular
 
Posts: 2692
Joined: Sun May 02, 2004 3:04 pm

Postby Timbalu » Wed Jan 18, 2012 10:12 am

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



Return to General discussions

Who is online

Users browsing this forum: No registered users and 1 guest