SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 make a sql script out of this php
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

monkeys123
Starting Member

USA
7 Posts

Posted - 07/08/2014 :  16:53:16  Show Profile  Reply with Quote
edited.
can anyone help me make a sql script out of this php... i dont know sql and am failing to learn.
<?php
$db = new PDO('mysql:host=localhost;dbname=wordpress;charset=utf8', 'root', '');
$query = "INSERT INTO $wpdb->terms (term_id, name, slug) VALUES (%d, %s, %s)";
$wpdb->query($wpdb->prepare($query, $term_id, $name, $slug));
foreach($db->query('SELECT * FROM wp_posts') as $row) {
global $wpdb;
$term_id = $row['ID'];
$name = $row['category'];
$slug = $row['category'];
$term_taxonomy_id = /*INCREASE THE BY 1(one) THE MAX TAZ_ID ALREDY INSERTED*/;
$taxonomy = 'category';
$query = "INSERT INTO $wpdb->term_taxonomy (term_taxonomy_id, term_id, taxonomy) VALUES (%d, %d, %s)";
$wpdb->query($wpdb->prepare($query, $term_taxonomy_id, $term_id, $taxonomy));
}
?>

Edited by - monkeys123 on 07/08/2014 19:12:19

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 07/08/2014 :  16:57:54  Show Profile  Visit SwePeso's Homepage  Reply with Quote
USE wordpress;

INSERT	tmp
	(
		post_id,
		slug,
		tax,
		term_id
	)
SELECT	ID,
	category,
	'category',
	ISNULL((SELECT MAX(term_id) + 1 FROM dbo.tmp), 1)
FROM	dbo.wp_posts;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 07/08/2014 :  16:58:41  Show Profile  Reply with Quote
change the inner select:
...
SELECT wp.ID, wp.category, 'category',(SELECT MAX(t.term_id) FROM tmp t)+1 FROM wp_posts wp
I don't know the logic that you are trying to implement, but be aware that if there is more than one row in wp_posts, all the new rows inserted into tmp will have the same term_id. And, if there are no rows already existing in tmp or if the term_id for for any existing rows are null, then the new rows will be inserted with a null.
Go to Top of Page

monkeys123
Starting Member

USA
7 Posts

Posted - 07/08/2014 :  17:05:56  Show Profile  Reply with Quote
quote:
Originally posted by James K

change the inner select:
...
SELECT wp.ID, wp.category, 'category',(SELECT MAX(t.term_id) FROM tmp t)+1 FROM wp_posts wp
I don't know the logic that you are trying to implement, but be aware that if there is more than one row in wp_posts, all the new rows inserted into tmp will have the same term_id. And, if there are no rows already existing in tmp or if the term_id for for any existing rows are null, then the new rows will be inserted with a null.

i am confused too .-. term_id need to increase and not repeat (it will be a key in another table but inst the key of this table)

i maybe back -sissy exterminator
Go to Top of Page

monkeys123
Starting Member

USA
7 Posts

Posted - 07/08/2014 :  17:17:38  Show Profile  Reply with Quote
its executing , i think i got it
USE wordpress;

INSERT tmp
	(
		post_id,
		slug,
		tax,
		term_id
	)
SELECT	ID,
	category,
	'category',
  (SELECT MAX(t.term_id) + 1 FROM tmp t)
FROM wp_posts;

all equal to 0 as expected the "ISNULL((SELECT MAX(term_id) + 1 FROM dbo.tmp), 1)" is givvin me a error as well

Edited by - monkeys123 on 07/08/2014 17:19:05
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 07/08/2014 :  17:54:20  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DECLARE	@t INT;

SELECT	@t = ISNULL(MAX(term_id), 1)
FROM	dbo.tmp;

INSERT	tmp
	(
		post_id,
		slug,
		tax,
		term_id
	)
SELECT	ID,
	category,
	'category',
	ROW_NUMBER() OVER (ORDER BY post_ID) + @t
FROM	dbo.wp_posts;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

monkeys123
Starting Member

USA
7 Posts

Posted - 07/08/2014 :  17:59:25  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

DECLARE	@t INT;

SELECT	@t = ISNULL(MAX(term_id), 1)
FROM	dbo.tmp;

INSERT	tmp
	(
		post_id,
		slug,
		tax,
		term_id
	)
SELECT	ID,
	category,
	'category',
	ROW_NUMBER() OVER (ORDER BY post_ID) + @t
FROM	dbo.wp_posts;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA

sorry for taking your time but i think there is a error in my normalization, i need to learn some stuff before i can do this right
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000