PHP函数生成空结果,无错误

PHP函数生成空结果,无错误,php,mysql,Php,Mysql,如果可能的话,给我一点帮助。我有一个从两个数据表(MySQL)提取的页面,其中一个函数提供空结果 function ShowClient() { global $agent; $sql = 'SELECT * FROM nuke_bulletins WHERE user=\'' . $agent . '\' AND isActive="Y" ORDER BY id'; $client = mysql_query($sql) or die('ERROR: OOPS Some

如果可能的话,给我一点帮助。我有一个从两个数据表(MySQL)提取的页面,其中一个函数提供空结果

function ShowClient() {
    global $agent;
    $sql = 'SELECT * FROM nuke_bulletins WHERE user=\'' . $agent . '\' AND isActive="Y" ORDER BY id';
    $client = mysql_query($sql) or die('ERROR: OOPS Something went wrong' . mysql_error());

    echo '<center><p><b>Current Campaigns</b></p>'; 

    // Pull the loop and display the data
    while($row = mysql_fetch_array($client)) {
        $agent = stripslashes($row['user']);
        $campaign = stripslashes($row['id']);
        $title = stripslashes($row['title']);       
        echo '<p><a href="bullies2.php?op=ShowCampaign&amp;id=' . $campaign . '"><b>' . $title . '</b></a></p>';
    }

    echo '<p>Click the Campaign Title to get the Bulletin Code</p><p>&nbsp;</p>';
    echo '<p align="center"><a href="bullies2.php"><b>Return to All Client\'s</a></p>';

}
函数ShowClient(){
全球代理;
$sql='SELECT*来自nuke_公告,其中user=\'.$agent.'\'和isActive=“Y”ORDER BY id';
$client=mysql\u query($sql)或die('ERROR:OOPS出错了’.mysql\u ERROR());
回应当前的活动;
//拉动循环并显示数据
while($row=mysql\u fetch\u数组($client)){
$agent=stripslashes($row['user']);
$campaign=stripslashes($row['id']);
$title=stripslashes($row['title']);
回声“

”; } echo'单击活动标题以获取公告代码; 回显“

”; }
$agent变量从基于用户($agent)创建url的主函数中提取


我做错了什么?

$agent是一个全局变量。使用全局变量通常被认为是不好的做法,因为在调用此函数之前,可能会在某个地方设置或取消设置全局变量

您是否检查过PHP错误日志以查看是否有任何错误

如果日志中没有错误,我将通过回显到屏幕(如果是开发环境)或转储错误日志文件中的值来查看$agent是否包含值,以查看它是否实际包含任何内容

然后我会看看SQL本身;表nuke_公告中的列标题是否与$row数组键完全匹配?例如,它们的大小写是否相同

$row['title']

我们走吧

  • 不要使用mysql扩展。它是未维护的,并且正式弃用
  • 不要使用globals。依赖外部状态会产生难闻的代码
  • 您正在循环中覆盖所述全局变量(
    $agent
    )。糟糕的主意
  • 或死亡
    必须死亡~
  • 我不建议在函数中使用
    echo
    。意大利面代码
  • 你的HTML有点乱
  • 下面是我使用mysqli扩展的建议

    function getCampaigns(mysqli $con, $agent) {
        if (!$stmt = $con->prepare("SELECT id, title FROM nuke_bulletins WHERE user = ? AND isActive = 'Y' ORDER BY id")) {
            throw new Exception($con->error, $con->errno);
        }
    
        $stmt->bind_param('s', $agent); // if the user column is a integer, use 'i' instead
    
        if (!$stmt->execute()) {
            throw new Exception($stmt->error, $stmt->errno);
        }
    
        $stmt->bind_result($id, $title);
    
        $campaigns = []; // or array() if you're on PHP < 5.4
    
        while ($stmt->fetch()) {
            $campaigns[$id] = $title;
        }
        return $campaigns;
    }
    

    强制性-是否发布了E_通知或更高版本?在要求PHP运行SQL查询之前,您是否仔细检查了SQL查询是否有效?(不是问题)请不要以这种方式构建SQL查询。使用占位符和基于数组的绑定
    function getCampaigns(mysqli $con, $agent) {
        if (!$stmt = $con->prepare("SELECT id, title FROM nuke_bulletins WHERE user = ? AND isActive = 'Y' ORDER BY id")) {
            throw new Exception($con->error, $con->errno);
        }
    
        $stmt->bind_param('s', $agent); // if the user column is a integer, use 'i' instead
    
        if (!$stmt->execute()) {
            throw new Exception($stmt->error, $stmt->errno);
        }
    
        $stmt->bind_result($id, $title);
    
        $campaigns = []; // or array() if you're on PHP < 5.4
    
        while ($stmt->fetch()) {
            $campaigns[$id] = $title;
        }
        return $campaigns;
    }
    
    <?php    
    // assuming you have a mysqli instance in a $con variable, eg $con = new mysqli(...)
    // and an $agent variable
    $campaigns = getCampaigns($con, $agent);
    ?>
    
    <p><strong>Current Campaigns</strong></p>
    
    <?php foreach ($campaigns as $id => $title) : ?>
        <p>
            <a href="bullies2.php?op=ShowCampaign&id=<?= $id ?>">
                <strong><?= htmlspecialchars($title) ?></strong>
            </a>
        </p>
    <?php endforeach ?>
    
    <p>Click the Campaign Title to get the Bulletin Code</p>
    <p>&nbsp;</p>
    <p align="center"><a href="bullies2.php"><strong>Return to All Client's</strong></a></p>
    
    display_errors = On
    error_reporting = E_ALL