Dustin013 Posted May 28, 2008 Share Posted May 28, 2008 I have two fields I need to work within a database for Wordpress. I have imported a lot of data in but I need to modify one of the fields but I am not exactly sure how its done. I have an ID field which is auto incrementing and contains the correct values. I have another field which is called guid which is currently blank. I need to insert data into all 4000+ cells located in the wp_posts table that have a "guid" cell. I need the following to be inserted into each GUID field based on the ID of the row being edited... so for instance, if the row I am editing has a ID of 40 than I need to insert the value "http://www.dailyddl.com/?p=40" into the GUID field. I need to connect to the database "wordpress", go to the table "wp_posts" and modify each GUID cell to read "http://www.dailyddl.com/?p=(POST ID # HERE). Any suggestions or tutorials? How would I go about doing this? I could manually do it of course, but looking for a method that would allow me to run a single script and it correctly update all of the cells within the table "wp_posts". If there is a way to do this without using PHP and simply use MySQL that would work as well. Again thanks in advance for any advice. Quote Link to comment https://forums.phpfreaks.com/topic/107693-need-help-modifying-mysql-database-with-a-php-script/ Share on other sites More sharing options...
.josh Posted May 28, 2008 Share Posted May 28, 2008 well I'm sure there's probably a better way, but I think this is what you're going for... $conn = mysql_connect('localhostorhostnamehere','dbusernamehere','dbpasswordhere') or die(mysql_error()); $db = mysql_select_db('dbnamehere') or die(mysql_error()); $query = "select id from wp_posts"; $result = mysql_query($query, $conn) or die(mysql_error()); while($id = mysql_fetch_array($result)) { $query = "update wp_posts set guid = 'http://www.dailyddl.com/?p={$id['id']}' where id = '{$id['id']}'"; $update = mysql_query($query, $conn) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/107693-need-help-modifying-mysql-database-with-a-php-script/#findComment-552090 Share on other sites More sharing options...
.josh Posted May 28, 2008 Share Posted May 28, 2008 although... something you might want to possibly consider is to just do an insert of "http://www.dailyddl.com/?p=" in your guid column and leave it out altogether, and in your script that actually needs the number added on, simply select id and guid and concat them ex: $link = $guid . $id; only reason I suggest this is it like, makes your db/script more open ended/versatile for changes... even better would be to not have a guid column at all, seeing as how it'd just be repeating the same thing over and over (taking up more space, process time, etc..) and have that address stored as a constant somewhere in your script to be called upon. That way if you need to change it, you just change that one single line. Quote Link to comment https://forums.phpfreaks.com/topic/107693-need-help-modifying-mysql-database-with-a-php-script/#findComment-552096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.