Php 无法使用PDO语句和全局变量调用和执行函数
我有一个简单的代码,其中包含select和insert PDO语句。。 在这段代码中,我在全局变量$p_id的基础上插入数据。我没有使用函数就执行了代码。但是现在在我的代码中有了函数,它就不起作用了。这是密码-Php 无法使用PDO语句和全局变量调用和执行函数,php,function,pdo,global-variables,Php,Function,Pdo,Global Variables,我有一个简单的代码,其中包含select和insert PDO语句。。 在这段代码中,我在全局变量$p_id的基础上插入数据。我没有使用函数就执行了代码。但是现在在我的代码中有了函数,它就不起作用了。这是密码- <?php include "connection.php"; $p_id; if (!empty($_POST['prod_sub'])) { $sth = $dbo->prepare("SELECT produc
<?php
include "connection.php";
$p_id;
if (!empty($_POST['prod_sub']))
{
$sth = $dbo->prepare("SELECT production_ID FROM le_planning.log_production ORDER BY production_ID DESC LIMIT 1");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$count = $sth->rowCount();
if($count > 0)
{
foreach($result as $arr)
{
$p_id = $arr['production_ID'];
$p_id = $p_id+1;
}
prodExec();
}
else // FIRST TIME INPUT
{
$p_id = 1;
prodExec();
}
function prodExec()
{
global $p_id; // GLOBAL
$q = "INSERT INTO le_planning.transactions (Type, Type_ID) VALUES ('Production', $p_id)";
$stmt = $dbo->prepare($q);
if($stmt->execute())
{echo "";}
else{echo "Error!";}
$sth1 = $dbo->prepare("SELECT * FROM le_planning.temp_prod");
$sth1->execute();
$result1 = $sth1->fetchAll(PDO::FETCH_ASSOC);
$count1 = $sth1->rowCount();
if($count1 > 0)
{
foreach($result1 as $item)
{
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$Component_ID = $item['Component_ID'];
$stock_ID = $item['stock_ID'];
$q1 = "INSERT INTO le_planning.log_production (production_ID, Transaction_ID, datetime, stock_ID, component, Component_ID, Qty) SELECT $p_id, MAX(Transaction_ID), now(), $stock_ID, '$component', '$Component_ID', $Qty FROM le_planning.transactions";
$stmt1 = $dbo->prepare($q1);
if($stmt1->execute())
echo "";
else{echo "Error!";}
}
}
}
}
?>
@davidstrachan是的,他们是!但必须有另一种选择!函数经常被使用。我从来没有想过PHP会发生如此糟糕的事情!定义函数时可以指定参数。例如函数prodExec($p_id)
@davidstrachan我尝试了函数中的每一种可能的组合,无论是使用全局关键字还是$global[]或传递参数。。仍然没有有利的结果。。
<?php
$p_id;
$count = 0;
if($count > 0)
{
$p_id=1;
prodExec(); // gives output 1 when $count = 1
}
else
{
$p_id=2;
prodExec(); // gives output 2 when $count = 0
}
function prodExec()
{
global $p_id;
echo $p_id;
}
?>