从PHP函数运行调用
我正在用php和html建立一个网站,我习惯于从数据库接收数据,也就是动态网站,我已经建立了一个CMS供我自己使用。 我试图使用php和函数“简化”接收过程 My Functions.php如下所示:从PHP函数运行调用,php,html,mysql,pdo,web,Php,Html,Mysql,Pdo,Web,我正在用php和html建立一个网站,我习惯于从数据库接收数据,也就是动态网站,我已经建立了一个CMS供我自己使用。 我试图使用php和函数“简化”接收过程 My Functions.php如下所示: function get_db($row){ $dsn = "mysql:host=".$GLOBALS["db_host"].";dbname=".$GLOBALS["db_name"]; $dsn = $GLOBALS["dsn"]; try {
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']代码>
我试着这样称呼它:
下面的代码段来自index.php
echo get_db($row['session_id']); // Line 22
只是为了显示所有行中的内容。
当我运行该代码段时,会出现以下错误:
注意:未定义变量:C:\wamp\www\Wordpress ish\index.php中的行
第22行
我也在使用PDO,只是想让您知道:)
非常感谢您的帮助
问候
斯蒂安
编辑:Updated 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");
}
}
正如安托克斯所说,这是一个完全的改变;将行更改为两处的行:
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
echo $row['session_id'] . ", ";
}
将其放在脚本开头的之后,函数应该以字符串形式返回值,而不是回显DB中的值
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
然后调用者可以使用数组中的值,也许可以在其他一些调用中使用它们作为键,而不只是打印它们。第22行是什么?这是echo get_db…
行吗?在index.php
中显示设置$row
的代码。是:)10字符你是什么意思?这就是调用get_db()时的工作原理;如果调用get\u db($row['session\u id')
,它必须在调用函数之前获取$row
的值,并将其元素作为参数传递给函数。仍然相同:注意:未定义的变量:C:\wamp\www\Wordpress ish\index.php第23行中的row这类:)我没有得到错误!现在我发现我处理的有点错误(我的坏!!!!),我想像这样使用它:echo get_db($row['session_id');$row['data']和$row['started']都使用相同的函数,这可能吗?
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