php php给出一个空白页面,没有错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/487301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:54:00  来源:igfitidea点击:

php giving a blank page, no errors

phpmysqlajaxmysqli

提问by Joshxtothe4

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.

我有一个 AJAX 应用程序的以下部分,它没有错误,但屏幕上也没有显示任何内容,所以我不确定问题出在哪里。使用 ?cmd&id=1 直接从浏览器调用此页面应该返回,或者甚至在没有 ?cmd 的情况下调用它应该返回 cmd 错误消息。

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?

编辑:添加了测试用例:我确实收到了 cmd 错误消息,但是当我通过 &id=1(1 是有效 ID)时,没有返回任何 html,查看源完全空白。我是否错误地使用了 echo 或类似的东西?

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

编辑2:添加回声作为第一行:第一个回声看不到任何东西

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.

edit3:回到旧版本并再次进行所有更改后,我现在在使用有效的 cmd 和 id 参数调用时获得测试字符集输出。我使用的代码与下面粘贴的代码相同。

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.

我为间距道歉,但是当我按下代码按钮时它会自动发生。

回答by Filip Ekberg

This will help you see your errors:

这将帮助您查看错误:

ini_set('display_errors', '1');

回答by Alan Storm

You have an extra parenthesis on line 22

第 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

未来的两个调试技巧。首先,您需要对 Web 主机进行 shell 访问,或者需要在开发机器上本地安装 PHP。如果您正在运行 Unix,您可能已经拥有它。打开终端并输入

php -v

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

如果你有它,你可以通过执行检查 PHP 文件的语法

//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.

这将避免严重的语法错误。此外,谷歌/搜索此站点以查找“本地设置 php 教程”或类似内容,以了解如何获取在您自己的机器上运行的网络服务器、mysql 数据库和 php 的完整副本。

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 不显示错误消息,错误仍会记录到某个文件中。在您的情况下,您会在日志文件中看到类似的内容

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

回答by Mike B

You have an extra closing parenthesis here:

这里有一个额外的右括号:

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

回答by Sampson

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

您是否尝试过将 print 或 echo 语句立即放置在脚本顶部以测试输出的基本测试?

<?php

  print "Test";

?>

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

我还建议or die($msg)您在尝试连接后使用catch。

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.

适合你的情况。只是作为一个起点。我会在需要时不时这样做。与其假设问题是一些非常复杂的问题,不如说是请求变量中的意外情况,访问错误的页面,在错误的时间访问正确的页面等。

回答by Ivan Krechetov

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

  1. 监视 Web 服务器日志(如tail -f /var/log/apache2/error.log

  2. 使用 Firebug 并在控制台中查看来自脚本的 AJAX 响应。错误可能出现在那里

回答by Simon

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.

检查连接详细信息,并检查是否启用了 mysqli 扩展。

回答by Liam

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:

我遇到过类似的问题,我会莫名其妙地得到一个空白页面,通常在 IE6 中,但有时在其他浏览器中。结果证明是字符编码问题。我输出了一些奇怪的字符,浏览器根据内容猜测编码是什么。我通过显式设置此标头来修复它:

header('Content-type: text/html; charset=utf-8');

回答by Kip

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

直接从浏览器调用这个页面?cmd&id=1应该返回

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

你说的“回归”是什么意思?从我对您的代码的观察来看,您需要?cmd=GetSALEData否则将返回一个空白页,因为所有内容都包含在if($cmd=="GetSALEData")语句中。

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

更正test contest charset如果您的查询字符串cmd设置为“GetSALEData”以外的其他内容,您应该看到“ ”。(这两个测试后来被添加到代码中。)

回答by Powerlord

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

我不知道这是否会导致错误,但不应该这样吗,getRecords bind_param 不应该将“i”作为第一个参数吗?

回答by Joe Phillips

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

将括号括在 if-else 语句周围。我有一种感觉,编辑您的代码的人可能已经“修复”了那里的潜在问题。