Ok so what i am doing is getting member id's from 1 table and looping those ID's through another table to get values to output. I had it working going through the first loop, then notice the output was all screwy, so released I needed to loop it through again, since there will could be multiple entries in the 2nd query for MemID. now I put in the 2nd loop but its not even going through, not sure where I messed up the code, but doesn't seem to output now when running it through the 2nd loop. though it did output without the loop. but that won't work due to multiple rows for each $memid.
$qry_skamembers = "SELECT * FROM ap_skamembers ORDER BY id";
$qry_skamembers = $mysqli->real_escape_string($qry_skamembers);
if($result_skamembers = $mysqli->query($qry_skamembers)){
while($row_skamembers = $result_skamembers->fetch_array()){
$AffID = $row_skamembers['AffID'];
$MemID = $row_skamembers['MemberID'];
$skacon = new mysqli(OW_DB_HOST, OW_DB_USER, OW_DB_PASSWORD, OW_DB_NAME);
if ($skacon->connect_error) {
die('Connect Error');
}
$get_data = "SELECT * FROM ow_base_billing_sale WHERE userID = $MemID AND status = 'delivered' ORDER BY id";
$get_data = $skacon->real_escape_string($get_data);
if($result_data = $skacon->query($get_data)){
while($finish = $result_data->fetch_array()){
$test = $finish['status'];
if($test == 'delivered') {
$sale_amount = $finish['price'];
$product = $finish['transactionUId'];
$products = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM ap_earnings where product = $product"));
if(mysqli_num_rows($products) > 0) { }
else {
echo "AFF: " . $AffID . " | ";
echo "Mem: " . $MemID . " | ";
echo "PRICE: " . $sale_amount . " | ";
echo "PRODUCT: " . $product . " -- ";
include('controller/record-sale.php');
echo "inserting record";
echo "run finished <br><br>";
}
}
}
}
}
}
I am still rather new at coding, so it might look a bit sloppy, my apologies for that.
I do not know if that is ist bu try to replace:
$get_data = $skacon-->real_escape_string($get_data);
with
$get_data = $skacon->real_escape_string($get_data);
i think there is an extra -
And as we found out in comments you need to change $product to '$product'
You can do all that in one query,nevermind it will be much faster
SELECT aps.AffID,aps.MemberID,owb.price,owb.transactionUId
FROM db1.ap_skamembers aps JOIN db2.ow_base_billing_sale owb
ON aps.MemberID = owb.userID AND owb.status='delivered'
JOIN db3.ap_earnings ape
ON ape.product = owb.transactionUId
ORDER BY aps.id
Related
I get an array with about 20.000 entries. Each entry has a PostID,
I have to go through these posts and check if they are published or exist.
When I query the whole thing with get_post_status, it takes about 15 seconds. If I take out get_post_status, I am at about 1 second.
Is there a faster solution to look if the post exists, which is then more performant?
The whole thing runs in the frontend.
Or do I just have to live with the fact at this point that 20,000 times querying the post status just always takes something ?
Here is my code
// $superUserLeads is the Array
$bikeEach = 0;
foreach($superUserLeads as $data){
$leadid = $data["leadID"];
$get_post_status = get_post_status( $leadid );
$ifKeyExists_EK_WKDM_shop_mail_bike_not_sold_1 = array_key_exists("EK_WKDM_shop_mail_bike_not_sold_1", $data);
if($ifKeyExists_EK_WKDM_shop_mail_bike_not_sold_1 == true){
$shopMailSended = $data["EK_WKDM_shop_mail_bike_not_sold_1"];
$shopMailSended = filter_var($shopMailSended, FILTER_VALIDATE_BOOLEAN);
}else{
$shopMailSended = false;
};
if($shopMailSended == true && $get_post_status === "publish"){
// Here an array is created, which is returned at the end.
$bikeEach++;
};
};
Edit sql query
#AaronJ had An Idea, but
nnfortunately, that doesn't make any difference in the end when it comes to time. But it does make a difference when it comes to memory usage
The Query Monitor plugin tells me with get_post_status
16.87s 250.112kB 10.5593S 23.443Q
with the SQl variant
16,48s 170.182kB 10,1352S 23.451Q
here is the code
global $wpdb;
$query = 'SELECT post_status FROM ' . $wpdb->posts . ' where id = ' . $leadid . " and post_status = 'publish' ";
$results = $wpdb->get_results($query, ARRAY_A);
if (empty($results)) {
echo "no result, post not existing: ".$leadid."<br>";
$resultsStatus = null;
}else{
echo "Has an result:".$leadid. "<br>";
$resultsStatus = $results[0]["post_status"];
}
get_post_status gets a lot of (sometimes) unnecessary data and it will continue to be slow if it's used often. To make this faster you can write your own query to get the data you need directly. Something like this.
global $wpdb;
$query = 'SELECT post_status FROM ' . $wpdb->posts . ' where id = ' . $id
. " and post_status = 'published'";
$results = $wpdb->get_results($query, ARRAY_A);
If you decide it's still too slow then you can move on to querying in chunks of 100 to 1000 $ids at a time by building a query and using in (id1, id2, id3 ...) Something like this.
function main() {
$leadIDList = array();
foreach($superUserLeads as $data){
array_push($leadIDList, $data["leadID"]);
if (count($leadIDList) >= 1000) {
$publishedIDs = $this->getPublishedIDs($leadIDList);
// do what you want here with the published IDs.
$this->dealWithLeads($publishedIDs);
// clear the list for the next query.
$leadIDList = array();
}
}
// include anything left over on the list.
$publishedIDs = $this->getPublishedIDs($leadIDList);
$this->dealWithLeads($publishedIDs);
}
function dealWithLeads($publishedIDs) {
// do what you want here with the published IDs.
}
function getPublishedIDs($ids) {
global $wpdb;
$query = "SELECT id, (case when post_status = 'publish' then 'yes' " .
"else 'nope' end) as published FROM " . $wpdb->posts . ' where id in (' .
implode(', ', $ids) . ")";
$queryResults = $wpdb->get_results($query, ARRAY_A);
$publishedIDs = array();
foreach ($queryResults as $row) {
$publishedIDs[row['id']] = row['published'];
}
return $publishedIDs;
}
I have 2 files, the first one:
gives me the list of products
The second one:
- information relating to the product, therefore as an information page
In the first file, I implement a <a> tag, to redirect to the second file
and the variable: $id_evidenza = $row_ListaEvidenze['ID_evidenza'];
<a href="edit_evidenza.php?id=<?php echo $id_evidenza; ?>&te=<?php echo $_GET['te']; ?>" title="Modifica">
<strong><?php echo outputDecode($row_ListaEvidenze['cod_evidenza']); ?></strong>
</a
make a select query, and take my variable:
$myid_processo = "-1";
if (isset($_GET['id'])) {
$myid_processo = $_GET['id'];
}
//42320819($database_riskmanagement,$riskmanagement);
$query_processo = sprintf("SELECT * FROM tev_Evidenze WHERE tev_Evidenze.id_struttura = ".$id_str." AND tev_Evidenze.ID_evidenza = %s", GetSQLValueString($myid_processo, "int"));
$processo = mysqli_query($riskmanagement, $query_processo) or die(mysqli_error($riskmanagement));
createLog($_COOKIE['cod_operatore'], $query_processo);
$row_processo = mysqli_fetch_assoc($processo);
$totalRows_processo = mysqli_num_rows($processo);
with a do / while loop, I show the results on screen and it's ok.
Now the problems begin, because right now from the page where there are the list of products, we are in the product info page, they asked me, to implement a navigation system, which gives the possibility that if I am inside the product A, going back and forth I can scroll through the other products, without going over the product list page, and so I did by implementing this navigation system:
if($_GET['te']==""){
$sel_tipo_acc = " (ev.id_tipo_accreditamento = 0)";
}else{
$sel_tipo_acc = " (ev.id_tipo_accreditamento = ".$_GET['te'].")";
}
$my_ric_att = searchControl(trim($_GET['ric_att']));
$txt_ric = "";
$txt_ric = "Risultati per ";
$r_sql = "SELECT * FROM tev_Evidenze AS ev LEFT JOIN tev_Fatt_crit AS fc
ON fc.ID_fatt_crit = ev.id_fatt_crit
WHERE ev.id_struttura = ".$_SESSION['str']." AND ".$sel_tipo_acc." AND ";
if ($_GET['facr'] != ""){
$r_sql .= " fc.ID_fatt_crit = ".$_GET['facr']." AND ";
}
$r_sql .= " ((ev.cod_evidenza LIKE '%" .$my_ric_att. "%') OR (ev.desc_evidenza LIKE '%" .$my_ric_att. "%')) ";
$txt_ric .= trim($_GET['ric_att']) ;
$r_sql .= " ORDER BY fc.ordine_fatt_crit, ev.cod_evidenza, ev.ordine_evidenza";
$txt_ric .= "<br>";
if($_GET['v']=="all"){
$paginazione = 1;
}else{
$paginazione = 1;
}
$maxRows_ListaEvidenze = $paginazione;
$pageNum_ListaEvidenze = 0;
if (isset($_GET['pageNum_ListaEvidenze'])) {
$pageNum_ListaEvidenze = $_GET['pageNum_ListaEvidenze'];
}
$startRow_ListaEvidenze = $pageNum_ListaEvidenze * $maxRows_ListaEvidenze;
//42320819($database_riskmanagement, $riskmanagement);
$query_ListaEvidenze = $r_sql;
$query_limit_ListaEvidenze = sprintf("%s LIMIT %d, %d", $query_ListaEvidenze, $startRow_ListaEvidenze, $maxRows_ListaEvidenze);
$ListaEvidenze = mysqli_query($riskmanagement, $query_limit_ListaEvidenze) or die(mysqli_error($riskmanagement));
createLog($_COOKIE['cod_operatore'], $v_sql);
$row_processo = mysqli_fetch_assoc($ListaEvidenze);
if (isset($_GET['totalRows_ListaEvidenze'])) {
$totalRows_ListaEvidenze = $_GET['totalRows_ListaEvidenze'];
} else {
$all_ListaEvidenze = mysqli_query($riskmanagement, $query_ListaEvidenze);
$totalRows_ListaEvidenze = mysqli_num_rows($all_ListaEvidenze);
}
$totalPages_ListaEvidenze = ceil($totalRows_ListaEvidenze/$maxRows_ListaEvidenze)-1;
$queryString_ListaEvidenze = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_ListaEvidenze") == false &&
stristr($param, "totalRows_ListaEvidenze") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_ListaEvidenze = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_ListaEvidenze = sprintf("&totalRows_ListaEvidenze=%d%s", $totalRows_ListaEvidenze, $queryString_ListaEvidenze);
I have the problem in my url, I can't get all the necessary parameters in $ _GET, that is, if for example I go from product A to that B, I get this:
pageNum_ListaEvidenze=5&totalRows_ListaEvidenze=76&id=340&te=5
pageNum_ListaEvidenze=6&totalRows_ListaEvidenze=76&id=340&te=5
where the only thing that changes is the number after: pageNum_ListaEvidenze=
displaying another record, but the id is always stopped at the first record, I have this problem from this snippet of code, which I posted before:
if (isset($_GET['pageNum_ListaEvidenze'])) {
$pageNum_ListaEvidenze = $_GET['pageNum_ListaEvidenze'];
}
how do i make everything dynamic in the url?
I really understand your issue , but here is a better solution that can make the life easy .
Let's suppose that currently , you are on the info page(Page B as you describe) that show the product information .
To swip for the next product , or previous , you can try to do this :
Normaly , one the info page , you are able to get the id of the current product
$currentProductID = $_GET['id'];
Think to create a method that give you the id of the next product , the query should be like :
SELECT id FROM `product` WHERE `id` > $currentProductID limit 1
This will return the id of the next product , and then redirect to the B page with this id returned using php redirection or by clicking on the tag etc .
To do that for the previous product , just inverse the query above
with <.
Hope that help you .
I know this may seem like a duplicated, out of stackoverflow, etc question but here we go.
I'm trying to make an SQL sentence that can find a coincidence between two strings
function getProductos($keyWords){
$keyWords = addslashes(strtolower($keyWords));
$keyWordsExploded = explode(" ",$keyWords);
$sql = "SELECT * FROM PRODUCTOS WHERE HOMBRE_MUJER LIKE :keyWords OR CATEGORIA LIKE :keyWords" OR NOMBRE LIKE :keyWords;
$query = self::$conn->prepare($sql);
$query->execute(array(":keyWords"=> "%" . $keyWords . "%"));
return $query;
}
In other part of the page I have this code:
<?php
if(isset($_GET['buscar'])){
require(PAGES_DIR . "queries_products.php");
$consultaProducts = new QueryProductos();
$productos = $consultaProducts->getProductos($_GET['buscar']);
if($productos->rowCount()!=0){
$arrayProductos = $productos->fetchAll(PDO::FETCH_ASSOC);
echo "<h3>Productos encontrados</h3>";
foreach($arrayProductos as $fila){
echo $fila['NOMBRE'] . " " . $fila['HOMBRE_MUJER'] . "<br>";
}
}else{
echo "<p class='alert alert-warning'>No results found <strong>" . $_GET['buscar'] . "</strong>";
}
}
?>
Everything works fine, In my database I store only 2 values in CATEGORIA which are: "hombres" and "mujeres", if i search for hombres I get all records which have a CATEGORIA of hombres but when i search for hombres y mujeres I get no results, I have tried using different sentences that i read but I haven't had any luck, I hope you can greatly save me by helping me solve this problem.
try this:
function getProductos($keyWords) {
$keyWordsExploded = explode(" ",$keyWords);
$sql = "SELECT * FROM PRODUCTOS WHERE ";
$likes = array("HOMBRE_MUJER", "CATEGORIA", "NOMBRE"); // build up our columns which will be used in our condition in sql statment
$params = array();
// building up our sql statment and collecting our params
foreach($likes as $like) {
foreach($keyWordsExploded as $kw) {
$sql .="$like like ? or ";
$params[] = "%".$kw."%";
}
}
$sql = rtrim($sql, "or "); // trim the last "or" condition in sql statment
$query = self::$conn->prepare($sql);
$query->execute($params);
return $query;
}
i am currently using url rewrite via IIS which is working and i am capturing the url with the below variable
$compiled_url= $_GET["params"];
however i need to check whats in that url with whats in the pages table in MySQL. the table looks like the below
tbl_pages
page_id | page_slug | page_parent
--------+-----------+------------
1 | posts | 0
2 | daily | 2
http://www.domain.com/posts/daily
what possible methods are there to check the above domain and passed parameters against the database and make sure they exist in that order so if the url was typed backwards daily/posts it would fail to a 404 as they don't reflect that way in the database
i have started with this method but just as an example my end result i would like to be a class or a neater option
$compiled_url = explode("/",$compiled_url);
$compiled_parent=0;
foreach($compiled_url as $url_decompiled)
{
$url_results = mysqli_query($webapp_db,"SELECT * FROM tbl_pages WHERE page_slug='" . $url_decompiled . "' AND page_parent ='" . $compiled_parent . "'");
if(mysqli_num_rows($url_results) > 0)
{
echo "page found <br>";
$result = mysqli_fetch_array($url_results);
$compiled_parent=$result['page_id'];
}
else
{
echo "page not found <br>";
break;
}
}
who has done something like this before? what methods are available without using a framework?
You can use something like this (have not tested it), but you get the gist of it...
<?php
$compiled_url = explode("/",$compiled_url);
// using alphabet characters for table aliases
$alphabet = explode("","abcdefghiklmnopqrstuvwxyz");
$sql_query = "SELECT * FROM tbl_pages ".$alphabet[0];
// loop to build all the JOINs
for($i=0; $i<count($compiled_url); $i++)
{
$sql_query .= " INNER JOIN tbl_pages ".$alphabet[$i+1]." ON ".$alphabet[$i+1].".page_id = ".$alphabet[$i].".page_parent";
}
// loop to build all the WHERE filters
$where = array();
for($i=0; $i<count($compiled_url); $i++)
{
$where[] = $alphabet[$i].".page_slug = '".$compiled_url[$i]."'";
}
$sql_query .= " WHERE ".implode(" AND ",$where);
// if results are found print "page found" else print "page not found"
if(mysqli_num_rows($url_results) > 0)
{
echo "page found <br>";
$result = mysqli_fetch_array($url_results);
$compiled_parent=$result['page_id'];
}
else
{
echo "page not found <br>";
break;
}
The problem with your code, that you have to check outside of the for loop not inside it. And I think you added some wrong data in your database.
So consider your table looks like this
tbl_pages
page_id | page_slug | page_parent
--------+-----------+------------
1 | posts | 0
2 | daily | 1 // <-- edited from 2 to 1
Then you use this script
$compiled_url = explode("/",$compiled_url);
$parent = 0;
$correct = true;
foreach($compiled_url as $item){
$results = mysqli_query($webapp_db,
"SELECT * FROM tbl_pages WHERE page_slug='" . $item . "' AND page_parent ='" . $parent . "' LIMIT 1"
);
if($row = mysqli_fetch_array($result)){
$parent = $row['id'];
} else {
$correct = false;
break;
}
}
if($correct){
echo "page found <br>";
} else {
echo "page not found <br>";
}
Edit this code as you wish and be aware of sql injections if you use it as is.
I have to create a dynamic query based on the value received by the user's input, the value of the variables are posted by GET
When I simply run this
$qry = "SELECT* FROM LAPTOP WHERE 1=1";
$resul = mysqli_query($qry);
retrieve($resul);
all the content of this table are displayed without any error,(retrieve function here displays all the results based on the query) but when I try to modify it like this, I get a blank page
$qry = "SELECT * FROM LAPTOP WHERE 1=1";
if(!empty($company))
{
$qry .= " AND company='$company'" ;
}
if(!empty($cpu))
{
$qry.= " AND cpu='$cpu' " ;
}
if(!empty($lifestyle))
{
$qry.= " AND lifestyle='$lifestyle' " ;
}
if(!empty($display))
{
$qry.= " AND display='$display'" ;
}
if(!empty($ram))
{
$qry.= " AND ram='$ram' " ;
}
if(!empty($HDD))
{
$qry.= " AND HDD='$HDD' " ;
}
echo $qry;
$result= mysqli_query($qry) || die(mysqli_error()) ;
retrieve($result) ;
$p = basename($_SERVER['REQUEST_URI']) ;
The result of echo $qry; is as expected, it displays this
SELECT * FROM LAPTOP WHERE 1=1 AND company='Asus' AND cpu='intel i3'
Is there a way to correct this? The reason I tried using WHERE 1=1 clause is that when all the variables are equal to NULL then the query returns all the rows from the table.
i guess you have no data in your database matched with your conditions . OR you have case sensitive with names.
example :
cpu='Intel i3' // with big I
cpu='intel I3' // with big I
cpu='intel i3' // double space.
OR if you have big string , think to use LIKE
$qry.= " AND cpu LIKE '%$cpu%' " ;
Is this a typo also ($resul_T_)?
$resul = mysqli_query($qry);
retrieve($resul);
How you managed to lose that 't' and later get it back? ;)
As others have pointed out it may simply be that your query does not match any records.
Anyway what I usually do in a similar case is put all the conditions in an array, and then implode the array with 'AND'. That way you don't have to bother with 1=1 and it doesn't matter whether you have 0, 1 or more conditions.
<?php
$qry = "SELECT * FROM LAPTOP";
$conditions = array();
$cpu = 'Intel';
$ram = '24GB';
if(!empty($cpu))
{
$conditions[] = "cpu='$cpu'";
}
if(!empty($lifestyle))
{
$conditions[] = "lifestyle='$lifestyle'";
}
if(!empty($display))
{
$conditions[] = "display='$display'";
}
if(!empty($ram))
{
$conditions[] = "ram='$ram'";
}
if(!empty($HDD))
{
$conditions[] = "HDD='$HDD'";
}
if( count( $conditions ) > 0 )
{
$qry .= " WHERE ";
$qry .= implode( " AND ", $conditions );
}
print_r($qry);
?>