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

使用PHP函数进行多个MySQL查询,php,mysql,Php,Mysql,我有一个名为“orders”的MySQL表。我想使功能,以便我可以轻松地访问多少订单在过去添加:小时,天,周,月和年 如果我在每个函数中不需要文件dbconfig.php,则会得到错误:( 到目前为止,下面的代码完全符合我的要求,但我认为可以缩短它吗 <?php function count_db_last_hour(){ require("../dbconfig.php"); $sql = "SELECT * FROM orders WHERE checkdate >= date

我有一个名为“orders”的MySQL表。我想使功能,以便我可以轻松地访问多少订单在过去添加:小时,天,周,月和年

如果我在每个函数中不需要文件
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() ;

?>

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

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文件,则会出现以下错误:(

由于函数中未看到
$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;
}

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

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文件,则会出现以下错误:(

由于函数中未看到
$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;
}

创建一个通用函数,只需在每个函数中传递$sql作为参数dnt include 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;

创建一个通用函数,只需在每个函数中传递$sql作为参数dnt include 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;

尝试使用
include
,然后将其放在全局范围内。您可以发布错误吗?阅读PHP中的。尝试使用
include
,然后将其放在全局范围内。您可以发布错误吗?阅读PHP中的。这几乎就是我要回答的问题。我会在函数和de>global$connect;在函数中,但这实际上取决于dbconfig.phpAlso中的内容。如果连接是全局的,那么关闭它可能不是最友好的做法,以防任何其他代码使用它。@cyborg如果使用第一个版本,则必须使用以下格式调用它:
echo count\u db\u last\u item('HOUR').count\u db\u last\u item('DAY').[剩余部分]
。对于第二种解决方案,您必须在每个函数调用中添加$connect作为第二个参数。这是一个非常好的代码!比我的代码要短得多:D@Kaddath谢谢,它现在运行得很好,我使用了1.code。$type选择非常棒!这几乎就是我要回答的。我会用require_once在函数外部和函数中的
global$connect;
,但这实际上取决于dbconfig.phpAlso中的内容。如果连接是全局的,那么关闭它可能不是最友好的做法,以防任何其他代码想要使用它。@cyborg如果使用第一个版本,则必须使用thi调用它s格式:
echo count\u db\u last\u item('HOUR')。
'.count\u db\u last\u item('DAY')。[其余]
。对于第二种解决方案,您必须在每个函数调用中添加$connect作为第二个参数。这是一个非常好的代码!比我的代码要短得多:D@Kaddath谢谢,它现在工作得很好,我使用了1.code。$type选择真的很棒!