简体   繁体   English

PHP function 与 SQL 查询不返回值

[英]PHP function with SQL query does not return a value

I want to create a function in PHP that will return the values of an SQL query in an array.我想在 PHP 中创建一个 function ,它将在数组中返回 SQL 查询的值。 The SQL code in the function works and i can display the values. function 中的 SQL 代码有效,我可以显示这些值。

But when i want to use it in an function, it will not work.但是当我想在 function 中使用它时,它将无法正常工作。 What is the error in my syntax?我的语法有什么错误?

function frequenz_suchen_datum_zeit($fdatum_von = "2000-01-01", $fdatum_bis = "2020-01-01", $fuhrzeit_von = "00:00:00", $fuhrzeit_bis = "23:59:59") {
$abfrage = $pdo->query("SELECT
                SUM(hg_ger_b) as hg_ger_b,
                SUM(hg_ger_v) as hg_ger_v,
                SUM(hg_zub_b) as hg_zub_b,
                SUM(hg_zub_v) as hg_zub_v,
                SUM(hg_ers_b) as hg_ers_b,
                SUM(hg_ers_v) as hg_ers_v,
                SUM(hg_rep) as hg_rep,
                SUM(pr_ger_b) as pr_ger_b,
                SUM(pr_ger_v) as pr_ger_v,
                SUM(pr_zub_b) as pr_zub_b,
                SUM(pr_zub_v) as pr_zub_v,
                SUM(pr_ers_b) as pr_ers_b,
                SUM(pr_ers_v) as pr_ers_v,
                SUM(pr_rep) as pr_rep,
                SUM(so_ser) as so_ser, 
                SUM(so_mie) as so_mie,
                SUM(so_was) as so_was,
                SUM(so_wer) as so_wer,
                SUM(so_web) as so_web
                FROM frequenz WHERE datum BETWEEN '".$fdatum_von."' AND '".$fdatum_bis."' AND uhrzeit BETWEEN '".$fuhrzeit_von."' AND '".$fuhrzeit_bis."'")->fetch();

return abfrage;
}

$test = frequenz_suchen_datum_zeit();
echo $test [hg_ger_b];


As $pdo is unknown inside the function it will not work.由于$pdo在 function 中是未知的,因此它将不起作用。

Quick ugly fix:快速丑陋的修复:

add as first line of the function:添加为 function 的第一行:

global $pdo; 

PROBLEM: The scope of variables.问题:变量的 scope。

Plese see urgently: https://www.php.net/manual/de/language.variables.scope.php急请看: https://www.php.net/manual/de/language.variables.scope.php

full code including the global:完整的代码,包括全局:

function frequenz_suchen_datum_zeit($fdatum_von = "2000-01-01", $fdatum_bis = "2020-01-01", $fuhrzeit_von = "00:00:00", $fuhrzeit_bis = "23:59:59") {

global $pdo;    
$abfrage = $pdo->query("SELECT
                    SUM(hg_ger_b) as hg_ger_b,
                    SUM(hg_ger_v) as hg_ger_v,
                    SUM(hg_zub_b) as hg_zub_b,
                    SUM(hg_zub_v) as hg_zub_v,
                    SUM(hg_ers_b) as hg_ers_b,
                    SUM(hg_ers_v) as hg_ers_v,
                    SUM(hg_rep) as hg_rep,
                    SUM(pr_ger_b) as pr_ger_b,
                    SUM(pr_ger_v) as pr_ger_v,
                    SUM(pr_zub_b) as pr_zub_b,
                    SUM(pr_zub_v) as pr_zub_v,
                    SUM(pr_ers_b) as pr_ers_b,
                    SUM(pr_ers_v) as pr_ers_v,
                    SUM(pr_rep) as pr_rep,
                    SUM(so_ser) as so_ser, 
                    SUM(so_mie) as so_mie,
                    SUM(so_was) as so_was,
                    SUM(so_wer) as so_wer,
                    SUM(so_web) as so_web
                    FROM frequenz WHERE datum BETWEEN '".$fdatum_von."' AND '".$fdatum_bis."' AND uhrzeit BETWEEN '".$fuhrzeit_von."' AND '".$fuhrzeit_bis."'")->fetch();
    
    return abfrage;
    }
    
    $test = frequenz_suchen_datum_zeit();
    echo $test [hg_ger_b];

Thanks for the quick help.感谢您的快速帮助。 Unfortunately, my code still doesn't work.不幸的是,我的代码仍然无法正常工作。 Thanks for the tip with the Prepare query.感谢您提供有关 Prepare 查询的提示。 I will also improve this if my function works.如果我的 function 有效,我也会改进这一点。

I've added the revised code again.我再次添加了修改后的代码。

A blank page is no longer output.空白页不再是 output。 My test text is output to me.我的测试文本对我来说是 output。

Unfortunately I still can't get the content of the function out.不幸的是,我仍然无法获取 function 的内容。

function frequenz_suchen_datum_zeit($fdatum_von = "2000-01-01", $fdatum_bis = "2020-01-01", $fuhrzeit_von = "00:00:00", $fuhrzeit_bis = "23:59:59") {
global $pdo;
$abfrage = $pdo->query("SELECT
                SUM(hg_ger_b) as hg_ger_b,
                SUM(hg_ger_v) as hg_ger_v,
                SUM(hg_zub_b) as hg_zub_b,
                SUM(hg_zub_v) as hg_zub_v,
                SUM(hg_ers_b) as hg_ers_b,
                SUM(hg_ers_v) as hg_ers_v,
                SUM(hg_rep) as hg_rep,
                SUM(pr_ger_b) as pr_ger_b,
                SUM(pr_ger_v) as pr_ger_v,
                SUM(pr_zub_b) as pr_zub_b,
                SUM(pr_zub_v) as pr_zub_v,
                SUM(pr_ers_b) as pr_ers_b,
                SUM(pr_ers_v) as pr_ers_v,
                SUM(pr_rep) as pr_rep,
                SUM(so_ser) as so_ser, 
                SUM(so_mie) as so_mie,
                SUM(so_was) as so_was,
                SUM(so_wer) as so_wer,
                SUM(so_web) as so_web
                FROM frequenz WHERE datum BETWEEN '".$fdatum_von."' AND '".$fdatum_bis."' AND uhrzeit BETWEEN '".$fuhrzeit_von."' AND '".$fuhrzeit_bis."'")->fetch();

return $abfrage;
}
echo "test";
$test = frequenz_suchen_datum_zeit();
echo $test['hg_ger_b'];

I think somehow the value is not passed correctly.我认为不知何故该值未正确传递。

But somehow I don't find the error.但不知何故,我没有找到错误。

Thank you.谢谢你。 It works now.现在可以了。

function frequenz_suchen_datum_zeit($pdo, $fdatum_von="2000-01-01", $fdatum_bis="2022-01-01", $fuhrzeit_von="00:00:00", $fuhrzeit_bis="23:59:59") {
$abfrage = $pdo->query("SELECT
                SUM(hg_ger_b) as hg_ger_b,
                SUM(hg_ger_v) as hg_ger_v,
                SUM(hg_zub_b) as hg_zub_b,
                SUM(hg_zub_v) as hg_zub_v,
                SUM(hg_ers_b) as hg_ers_b,
                SUM(hg_ers_v) as hg_ers_v,
                SUM(hg_rep) as hg_rep,
                SUM(pr_ger_b) as pr_ger_b,
                SUM(pr_ger_v) as pr_ger_v,
                SUM(pr_zub_b) as pr_zub_b,
                SUM(pr_zub_v) as pr_zub_v,
                SUM(pr_ers_b) as pr_ers_b,
                SUM(pr_ers_v) as pr_ers_v,
                SUM(pr_rep) as pr_rep,
                SUM(so_ser) as so_ser, 
                SUM(so_mie) as so_mie,
                SUM(so_was) as so_was,
                SUM(so_wer) as so_wer,
                SUM(so_web) as so_web
                FROM frequenz WHERE datum BETWEEN '".$fdatum_von."' AND '".$fdatum_bis."' AND uhrzeit BETWEEN '".$fuhrzeit_von."' AND '".$fuhrzeit_bis."'")->fetch();
echo "<pre>"; print_r($abfrage); echo "</pre>";
return $abfrage;

}

$test = frequenz_suchen_datum_zeit($pdo);

echo "Testausgabe1: ";
echo $test['hg_ger_b'];

This is a code with an test database, i will delet it when the issue is fixed:这是一个带有测试数据库的代码,我会在问题解决后将其删除:

<?php
//Direkter Zugriff auf die Datei erlauben?

$pdo = new PDO('mysql:host=vwp14745.webpack.hosteurope.de;dbname=db12258506-test', 'db12258506-test', '12test12');

?>

This is the main code:这是主要代码:

<?
require_once('connect.php');

error_reporting(E_ALL & ~ E_NOTICE);
ini_set ('display_errors', 'On');

//Parameter für die einzelnen Abfragen.
$fdatum_von = "2018-01-19";
$fdatum_bis = "2022-01-19";
$fuhrzeit_von = "10:00:00";
$fuhrzeit_bis = "18:33:00";

function frequenz_suchen_datum_zeit($pdo, $fdatum_von = "2000-01-01", $fdatum_bis = "2020-01-01", $fuhrzeit_von = "00:00:00", $fuhrzeit_bis = "23:59:59") {
global $pdo;
$abfrage = $pdo->query("SELECT
                SUM(hg_ger_b) as hg_ger_b,
                SUM(hg_ger_v) as hg_ger_v,
                SUM(hg_zub_b) as hg_zub_b,
                SUM(hg_zub_v) as hg_zub_v,
                SUM(hg_ers_b) as hg_ers_b,
                SUM(hg_ers_v) as hg_ers_v,
                SUM(hg_rep) as hg_rep,
                SUM(pr_ger_b) as pr_ger_b,
                SUM(pr_ger_v) as pr_ger_v,
                SUM(pr_zub_b) as pr_zub_b,
                SUM(pr_zub_v) as pr_zub_v,
                SUM(pr_ers_b) as pr_ers_b,
                SUM(pr_ers_v) as pr_ers_v,
                SUM(pr_rep) as pr_rep,
                SUM(so_ser) as so_ser, 
                SUM(so_mie) as so_mie,
                SUM(so_was) as so_was,
                SUM(so_wer) as so_wer,
                SUM(so_web) as so_web
                FROM frequenz WHERE datum BETWEEN '".$fdatum_von."' AND '".$fdatum_bis."' AND uhrzeit BETWEEN '".$fuhrzeit_von."' AND '".$fuhrzeit_bis."'")->fetch();

return $abfrage;

}
//frequenz_suchen_datum_zeit($pdo);

$test = frequenz_suchen_datum_zeit($pdo);

echo "Testausgabe1: ";
echo $test['hg_ger_b'];


echo "</br>-------------------------------------------------</br>";
//SQL Code without function
$abfrage = $pdo->query("SELECT
                SUM(hg_ger_b) as hg_ger_b,
                SUM(hg_ger_v) as hg_ger_v,
                SUM(hg_zub_b) as hg_zub_b,
                SUM(hg_zub_v) as hg_zub_v,
                SUM(hg_ers_b) as hg_ers_b,
                SUM(hg_ers_v) as hg_ers_v,
                SUM(hg_rep) as hg_rep,
                SUM(pr_ger_b) as pr_ger_b,
                SUM(pr_ger_v) as pr_ger_v,
                SUM(pr_zub_b) as pr_zub_b,
                SUM(pr_zub_v) as pr_zub_v,
                SUM(pr_ers_b) as pr_ers_b,
                SUM(pr_ers_v) as pr_ers_v,
                SUM(pr_rep) as pr_rep,
                SUM(so_ser) as so_ser, 
                SUM(so_mie) as so_mie,
                SUM(so_was) as so_was,
                SUM(so_wer) as so_wer,
                SUM(so_web) as so_web
                FROM frequenz WHERE datum BETWEEN '".$fdatum_von."' AND '".$fdatum_bis."' AND uhrzeit BETWEEN '".$fuhrzeit_von."' AND '".$fuhrzeit_bis."'")->fetch();
                
echo "Testausgabe1: ";
echo $abfrage['hg_ger_b'];
                
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM