views:

1238

answers:

11

Hello,

I have the following part of an AJAX application, which gives no errors, but also nothing is displayed to the screen, so I am unsure of where the problem lies. Calling this page directly from a browser with ?cmd&id=1 should return, or even calling it without ?cmd should return the cmd error message.

edit: added test cases: I do get the cmd error message, but when I pass &id=1 (1 is a valid id), no html gets returned whatsoever, view source is completely blank. Have I used echo incorrectly or something similar?

edit2: added echo as first line: the first echo is not seen whatsoever

edit3: After going back to an older version and making all the changes again, I now get test charset output when calling with valid cmd and id paramters. The code I am using is identical to what is pasted below.

the code:

<?php
echo "hello world";    
error_reporting(E_ALL);

if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"];
else
  die("You should have a 'cmd' parameter in your URL");

$id = $_GET["id"];

$con = mysqli_connect("localhost", "user", "password", "db");
echo "test con";

if(!$con)
{
  die('Connection failed because of' .mysqli_connect_error());
  echo "test error";
}

//$con->query("SET NAMES 'utf8'"); 
$con->set_charset("utf8"));
echo "test charset";

if($cmd=="GetSALEData")
{
  echo "test cmdifloop";

  if ($getRecords = $con->prepare("SELECT * FROM SALES WHERE PRODUCT_NO = ?"))
  {
    echo "test recordifloop";

    $getHtml = $con->prepare("SELECT PRODUCT_DESC FROM SALES WHERE PRODUCT_NO = ?");
    $getHtml->bind_param("s", $id);
    $getHtml->execute();
    $getHtml->bind_result($PRODUCT_DESC);

    $getRecords->bind_param("s", $id);
    $getRecords->execute(); 
    $getRecords->bind_result($PRODUCT_NO, $PRODUCT_NAME, $SUBTITLE, $CURRENT_PRICE, $START_PRICE,
                             $PRICE_COUNT, $QUANT_TOTAL, $QUANT_SOLD, $ACCESSSTARTS, $ACCESSENDS,
                             $ACCESSORIGIN_END, $USERNAME, $BEST_PRICEDER_ID, $FINISHED, $WATCH,
                             $BUYITNOW_PRICE, $PIC_URL, $PRIVATE_SALE, $SALE_TYPE, $ACCESSINSERT_DATE,
                             $ACCESSUPDATE_DATE, $CAT_DESC, $CAT_PATH, $COUNTRYCODE, $LOCATION,
                             $CONDITIONS, $REVISED, $PAYPAL_ACCEPT, $PRE_TERMINATED, $SHIPPING_TO,
                             $FEE_INSERTION, $FEE_FINAL, $FEE_LISTING, $PIC_XXL, $PIC_DIASHOW,
                             $PIC_COUNT, $ITEM_SITE_ID
                            ); 
    while ($getRecords->fetch()) 
    {
      $ccodes = array(  "1" => "USA",
                       "77" => "Germany",
                       "16" => "Austria",
                      "122" => "Luxemburg",
                      "193" => "Switzerland",
                     );

      $conditions = array( "0" => "USA",
                          "77" => "Germany",
                          "16" => "Austria",
                         );

      $country = $ccodes[$COUNTRYCODE];
      if ( $country == "" ) $country = "Not applicable";

      $columns = array('FINISHED', 'WATCH', 'PRIVATE_SALE', 'REVISED', 'PAYPAL_ACCEPT', 'PRE_TERMINATED', 'PIC_XXL', 'PIC_DIASHOW');

      foreach($columns as $column) {
        $$column = $row[$column] ? 'YES' : 'NO';
      }

      imageResize($PIC_URL, 250, 300);
      file_put_contents($id, file_get_contents($PIC_URL));

      $html = htmlentities(json_encode($PRODUCT_DESC));
      $shortDate = strftime("%d %m %Y", strtotime($ACCESSSTARTS));

      echo "<h1>".$PRODUCT_NAME."</h1>
<div id='leftlayer' class='leftlayer'>
<p><strong>Username: </strong>".$USERNAME."
<p><strong>PRODUCT Number: </strong>".$PRODUCT_NO."
<p><strong>Subtitle: </strong>".$SUBTITLE."
<p><strong>SALE Start: </strong>".$ACCESSSTARTS." 
<p><strong>SALE End: </strong>".$ACCESSENDS."
<p><strong>SALE Type: </strong>".$SALE_TYPE."
<p><strong>Category: </strong>".$CAT_DESC."
</div>
<div class='leftlayer2'>
  <p><strong>Condition: </strong> ".$CURRENT_PRICE."
  <p><strong>Total Items: </strong> ".$QUANT_TOTAL."
  <p><strong>Total Sales: </strong> ".$QUANT_SOLD."
  <p><strong>Start Price: &#8364</strong> ".$START_PRICE."
  <p><strong>Buyitnow Price: &#8364</strong> ".$BUYITNOW_PRICE."
  <p><strong>PRICEs: </strong> ".$PRICE_COUNT." 
  <p><strong>Revised: </strong> ".$REVISED."
</div>
<div class='leftlayer2'>
  <p><strong>Private: </strong> ".$PRIVATE_SALE."
  <p><strong>Finished: </strong> ".$FINISHED."
  <p><strong>Cancelled: </strong> ".$PRE_TERMINATED."
  <p><strong>Paypal: </strong> ".$PAYPAL_ACCEPT."
  <p><strong>Country: </strong> ". $country ."
  <p><strong>Location: </strong> ".$LOCATION."
  <p><strong>Shipping to: </strong> ". $country ."
</div>
<div id='rightlayer'>
 <img src='".$PIC_URL."' width='".$imageSize["width"]."' height='".$imageSize["height"]."'>
 <p><a href='#' onclick=\"makewindows(" . $html . "); return false;\">Click for full description </a></p>
</div>
</div>
</div>";

    } 
  }

  function imageResize($imageURL, $maxWidth, $maxHeight)
  {
    $imageSize["width"] = 0;
    $imageSize["height"] = 0;

    $size = getimagesize($imageURL);
    if ($size) {
      $imageWidth  = $size[0];

      $imageHeight = $size[1];
      $wRatio = $imageWidth / $maxWidth;
      $hRatio = $imageHeight / $maxHeight;
      $maxRatio = max($wRatio, $hRatio);

      if ($maxRatio > 1) {
        $imageSize["width"] = $imageWidth / $maxRatio;
        $imageSize["height"] = $imageHeight / $maxRatio;
        return $imageSize;
      } else {
        $imageSize["width"] = $imageWidth;
        $imageSize["height"] = $imageHeight;
        return $imageSize;
      }
    } else {
      die(print_r(error_get_last()));
    }
  }

}

