Ask about the php code.

Asked 1 years ago, Updated 1 years ago, 42 views

I have two questions about the code in the function as follows.

1. What does "ORDER BY `order`);" mean?

2. "while($row=$results->fetch(PDO::FETCH_ASSOC))"){
$product["size"][ ] = $row["size"];
What does "}" mean?

 function get_product_single($sku){
    require(ROOT_PATH. "inc/database.php");
    try{
        $results=$db->prepare("SELECT name, price, img, sku, paypal FROM products where sku=?");
        $results->bindParam(1,$sku);
        $results->execute();
    }catch(Exception$e){
      echo "Data could not be retried from the database.";
      exit;
    }

    $product=$results->fetch(PDO::FETCH_ASSOC);
    if($product===false)return$product;
    $product["sizes"] = array();
    try{
        $results=$db->prepare("
        SELECT size
        FROM products_sizesps 
        INNER JOIN SIZES ON ps.size_id=s.id
        WHERE product_sku=?
        ORDER BY `order`);
        $results->bindParam(1,$sku);
        $results->execute();
    } catch(Exception$e){
        echo "Data could not be retried from the database.";
        exit;
    }
    while($row=$results->fetch(PDO::FETCH_ASSOC)){
        $product["sizes"][ ] = $row["size"];
    }

    return$product;
}

php

2022-09-30 21:18

1 Answers

You use MySQL, right?

It means to sort by column order.
The order is a reserved MySQL word, so if you want to use it as a column name in SQL, you must enclose it in `(backquote).
https://dev.mysql.com/doc/refman/5.6/ja/reserved-words.html

All SQL results have been added to the array $product["sizes"].
The documentation is useful for $results->fetch (PDO::FETCH_ASSOC).
http://php.net/manual/ja/pdostatement.fetch.php


2022-09-30 21:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.