【问题标题】:Php working on localhost but not on serverPhp在本地主机上工作但不在服务器上
【发布时间】:2013-04-28 17:31:13
【问题描述】:

我一直在编写一些代码并在 localhost 上对其进行了测试,一切正常,但是当我将其上传到我的服务器时,它在我的 php 标签之外无法正常工作。也没有显示错误。我检查了两个 php 版本,在 localhost 上我运行版本 5.4.7,在服务器上运行版本 5.3.21。也许这是造成问题的原因?我应该在 phpinfo() 中寻找什么吗?我的代码中是否缺少某些内容? 这是代码:

    <!DOCTYPE html>
    <?php phpinfo(); ?>
    <html>
    <head>

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <meta charset="utf-8">

    <style>
     body { background:black;}
    .toggle { display:none; }

    p {  
  width:570px;
  text-align:justify;
  color:#686868;
  font-family:Georgia, serif;
      }

    h2 { color:black; } 

    button { background:#686868; 
     border:none;
     font-family:Georgia, serif;
     width:570px;
   }
    </style>
    </head>

    <body>
    <?php 
    include('sql.php');

    $i = 0;

    while($i < count($rows)) {
    echo "
    <div>
        <button class='trigger'>
    <table width='100%'>
    <tr>
        <td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />             
                  </td>
        <td width='80%'><h2>{$rows[$i]['titel']} </h2></td> 
    </tr>
    </table>

</button>
       <div class='toggle'>
     <p>
       {$rows[$i]['info']}
     </p>   
   </div>
    </div>";


    $i++;
    }?>

    </body>
    <script>

    $('.trigger').click(function() { 

    $(this).siblings('.toggle').slideToggle('fast');

    });

   </script>
   </html>

当我运行它时,它会显示黑色背景(正如它所假设的那样),但我的 php 起始标记之外的所有内容都会被切断。 我还尝试强制我的 while 循环循环 10 次,并且还删除了它从我的数据库中获取数据的部分,以查看它是否是与 mysql 相关的问题。我可以断定它不是。

【问题讨论】:

    标签: php html


    【解决方案1】:

    下面一行是问题:

    <img src='http://localhost/app-side/Icons/bar_icon.png
    

    图像无法加载,因为localhost 指的是客户端的本地计算机(浏览器运行的地方)而不是您的服务器。通常网站中的此类 url 被认为是恶意的 ;)

    使其在localhostserver 上都工作的解决方法是使用相对路径。这可以相对于您的文档或相对于您的服务器的DOCUMENT_ROOT。我使用了第二种方法,因为我不知道您的 php 文件在服务器上的位置。

    DOCUMENT_ROOT相关的链接的解决方案:

    替换:

    <img src='http://localhost/app-side/Icons/bar_icon.png
    

    通过

    <img src='/app-side/Icons/bar_icon.png
    

    【讨论】:

    • 但即使我将其更改为正确路径,除了黑色背景之外,它仍然没有显示任何内容。
    • 在浏览器中为您提供“显示页面源...”
    • 问题是,我无法到达显示图像的地步。我试图删除图像部分,它仍然只显示黑色背景,当我检查页面时它说正文是空的。所以它似乎因为某种原因切断了我的 php。
    • 我担心,那么它与编程无关。应移至 superuser.com。 (或在那里再次询问)
    【解决方案2】:

    我唯一可以假设的是$rows 是空的,因此以下代码不会运行:

    // count($rows) could be equal to 0
    while($i < count($rows)) { // this condition becomes false in that case
        echo "
        <div>
            <button class='trigger'>
        <table width='100%'>
        <tr>
            <td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />             
                      </td>
            <td width='80%'><h2>{$rows[$i]['titel']} </h2></td> 
        </tr>
        </table>
    
    </button>
           <div class='toggle'>
         <p>
           {$rows[$i]['info']}
         </p>   
       </div>
        </div>";
    
    
        $i++;
        }
    

    更新

    根据您的评论,这可能是由于sql.php 内部的某些东西,很可能是数据库连接问题。尝试将error_reporting(E_ALL) 放在页面的最顶部,看看是否会打印出任何错误。

    【讨论】:

    • 我也通过将循环更改为运行 10 次而不是 count($rows) 并在按钮和

      中放置“hello”而不是数据库中的数据来测试这一点.发生同样的事情,没有显示任何按钮。

    【解决方案3】:

    您是否检查过您的 sql.php 以确保您已更改它以匹配您的实时服务器(从 localhost 变量更改)并检查您的数据库字段名称与您的脚本是否有拼写错误。

    [code] 
    {$rows[$i]['titel']} </h2></td>
    [\code]
    

    它应该是: [代码]

    {$rows[$i]['title']} </h2></td>
    
    [\code]
    

    如果您的数据库字段名称是“title”并且 您正在尝试从名为 “标题”可能会导致您的错误。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2020-12-07
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多