-1

A little help if possible. I have a Page that pulls from two data tables (MySQL) and one function is providing empty results.

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>';

}

The $agent variable is pulled from a main function that creates a url based on the user ($agent).

What am I doing wrong here?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • 1
    Obligatory - any E_NOTICE or above issued? Have you double-checked your SQL query is valid before asking PHP to run it? – Lee S May 22 '14 at 00:30
  • 1
    (not the problem) Please don't build SQL queries that way. Use placeholders and array-based binding with https://php.net/manual/en/book.pdo.php – David-SkyMesh May 22 '14 at 00:30
  • [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil May 22 '14 at 00:55

2 Answers2

0

$agent is a global variable. Using global variables is generally considered bad practice, as this could be getting set or unset somewhere before this function is called.

Have you checked the PHP Error Log to see if you are getting any errors?

If no errors in log I would look to see if $agent conatins a value either by echoing to screen (if dev environment) or dumping the value in the error log file to see if it actually contains anything. http://www.php.net/manual/en/function.error-log.php

Then I would look at the SQL itself; do the Column headings in your table nuke_bulletins match the $row array keys exactly for instance are they the same case?

$row['title']

or

$row['Title']
mrjamesmyers
  • 454
  • 4
  • 13
0

Here we go...

  1. Don't use the mysql extension. It is unmaintained and officially deprecated
  2. Don't use globals. Relying on external state makes for smelly code
  3. You're overwriting said global variable ($agent) in a loop. Terrible idea.
  4. or die must die ~ http://www.phpfreaks.com/blog/or-die-must-die
  5. I wouldn't recommend using echo within a function. Makes for spaghetti code
  6. Your HTML is a bit of a mess

Here's my suggestion using the mysqli extension

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;
}

Now you can call this function like this...

<?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>

And, as always, your development environment should have the following properties set in your php.ini file

display_errors = On
error_reporting = E_ALL
Phil
  • 157,677
  • 23
  • 242
  • 245