2013-05-21 91 views
0

我正在建立一个使用php和html的网站,我用来从数据库接收数据,又名动态网站,我已经建立了一个CMS供我自己使用。我试图“简化”使用php和函数的接收过程。从一个函数运行调用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']; 我想这样称呼它: 下面的代码段是从索引。 php

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

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

Notice: Undefined variable: row in C:\wamp\www\Wordpress ish\index.php on line 22

我还使用PDO只是让你会知道:)

任何帮助,非常感谢!

问候 了Stian

编辑:更新的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"); 
     } 
    } 
+0

什么是第22行?那是'echo get_db ...'行吗? – Barmar

+0

在'index.php'中显示设置'$ row'的代码。 – Barmar

+0

是的,它是:) 10 char –

回答

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 

和呼叫者可以t在使用数组中的值时,可能使用它们作为其他调用中的键而不是仅打印它们。

+0

这工作有点:)我没有得到错误!我现在看到,我处理它有点不对(我的坏!!!!)我想要使用它:echo get_db($ row ['session_id']);和$ row ['data']和$ row ['started']都使用相同的函数,这可能吗? –

1

正如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,因为它会与两个字符串工作,整数。

+0

仍然是一样的:注意:未定义的变量:在C:\ wamp \ www \ Wordpress中的行\ isw \ index.php 23行 –