I apologize for the spacing, but it happens automatically when I press the code button.

+4  A: 

This will help you see your errors:

ini_set('display_errors', '1');
Filip Ekberg
still no errors are shown. I dont think it is a php error problem, just a logic problem or such.
Joshxtothe4
this won't let you see parse errors
Tom Haigh
error reporting + display errors shows parse errors
Filip Ekberg
to show parse errors, display_errors has to be enabled in a config file, not at run time
benlumley
add this directive to you .htaccess if you want to catch parse errors
Jacco
+1  A: 

May help:

  1. Monitor the Web server log (like tail -f /var/log/apache2/error.log)

  2. Use Firebug and see in the Console the AJAX response coming from your script. The error may appear there

Ivan Krechetov
I have checked the error log, all is fineI will investigate firebug.
Joshxtothe4
+5  A: 

You have an extra closing parenthesis here:

$con->set_charset("utf8"));
Mike B
fixed, but it made no difference
Joshxtothe4
This script runs fine for me after fixing this line, except that I don't have any records in my database.
mabwi
My database definitely has data in it. I can paste the table information so you can use some test data?
Joshxtothe4
+1  A: 

If none of the debug echo's are reached, then the point of failure would seem to be around the database connection, unless I'm missing something.

Check the connection details, and check you have the mysqli extension enabled.

