【问题标题】:Run a call from a function PHP从函数 PHP 运行调用
【发布时间】:2013-05-21 23:55:44
【问题描述】:

我正在使用 php 和 html 构建一个网站,我习惯于从数据库接收数据,也就是动态网站,我已经构建了一个 CMS 供我自己使用。 我正在尝试使用 php 和函数“简化”接收过程。

我的 Functions.php 如下所示:

function get_db($row){
        $dsn = "mysql:host=".$GLOBALS["db_host"].";dbname=".$GLOBALS["db_name"];
        $dsn = $GLOBALS["dsn"];
        try {
            $pdo = new PDO($dsn, $GLOBALS["db_user"], $GLOBALS["db_pasw"]);
            $stmt = $pdo->prepare("SELECT * FROM lp_sessions");
            $stmt->execute();
            $row = $stmt->fetchAll();
            foreach ($row as $row) {
                echo $row['session_id'] . ", ";
            }
        }
        catch(PDOException $e) {
            die("Could not connect to the database\n");
        }
    }

我会在哪里得到这样的行内容:$row['row']; 我试图这样称呼它: 下面的sn-p来自index.php

echo get_db($row['session_id']); // Line 22

只是为了显示所有行中的内容。 当我运行该代码 sn-p 我得到错误:

注意:未定义变量:C:\wamp\www\Wordpress ish\index.php 中的行 在第 22 行

我也在使用 PDO,只是为了让你知道 :)

非常感谢任何帮助!

问候 斯蒂安

编辑:更新的functions.php

function get_db(){
        $dsn = "mysql:host=".$GLOBALS["db_host"].";dbname=".$GLOBALS["db_name"];
        $dsn = $GLOBALS["dsn"];
        try {
            $pdo = new PDO($dsn, $GLOBALS["db_user"], $GLOBALS["db_pasw"]);
            $stmt = $pdo->prepare("SELECT * FROM lp_sessions");
            $stmt->execute();
            $rows = $stmt->fetchAll();
            foreach ($rows as $row) {
                echo $row['session_id'] . ", ";
            }
        }
        catch(PDOException $e) {
            die("Could not connect to the database\n");
        }
    }

【问题讨论】:

  • 第 22 行是什么?那是echo get_db... 行吗?
  • index.php 中显示设置$row 的代码。
  • 是的 :) 10 个字符
  • 什么意思?这就是调用 get_db(); 时的工作原理
  • 如果你调用get_db($row['session_id']),它必须在调用函数之前获取$row的值,并将它的元素作为参数传递给函数。

标签: php html mysql pdo web


【解决方案1】:

函数应该将它们作为字符串返回,而不是从数据库中回显值。

function get_db(){
    $dsn = "mysql:host=".$GLOBALS["db_host"].";dbname=".$GLOBALS["db_name"];
    $dsn = $GLOBALS["dsn"];
    $result = '';
    try {
        $pdo = new PDO($dsn, $GLOBALS["db_user"], $GLOBALS["db_pasw"]);
        $stmt = $pdo->prepare("SELECT * FROM lp_sessions");
        $stmt->execute();
        $rows = $stmt->fetchAll();
        foreach ($rows as $row) {
            $result .= $row['session_id'] . ", ";
        }
    }
    catch(PDOException $e) {
        die("Could not connect to the database\n");
    }
    return $result;
}

然后称它为:

echo get_db();

另一个选项是函数将会话 ID 作为数组返回:

function get_db(){
    $dsn = "mysql:host=".$GLOBALS["db_host"].";dbname=".$GLOBALS["db_name"];
    $dsn = $GLOBALS["dsn"];
    $result = array();
    try {
        $pdo = new PDO($dsn, $GLOBALS["db_user"], $GLOBALS["db_pasw"]);
        $stmt = $pdo->prepare("SELECT * FROM lp_sessions");
        $stmt->execute();
        $rows = $stmt->fetchAll();
        foreach ($rows as $row) {
            $result[] = $row['session_id'];
        }
    }
    catch(PDOException $e) {
        die("Could not connect to the database\n");
    }
    return $result;
}

然后你会使用它:

$sessions = get_db(); // $sessions is an array

然后调用者可以使用数组中的值,也许将它们用作其他调用中的键,而不仅仅是打印它们。

【讨论】:

  • 这工作有点:) 我没有得到错误!我现在看到我处理它有点错误(我的错!!!!)我想像这样使用它: echo get_db($row['session_id']);和 $row['data'] 和 $row['started'] 都使用相同的功能,这可能吗?
【解决方案2】:

正如 antoox 所说,但是一个完整的变更集;在两个地方将行更改为行:

        $rows = $stmt->fetchAll();
        foreach ($rows as $row) {
            echo $row['session_id'] . ", ";
        }

将其放在脚本开头<?php 行之后会输出有趣的警告:

error_reporting(E_ALL|E_NOTICE);

要只输出一行,假设数据库表有一个名为 id 的字段,并且您要获取 id=1234 的行:

$stmt = $pdo->prepare("SELECT * FROM lp_sessions WHERE id=?");
$stmt->bindValue(1, "1234", PDO::PARAM_STR);

我选择了 PDO::PARAM_STR 因为它可以处理字符串和整数。

【讨论】:

  • 还是一样:注意:未定义变量:第 23 行 C:\wamp\www\Wordpress ish\index.php 中的行
猜你喜欢
  • 1970-01-01
  • 2019-05-19
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 2017-05-27
  • 1970-01-01
相关资源
最近更新 更多