PHP Skript optimieren - SQL Anfragen minimieren

FrazeColder

Lt. Commander
Registriert
Okt. 2013
Beiträge
1.718
Guten Tag zusammen,

ich habe für ein Plugin auf meiner WordPress Seite ein bisschen extra Custom Code geschrieben. Denn das Plugin kann von Haus aus leider nur die Produkte überschreiben. Ich möchte aber gerne die Produkte nicht überschreiben, sondern die Daten nur hinzufügen bzw. ein paar Daten löschen, falls diese nicht mehr in der Datenbank Tabelle (wp_all_import_xml) vorhanden sind.

Damit ich euch die Verwirrung nehme. Das ganze ist ein Preisvergleich. Daher kann ich mehrere Links, Shops und Preise pro Produkt haben (for-Schleife) :)

Meine Frage und Bitte an euch ist, wie kann ich man Skript beschleunigen und optimieren. Da das Plugin leider die Datenbank etwas vermüllt, muss ich mir die Daten mühselig heraussuchen... Hier ein Ausschnitt der Daten von einem Produkt. Ich habe mir direkt nur 2 von 4 Feldern anzeigen lassen, welche ich auch brauche. (mich interessieren beim abgleich nur die product_shops_%_price/price_old/link/shop):

Bildschirmfoto 2017-03-19 um 15.57.29.png

Hier ist mein Code:
Code:
<?php

add_action('pmxi_before_xml_import', 'wp_all_import_before_xml_import', 10, 1);
add_action('pmxi_saved_post', 'my_saved_post', 10, 2);
add_action('pmxi_after_xml_import', 'after_xml_import', 10, 1);

function wp_all_import_before_xml_import($import_id){
	
	if($import_id !== 72 || $import_id !== 88){
	
		unlink("wp_all_import.txt");

		//Datenbankverbindung aufbbauen
		$database = new mysqli("localhost", "wordpress_dc", "censored", "wordpress_5");
		$database_gk = new mysqli("localhost", "wordpress_8", "censored", "wordpress_6");

		//Datenbankverbindung checken
		if($database->connect_errno){
			$myfile = fopen("wp_all_import.txt", "a");
			fwrite($myfile, "+++Couldn't connect to database!+++\n\n");
			fclose($myfile);
		}

		if($database_gk->connect_errno){
			$myfile = fopen("wp_all_import.txt", "a");
			fwrite($myfile, "+++Couldn't connect to database!+++\n\n");
			fclose($myfile);
		}

		//WP_ALL_IMPORT Tabelleninhalt löschen
		$sql = 'DELETE FROM `wp_all_import`';
		$database->query($sql);

		//Holen alle Posts
		$values_gkw = $database_gk->query("SELECT `ID` FROM `fWR6qIN_posts` where post_type = 'product' AND post_status = 'publish'");

		while($row = $values_gkw->fetch_assoc()){
			$id = $row["ID"];
			$pid = $id;
			$title = get_the_title($pid);

			$repeater = $database_gk->query("SELECT Count(meta_key) AS cnt FROM `fWR6qIN_postmeta` Where meta_key like 'product_shops_%_price' AND (post_id = $pid)");

			while($row = $repeater->fetch_assoc()){
				$count = $row["cnt"];
			}

			for($i = 0; $i < $count; $i++){
				$price_meta = "product_shops_".$i."_price";
				$price_old_meta = "product_shops_".$i."_price_old";
				$link_meta = "product_shops_".$i."_link";
				$shop_meta = "product_shops_".$i."_shop";

				$price;
				$price_old;
				$link;
				$shop;

				$details = $database_gk->query("SELECT `meta_key`, `meta_value` FROM `fWR6qIN_postmeta` WHERE post_id = '$pid' AND (meta_key like '$price_meta' OR meta_key like '$price_old_meta' OR meta_key like '$link_meta' OR meta_key like '$shop_meta')");

				while($row_meta = $details->fetch_assoc()){
					if($row_meta["meta_key"] == $price_meta){
						$price = $row_meta["meta_value"];
					}elseif($row_meta["meta_key"] == $price_old_meta){
						$price_old = $row_meta["meta_value"];
					}elseif($row_meta["meta_key"] == $link_meta){
						$link = $row_meta["meta_value"];
					}elseif($row_meta["meta_key"] == $shop_meta){
						$shop = $row_meta["meta_value"];
					}else{
						$myfile = fopen("wp_all_import.txt", "a");
						fwrite($myfile, "Is not matching!\n");
						fclose($myfile);
					}
				}

				//Checken ob Product Shop Row noch in Datenbank vorhanden
				$values = $database->query("SELECT * FROM `wp_all_import_xml` WHERE name = '$title' AND price = '$price' AND shop = '$shop' AND url = '$link'");

				$count_values = mysqli_num_rows($values);

				//Falls nein, lösche diese Product Shop Row aus Datenbank
				//Falls ja, füge Product Shop Row der "echten" Datenbank hinzu
				if($count_values == 0){
					$sql = "DELETE FROM `wp_all_import` WHERE pid = '$pid' AND shop = '$shop' AND price = '$price' AND link = '$link'";
					$database->query($sql);

					/*
					$myfile = fopen("wp_all_import.txt", "a");
					fwrite($myfile, "Would delete! " . $pid . " Preis: " . $price . " Link: " . $link . "\n");
					fclose($myfile);
					*/

				}elseif($count_values == 1){
					$sql = "INSERT INTO `wp_all_import` (pid, price, price_old, link, shop) VALUES ('$pid', '$price', '$price_old', '$link', '$shop')";
					
					if($database->query($sql) === TRUE){
						
					}else{
						$myfile = fopen("wp_all_import.txt", "a");
						fwrite($myfile, "Product ERROR!\n");
						fclose($myfile);
					}

					/*
					$myfile = fopen("wp_all_import.txt", "a");
					fwrite($myfile, "Would insert! " . $pid . " Preis: " . $price . " Link: " . $link . "\n");
					fclose($myfile);
					*/
					
				}else{
					$myfile = fopen("wp_all_import.txt", "a");
					fwrite($myfile, "ERROR by detecting Product (More than 1 Row return by SQL!): " .$title. " Preis: " .$price. " Shop: " .$shop. " Link: " .$link. "\t num_rows: " .$count_values. "\n\n");
					fclose($myfile);
				}

				$price = null;
				$price_old = null;
				$link = null;
				$shop = null;
				$price_meta = null;
				$price_old_meta = null;
				$link_meta = null;
				$shop_meta = null;

			}

			$title = null;
			$count = null;
		}
	}	
}

