How does php output to screen + using mysql to show values on screen?
I'm still relatively new to php.
I created a sub folder below my 开发者_C百科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!
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 {
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;
A ha, just found out the php version is 5.2 so won't have namespaces.
Thanks for all the other answers.
精彩评论