Simon
If it were a connection problem, would it not display the error from die? If mysqli were not enabled, would that not give an error in the error log?
Joshxtothe4
@Josh you don't have an or die() catch on your connection attempt. You should.
Jonathan Sampson
Hi Jonathan, surely the line after echo test con would catch any connection errors?
Joshxtothe4
A: 

I have encountered a similar issue where I would inexplicably get a blank page, usually in IE6 but sometimes in other browsers. It turned out to be a character encoding problem. I was outputting some strange characters, and the browsers were guessing what the encoding was based on the content. I fixed it by explicitly setting this header:

header('Content-type: text/html; charset=utf-8');
Liam
This made no difference
Joshxtothe4
+2  A: 

Have you tried a basic test of placing a print or echo statement immediately at the top of the script to test output?

<?php

  print "Test";

?>

I would also suggest using an or die($msg) catch after your connect attempt.

Would be fine for your case. Just as a starting-point. I do this from time to time when needed. Rather than assuming the issue is some massively-complex problem, it often times turns out to be unexpected condition in request variables, accessing wrong page, accessing right page at wrong time, etc.

Jonathan Sampson
+1 for this one! lool... i like it ... but why not phpinfo()
Andreas Niedermair
Josh, you need to try this.
Joe Philllips
Isnt my line if (!con) catching any errors? The fact that the page outputs an error if cmd is not passed must mean it is working in part yes?
Joshxtothe4
I added echo "hello world" right after <?php and still nothing was output
Joshxtothe4
@Josh - empty the document. ONLY have print "Hello World"; and try that.
Jonathan Sampson
Put ?> at the end of the file just to humor me
Joe Philllips
hello world works when it is the only statement in the file. Putting ?> at the end made no difference.
Joshxtothe4
I now get test charset displayed.
Joshxtothe4
+1  A: 

Calling this page directly from a browser with ?cmd&id=1 should return

What do you mean by "return"? From what I can see of your code, you need ?cmd=GetSALEData or else a blank page will be returned, because everything is wrapped inside of the if($cmd=="GetSALEData") statement.

Correction: You should see "test contest charset" if your query string has cmd set to something other than "GetSALEData". (Those two tests were added to the code later.)

Kip
echo "Test Con"; is not within the condition.
Jonathan Sampson
That line was not in the original code posted...
Kip
Perhaps "Community Code" would be a nice feature to StackOverflow.
Jonathan Sampson
?cmd=blah still outputs nothing. By return I meant output or display to the browser.
Joshxtothe4
?cmd=blah doesn't even output the "test contest charset" code?? If you just put echo "hello world" as the first line of the file, do you even see that??
Kip
@Kip: nope. The only output I get is by omitting a cmd paramter
Joshxtothe4
+1  A: 
$getHtml->bind_param("s", $id);

I don't know if this would cause the error, but shouldn't that and the getRecords bind_param have "i" as the first parameter?

R. Bemrose
A: 

Put parentheses around your if-else statements. I have a feeling the person who edited your code may have "fixed" a potential problem there.

Joe Philllips
A: 

There are several good suggestions here for trying. The one that I didn't see yet is to make sure there is enough memory allocated to PHP. The folks over are drupal.org have a good break down of the ways to increase PHP's memory allocation.

acrosman
+1  A: 

You have an extra parenthesis on line 22

This

$con->set_charset("utf8"));

Needs to be this

$con->set_charset("utf8");

Two debugging tips for the future. For the first, you'll need shell access to your web host, or you'll need to install PHP locally on your development machine. If you're running Unix, you may already have it. Open a terminal and type

php -v

If you have it, you can check the syntax of a PHP file by doing

//on *nix
php yourfile.php

//or on windows
c:\path\to\php.exe yourfile.php

This will bail on gross syntax errors. Also, google/search this site for "setup php locally tutorial" or something similar to learn how to get a full copy of A Webserver, mysql database and php running on your own machine.

The second suggestion, which will be easier if you install a local copy, is to check your error logs. Even when PHP isn't displaying error messages, errors will still be logged to a file somewhere. In your case, you'd have seen something like this in the log file

PHP Parse error:  syntax error, unexpected ')' in foo.php on line 22
Alan Storm