php如何输出到屏幕+使用mysql在屏幕上显示值?

发布于 2024-11-18 07:41:37 字数 1101 浏览 9 评论 0原文

我对 php 还比较陌生。

我在我的 webroot 下创建了一个名为 lib 的子文件夹,并添加了 test.php

然后添加了以下代码...

<?php
new \Wisdom\Question();
?>

然后我在 lib 中创建了另一个名为“Wisdom”的文件夹和一个名为“Question”的类,其中包含以下代码...

<?php

class Question {

function __construct()
{
    $user="username";
    $password="password";
    $database="dbname";
    mysql_connect(localhost,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query = "SELECT * FROM questions";
    $result = mysql_query($query);
    $num = mysql_numrows($result);
    mysql_close();
    echo "<b><center>Database Output</center></b><br><br>";

    $i=0;

    while($i < $num)
    {
        $description = \mysql_result($result,$i, "description");
        $id = \mysql_result($result,$i, "id");
        echo "<b>$id $description</b>";
        $i++;
    }

}

}

当我在浏览器中查看 test.php 时,它只显示错误 500(另一方面,如何在屏幕上显示 php 错误?我尝试了 ERROR_REPORTING(E_ALL) 但它没有执行任何操作。但它从未显示任何错误的回声在数据库连接期间,我的数据库具有预期的所有字段,谢谢!

I'm still relatively new to php.

I created a sub folder below my webroot called lib and added test.php

I then added the following code...

<?php
new \Wisdom\Question();
?>

I then created another folder within lib called "Wisdom" and a class called "Question" with the following code...

<?php

class Question {

function __construct()
{
    $user="username";
    $password="password";
    $database="dbname";
    mysql_connect(localhost,$user,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query = "SELECT * FROM questions";
    $result = mysql_query($query);
    $num = mysql_numrows($result);
    mysql_close();
    echo "<b><center>Database Output</center></b><br><br>";

    $i=0;

    while($i < $num)
    {
        $description = \mysql_result($result,$i, "description");
        $id = \mysql_result($result,$i, "id");
        echo "<b>$id $description</b>";
        $i++;
    }

}

}

When I view the test.php in a browser, it just shows an error 500 (on another note, how do I show php errors on screen? I tried ERROR_REPORTING(E_ALL) but it didn't do anything. But it never shows any of the echos during the db connect. My db has all the fields as expected. Any ideas? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赢得她心 2024-11-25 07:41:37

从你的问题来看,很难说出你面临着什么样的错误。 500 内部服务器错误可能有多种原因。它可能是 PHP 关心的事情,但也可能是网络服务器关心的事情。

因此,我实际上可以给出的最佳答案是,您正确配置 PHP 错误处理 /PHP错误配置然后查​​看php错误日志。

同时,您还需要检查网络服务器错误日志。

除此之外,由于您发布的代码,我已经有了一些想法,但这可能会产生误导:

您尝试实现的目标需要启用自动加载器。当您尝试实例化它时,类 \Wisdom\Question 看起来不存在:

new \Wisdom\Question();

需要自动加载器来实际从目录/文件布局加载不存在的类,就像您似乎使用的那样(我不能具体说,因为你的问题没有提供更多信息)。

对于自动加载器,您似乎可以使用任何与 PSR-0 兼容的自动加载器,例如这个

除此之外,类定义还需要在顶部正确的命名空间

<?php
namespace Wisdom;
class Question {

From your question it's not easy to say what kind of error you are facing. The 500 internal server error can have multiple reasons. It can be something PHP is concerned about, but it can be something with the webserver as well.

So the best answer I can actually give is that you properly configure your PHP error handling / PHP error configuration and then check the php error log.

In parallel you need to check your webservers error log as well.

Next to that, these are some thoughts I had already because of the code you posted, but this might be misleading:

What you try to achieve needs an autoloader to be enabled. It looks like that the class \Wisdom\Question does not exists when you try to instantiate it:

new \Wisdom\Question();

The autoloader is needed to actually load non-existent classes from the directory/file-layout like you seem to use (I can't specifically say because your question does not give more information).

For the autoloader it looks like you can use any autoloader that is PSR-0 compatible, for example this one.

Next to that, the class definition needs as well the correct Namespace on top:

<?php
namespace Wisdom;
class Question {
柳若烟 2024-11-25 07:41:37

\Wisdom\Question 语法适用于 命名空间,而不是文件夹。您需要加载包含该类的文件,这是无法使用该语法完成的。使用一些简单的东西,例如:

require 'Wisdom/Question.php';
new Question;

The \Wisdom\Question syntax is for namespaces, not folders. You need to load the file that contains the class, that cannot be done with that syntax. Use something simple like:

require 'Wisdom/Question.php';
new Question;
幻想少年梦 2024-11-25 07:41:37

哈哈,刚刚发现 php 版本是 5.2,所以没有命名空间。

感谢所有其他答案。

A ha, just found out the php version is 5.2 so won't have namespaces.

Thanks for all the other answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文