1

I am trying to automate some email when our company is done with an install, below is he code that populates the variables, but when I go to use them it does not contain any of the data. What am I doing wrong.

$panelNumber = "";
    $physicalAddress = "";
    $city = "";
    $state = "";
    $releaseNumber = "";
    $matrix = "";
    $dateOfInstall = "";
    $serverIP = "";
    $webcamIP = "";
    $webcamPort = "";
    $useLan = 0;
    $WANIP = "";
    $from= 'NIW@yesco.com';
    $from_name=' Yesco Electronics Network Operations Center';
    $market_id ="";
    $Cname="";
    $Cemail= "";

    $existingQueryResult = mysql_query("SELECT *
                                    FROM assets_new
                                    WHERE asset='" . $assetName . "'");

    $num_results = mysql_num_rows($existingQueryResult); 

    if ($num_results > 0)
    {
        while($row = mysql_fetch_array($existingQueryResult))
        {
            $panelNumber = $row['panel'];
            $physicalAddress = $row['location'];
            $city = $row['city'];
            $state = $row['state'];
            $releaseNumber = $row['release_num'];
            $matrix = $row['matrix'];
            $dateOfInstall = $row['install_dt'];
            $serverIP = $row['lansideip'];
            $webcamIP = $row['webcamip'];
            $webcamPort = $row['webcamport'];
            $useLan = $row['uselan'];
            $WANIP = $row['ipaddress'];
            $market_id= $row['market_id'];
        }
    }

    $CnameQueryResult = mysql_query("SELECT c.name, m.email
    FROM markets m, customers c
    WHERE m.customer_id = c.id
    AND m.id ='".$market_id."'");

    $num_results = mysql_num_rows($CnameQueryResult); 
    if ($num_results > 0)
    {
        while ($row = mysql_fetch_array($CnameQueryResult))
        {
            $Cname= $row['name'];
            $Cemail= $row['email'];
        }
    }
CodeMonkey
  • 1,087
  • 2
  • 15
  • 24
  • Welcome to Stack Overflow! You are not doing any error checking in your queries. While not necessarily rlated to your problem, you should do that - otherwise, your script will break if the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Jan 14 '12 at 22:38
  • The only location where I see $assetName is when you're trying to obtain the value of the variable. Where are you initializing/setting it? – JustinDanielson Jan 14 '12 at 22:39
  • I initialize it further up in the code. I only posted the code with the variables that were not working, $assetName works. – CodeMonkey Jan 14 '12 at 22:47
  • 2
    is that while loop necessary? try it without the loop. – Different55 Jan 14 '12 at 23:27

2 Answers2

1

Troubleshooting PHP can be painful without a way to step through the code. My suggestion is to echo out things during testing so you can follow the progression.

For example, add a die to your queries so you can see failures:

$existingQueryResult = mysql_query("<query>") or die('Error: ' . mysql_error());

If it didn't fail, did it have any results?

$num_results = mysql_num_rows($existingQueryResult);
echo 'Results: ' . $num_results;

If it did, did the loop get the data you expected? (And as @Different55 eluded to, you don't need a loop if you only expect one result.)

$row = mysql_fetch_array($existingQueryResult);
$panelNumber = $row['panel'];
...
echo 'Panel: ' . $panelNumber;

Also check out mysql_fetch_assoc because I believe mysql_fetch_array just returns an array with numeric indexes whereas mysql_fetch_assoc gets you an array with named indexes. I am thinking $row['panel'] will contain no data unless you use an associative array instead.

Finally, it is helpful to make a debugger function that you can easily find and remove after debugging. Echoing out variables is helpful but if you solve the problem and accidentally leave one in, you will wind up with something "strange" for users to see.

I have a debugger function named pr which simply echoes out the variable in a fixed-width style so it's easy to see array contents. Removal after debugging is as easy as searching for pr(.

function pr($ar, $vn='undefined')
{
    echo '<span style="font-family: Lucida Console; font-size: 8pt; white-space: pre; display: block; color: #66F;">';
    echo strtoupper($vn) . ':';
    if(gettype($ar) != 'array') echo ucfirst(gettype($ar)) . ' ';
    print_r($ar);
    echo '</span>';
}

You would use it like this:

pr($panelNumber, 'panelnumber');

// PANELNUMBER:String asdf

Good luck!

JYelton
  • 35,664
  • 27
  • 132
  • 191
  • The obvious flaw is the mysql_fetch_array call. – dar7yl Jan 16 '12 at 23:34
  • Reading up on `mysql_fetch_array`, it turns out that it probably isn't at fault. From the [manual page](http://php.net/manual/en/function.mysql-fetch-array.php): *The type of returned array depends on how `result_type` is defined. By using `MYSQL_BOTH` (default), you'll get an array with both associative and number indices.* – JYelton Jan 17 '12 at 01:18
  • my bad. I was confusing it with mysql_fetch_row() – dar7yl Jan 17 '12 at 01:27
0

After months of beating my head against a wall I figured it out. To finish what we call an install the following functions were being called. 1. write_to_pdf() 2. delete_info() 3. send_email()

As you can see, I was deleting the info from the tables before sending the email. So the email functions were working fine. They were sending exactly what I asked them to. Thanks for everyone's help and contributions.

CodeMonkey
  • 1,087
  • 2
  • 15
  • 24