0

I'm trying to generate a CSV but its taking too much time. I have also specified the cols. I want in the query still the generation is very slow, how to speed it up ? Any suggestions will be appreciated. My php code is following, Thanks in advance.

$strdate = strtotime($_POST["strdate"]);
$enddate = strtotime($_POST["enddate"]);
    
$q0 = "SELECT ordr_id, ordr_cust_id, ordr_b_company, ordr_shipping_selected, ordr_timedate FROM shop_orders WHERE 
            ordr_ship_id = " . fix($_POST["sid"]) . " AND 
            ordr_timedate BETWEEN " . fix($strdate) . " AND " . fix($enddate) . " 
            ORDER BY ordr_timedate DESC";
$r0 = mysqli_query($conn, $q0) or die("Query Failed: Get Orders");
    
    if (mysqli_num_rows($r0) <> 0) {
        $prodtype = array("Consumables", "Materials");
    
        $csvfile = "orders-by-supplier.csv";
        $file = fopen($csvfile, "w");
    
        // Save headings alon
        $HeadingsArray = array(
            "Date",
            "Order #",
            "Customer ID",
            "GL Code",
            "Company",
            "SKU",
            "Part #",
            "Name",
            "Cat",
            "Size",
            "Price",
            "Qty",
            "Total",
            "Supplier"
        );
        fputcsv($file, $HeadingsArray);
    
        // Save all records
        while ($a0 = mysqli_fetch_array($r0)) {
    
            $q2 = "SELECT cust_gl_code FROM shop_customers WHERE cust_id = " . fix($a0["ordr_cust_id"]) . " LIMIT 1";
            $r2 = mysqli_query($conn, $q2) or die("Query Failed: Get Customer");
            $a2 = mysqli_fetch_assoc($r2);
    
            // Get Order Content
            $q1 = "SELECT orcn_prod_code, orcn_prod_part_num, orcn_prod_name, orcn_prod_type, orcn_option_0, orcn_price, orcn_qty, orcn_total  FROM shop_orders_content WHERE orcn_ordr_id = " . fix($a0["ordr_id"]);
            $r1 = mysqli_query($conn, $q1) or die("Query Failed: Get Order Content");
    
            while ($a1 = mysqli_fetch_assoc($r1)) {
                fputcsv($file, array(
                    date("d-m-Y", $a0["ordr_timedate"]),
                    $a0["ordr_id"],
                    $a0["ordr_cust_id"],
                    $a2["cust_gl_code"],
                    $a0["ordr_b_company"],
                    $a1["orcn_prod_code"],
                    $a1["orcn_prod_part_num"],
                    $a1["orcn_prod_name"],
                    $prodtype[$a1["orcn_prod_type"]],
                    $a1["orcn_option_0"],
                    $a1["orcn_price"],
                    $a1["orcn_qty"],
                    $a1["orcn_total"],
                    $a0["ordr_shipping_selected"]
                ));
            }
        }
    
        fclose($file);
    }
Cid
  • 14,968
  • 4
  • 30
  • 45
Noob Coder
  • 68
  • 6
  • 1
    don't execute many queries in a loop, but execute only one query with a `JOIN` – Cid Nov 23 '21 at 10:10
  • 1
    And secure your queries, they are vulnerable to SQL injections, do **not** use some obscure custom functions that gives you the false feeling of security, but use instead [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Cid Nov 23 '21 at 10:12
  • 2
    make your query one query with three joins. the seperate queries is what slows you down significantly – Tschallacka Nov 23 '21 at 10:12
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Nov 23 '21 at 10:15
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. – ADyson Nov 23 '21 at 10:15
  • Can anyone help me combine the 3 sql queries into 1 ? Thanks – Noob Coder Nov 23 '21 at 10:22
  • 1
    Well first you need to take a SQL tutorial and learn how to use JOINs – ADyson Nov 23 '21 at 10:33
  • Then to get help with your query, I suggest asking a new question where you provide sufficient details of the database schema, sample data, and expected result, along with your own attempt to JOIN all the necessary tables and any problem you encountered while writing and running the query. Make sure whatever you provide constitutes a [mre] of the issue. It's hard for us to help you with only minimal knowledge of the database design and data, and this question is really focused on the wrong issue, so it would be better to start a fresh one which is focused on the SQL, not the PHP or CSV part. – ADyson Nov 23 '21 at 10:35
  • Its pretty straightforward db, first I get the order, then I get the order details i.e all the products that are ordered. That's it. Parent with many child listings. I don't think one to many query can be done where in the criteria is based on the parent but all related child's are needed to be listed as well. – Noob Coder Nov 23 '21 at 14:29

0 Answers0