使用PHP函数进行多个MySQL查询

时间:2017-05-24 09:17:41

标签: php mysql

我有一个名为&#34的订单表;订单"。我想创建函数,以便我可以轻松访问在Last:Hours,Day,Week,Month和Year中添加了多少订单。

如果我在每个函数中都不需要文件dbconfig.php,那么我会得到错误:(

到目前为止,下面的代码完全符合我的要求,但我认为可以缩短它吗?

<?php

function count_db_last_hour(){
require("../dbconfig.php");
$sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 HOUR) ORDER BY checkdate DESC";
$result = $connect->query($sql);
$connect->close();
return $result->num_rows;
}

function count_db_last_week(){
require("../dbconfig.php");
$sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 WEEK) ORDER BY checkdate DESC";
$result = $connect->query($sql);
$connect->close();
return $result->num_rows;
}

function count_db_last_day(){
$sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 DAY) ORDER BY checkdate DESC";
require("../dbconfig.php");
$result = $connect->query($sql);
$connect->close();
return $result->num_rows;
}

function count_db_last_month(){
$sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 MONTH) ORDER BY checkdate DESC";
require("../dbconfig.php");
$result = $connect->query($sql);
$connect->close();
return $result->num_rows;
}

function count_db_last_year(){
$sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 YEAR) ORDER BY checkdate DESC";
require("../dbconfig.php");
$result = $connect->query($sql);
$connect->close();
return $result->num_rows;
}

echo count_db_last_hour() . '<br>' . count_db_last_day() . '<br>' . count_db_last_month() . '<br>' . count_db_last_year() ;

?>

是否可以缩短此代码?谢谢你帮忙。

更新:

如果我不需要dbconfig.php内部功能,我会收到错误:

  

PHP注意:未定义的变量:连接在第9行的/home/test2.php中   PHP致命错误:未捕获错误:调用成员函数query()   /home/test2.php:9中的null堆栈跟踪:
  #0 /home/test2.php(35):count_db_last_hour()
  #9 {main}在第9行的/home/test2.php中抛出

dbconfig.php文件包含以下代码:

<?php
$servername = "localhost";
$username = "ordzxq_usar";
$password = "9ir3Grd_fXz";
$dbname = "store_orders";
$connect = new mysqli($servername, $username, $password, $dbname);
if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
} 
?>

2 个答案:

答案 0 :(得分:2)

创建一个函数并将type传递给它:

function count_db_last_item($type) {
    $allowedTypes = ['HOUR', 'DAY',];   // and more
    // if type not allowed - return from function
    if (!in_array($type, $allowedTypes)) {
        return false;
    }

    require("../dbconfig.php");
    // add $type to query text
    $sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 $type) ORDER BY checkdate DESC";
    $result = $connect->query($sql);
    $connect->close();
    return $result->num_rows;
}
  

如果我在每个函数中都不需要文件dbconfig.php而不是ERROR :(

由于您的函数中未显示$connect变量,因此出现错误。只需将其作为参数传递:

require("../dbconfig.php");

function count_db_last_item($type, $connect) {
    $allowedTypes = ['HOUR', 'DAY',];   // and more
    // if type not allowed - return from function
    if (!in_array($type, $allowedTypes)) {
        return false;
    }

    // add $type to query text
    $sql = "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 $type) ORDER BY checkdate DESC";
    $result = $connect->query($sql);
    // if you really need to close connection - close it
    $connect->close();
    return $result->num_rows;
}

答案 1 :(得分:0)

创建一个公共函数,只需传递$ sql作为参数dnt在每个函数中包含dbconfig而不是将$ connect作为第二个参数传递给函数

 function count($sql,$connect){       //$connect is your $conn variable
    $result = $connect->query($sql);
    $connect->close();
    return $result->num_rows;
    }
 $sql= "SELECT * FROM orders WHERE checkdate >= date_sub(now(), INTERVAL 1 HOUR) ORDER BY checkdate DESC";

$count_db_last_week =count($sql);

echo $count_db_las_week;