function my_saved_post($pid, $xml_node){
	$title = get_the_title($pid);
	
	if($title != "End of Import" || $title !== "Start of Import"){
	
		//Datenbankverbindung aufbbauen
		$database = new mysqli("localhost", "wordpress_dc", "censored", "wordpress_5");
		$title = get_the_title($pid);

		//Datenbankverbindung checken
		if($database->connect_errno){
			$myfile = fopen("wp_all_import.txt", "a");
			fwrite($myfile, "+++Couldn't connect to database!+++\n\n");
			fclose($myfile);
		}

		//Anzahl an an Product Shop Rows holen
		$aktueller_counter = -1;
		while(have_rows('product_shops', $pid)): the_row();
			$aktueller_counter = $aktueller_counter + 1;
		endwhile;

		if($aktueller_counter == -1){
			$aktueller_counter = 0;
		}	

		$pic = get_field("product_gallery_external_0_url", $pid);
		$alt = get_field("product_gallery_external_0_alt", $pid); 

		delete_row('product_gallery_external', 1, $pid);

		if($pic != null && $alt != null){
			update_post_meta($pid, 'fifu_image_url', $pic);
			update_post_meta($pid, 'fifu_image_alt', $alt);
		}

		//Preis, Alter Preis, Link und Shop holen
		$price = get_field("product_shops_0_price", $pid);
		$price_old = get_field("product_shops_0_price_old", $pid);
		$link = get_field("product_shops_0_link", $pid);
		$shop = get_field("product_shops_0_shop", $pid)->ID;


		//Holen Preis, Alten Preis, Link und Shop aus "echter" Datenbenk, wo PID = PID Und Shop = Shop
		$value = $database->query("SELECT `price`, `price_old`, `link`, `shop` FROM `wp_all_import` WHERE pid = '$pid' AND shop = '$shop' AND link = '$link'");
		$count_values = mysqli_num_rows($values);

		//Itterieren über Rückgabewert(e) der Datenbank
		while($row = $value->fetch_assoc()){
			//Wenn Shop = Shop und Preis oder Alter Preis haben sich geändert, dann lösche Eintrag aus der "echten" Datenbank, wo PID = PID, SHOP = SHOP und PREIS = PREIS (Könnten mehere Sein!)
			//Anschließend füge neuen Wert in Dantenbank ein
			if($row["shop"] == $shop && $row["link"] == $link && ($row["price"] != $price || $row["price_old"] != $price_old)){

				$rprice = $row["price"];
				$rprice_old = $row["price_old"];
				$rshop = $row["shop"];

				$sql = "DELETE FROM `wp_all_import` WHERE pid = '$pid' AND shop = '$rshop' AND price = '$rprice'";
				$database->query($sql);

				$sql = "INSERT INTO `wp_all_import` (pid, price, price_old, link, shop) VALUES ('$pid', '$rprice', '$rprice_old', '$link', '$rshop')";
				$database->query($sql);
			}
		}

		//Holen Preis, Alten Preis, Link und Shop aus "echter" Datenbenk, wo PID = PID
		$values = $database->query("SELECT `price`, `price_old`, `link`, `shop` FROM `wp_all_import` WHERE pid = '$pid'");
		$count_values = mysqli_num_rows($values);

		//Itterieren über Rückgabewert(e) der Datenbank
		while($row = $values->fetch_assoc()) {
			$row = array(
				'price' => $row["price"],
				'price_old' => $row["price_old"],
				'currency' => 'euro',
				'portal' => '',
				'link' => $row["link"],
				'shop' => $row["shop"]
			);

			//Wenn ungleich Preis = Preis, Alter Preis = Alter Preis, Link = Link und Shop = Shop, dann füge Row hinzu, da nicht gerade hinzugefügte Row aus Import und Import hat alle Product Shop Rows gelöscht

			//Ansonsten füge Eintrag in Datenbank ein, da eben neu hinzugefügt durch Import
			if(!($row["price"] == $price && $row["price_old"] == $price_old && $row["link"] == $link && $row["shop"] == $shop)){
				$j = add_row('product_shops', $row, $pid);
			}else{
				$sql = "INSERT INTO `wp_all_import` (pid, price, price_old, link, shop) VALUES ('$pid', '$price', '$price_old', '$link', '$shop')";
				$database->query($sql);
			}
		}
	}
}

function after_xml_import($import_id){		

	$myfile = fopen("wp_all_import.txt", "a");
	fwrite($myfile, "+++ Entered After XML Import +++\n");
	
	if($import_id == 89){
		fwrite($myfile, "In After Import ID: 89\n\n");
	}elseif($import_id == 90){
		fwrite($myfile, "In After Import ID: 90\n\n");
	}elseif($import_id == 91){
		fwrite($myfile, "In After Import ID: 91\n\n");
	}elseif($import_id == 92){
		fwrite($myfile, "In After Import ID: 92\n\n");
	}elseif($import_id == 93){
		fwrite($myfile, "In After Import ID: 93\n\n");
	}elseif($import_id == 94){
		fwrite($myfile, "In After Import ID: 94\n\n");
	}elseif($import_id == 95){
		fwrite($myfile, "In After Import ID: 95\n\n");
	}elseif($import_id == 96){
		fwrite($myfile, "In After Import ID: 96 (Start)\n\n");
	}else{
		fwrite($myfile, "In After Import ID: Not Found!\n\n");
	}
	
	fclose($myfile);
}

?>

Ich würde mich sehr über Verbesserungsvorschläge und Ideen freuen!
MfG und Vielen Dank!
 
Zuletzt bearbeitet:

Ähnliche Themen

Zurück
Oben