Issue with PHP MySQL on Ubuntu 12.10 - php

I'm new to PHP, MySQL dev. I'm trying to make a simple webapp, was following a tutorial.
This is the PHP script it showed to grab info about the 'detectives' table.
<?php
$mysqli = new mysqli('localhost', 'root', 'root!', 'intrepid_detectives');
$result = $mysqli->query("SELECT * FROM detectives");
echo "<pre>";
print_r($result);
But when I open this page on my browser by going to localhost/index.php, it gives me this error:
The website encountered an error while retrieving http://localhost/index.php. It may be down for maintenance or configured incorrectly.
I asked a guy, he said the problem is with mysqli, so I searched on SO, and followed this post, but it still gives the same error.

Things to try :
can you open a simple html file in the same directory, index.html for example ?
did you try showing the errors on page with ini_set('display_errors', 1); error_reporting(E_ALL); ? Or is there something in the error.log ?
do you have a .htaccess or vhost configuration that could interfere with what you want?
Checking these will certainly point you to the right direction I hope :)

Related

PHP MySQL Error Setting

I have developed some php/mysql code on linux machine, and it runs fine. On client's machine running wamp server on windows, same operation gives a mysql error - which Php is displaying neatly in a tabular format. The error is in a mysql query with INSERT, pertaining to Duplicate Keys. (I know I can use ON DUPLICATE KEY UPDATE, but that is not the issue). Problem is, the code is supposed to handle errors gracefully.
It does on my linux machine, but on client's wamp - it displays html tables with error details. I tried try and catch around the function call, but that also does not prevent the error from getting displayed in the browser (the PHP file is supposed to emit json data for ajax calls, that is why I cannot afford to have those error display by PHP).
Also, in the php file, I have
ini_set('display_errors', 1); error_reporting(E_ALL);
and still, since its a mysql error, it rightly does not throw any php error on linux, but does on windows.
So basically, I want to know what PHP setting (or something else?) on client's machine could be causing the MySQL error to be propagated / processed by PHP, and how to turn it off.
Thanks
You might want to check your php.ini and look at these values:
xdebug.auto_trace=1
xdebug.trace_format=1
mysql.trace_mode = On
pdo_mysql.debug = On
If they are there, disable them
its better to make error_reporting(0); in start of your code when you put your code on live server , its is better to hide un necessary errors and notifications
First make sure your MySQL version numbers are the same!
If you are using mysqli try having the following:
mysqli_report(MYSQLI_REPORT_OFF);
If you want to turn it on with the Linux stack, try something like this:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try this instead
ini_set('display_errors', 0);
error_reporting(E_ERROR); OR error_reporting(0);

PHP returns blank with legit code

I've copy pasted the get facebook messages script from this website:
http://cappelendesign.no/2010/04/tutorial-facebook-rss-feed-parser-in-pure-php/
Works like a charm on two test hosts but I can't determine why it doesn't on the main..
PHP Version is 5.3.15
And SimpleXML is enabled.
I've tryed different retrieve PHP scripts from whom I know they work. They all return blank.
Any ideas on what this problem could be?
Gr.
Kevin
This usually happens when you have an error but the php is defined not to show errors.
try to add:
error_reporting(E_ALL);
ini_set('display_errors', 1);
at the begin of your code to see the errors.

PHP Zend Framework: Internal Server Error 500 on localhost when trying to run "Lucene.php" on browser

I am running windows 7 and I have a fresh install of Zend framework. On a new php file called 'search.php' I am trying to build a search application using Zend_Search_Lucene with the first line being:
<?php require_once "C:...\Zend\Search\Lucene.php";?>
What I see on the browser when I run this code is internal server error 500..I then discovered that I get this error whenever I try to run some of the files in Zend library by itself and this is what caused the error I mentioned..i.e ERROR 500 on localhost/Zend/Search/Lucene.php, localhost/Zend.../blabla.php..
Some of the files however, did not display this 500 server error when run on the browser. ie: localhost/Zend/ProgressBar.php shows an empty page, which is fine since I assume there are not any 'echo'es in the code. This is actually what I expected when I ran lucene.php on the browser...
Can somebody experienced let me know how this can happen? Why do I get internal server error instead of exception? How do I check if my search application that uses "Lucene.php" file runs properly regardless of this internal 500 server error? thanks.
Try to turn on error reporting:
on the fly
ini_set('display_errors', 1);
error_reporting(E_ALL);
in php.ini (probably different for php and cli)
error_reporting = E_ALL
display_errors = 1
For more information see:
http://php.net/manual/en/errorfunc.configuration.php
http://php.net/manual/en/function.error-reporting.php
I have finally solved the problem :) After looking at the error traces, the internal server error is because the code <?php require_once "C:...\Zend\Search\Lucene.php";?> tries to access a particular code inside "Lucene.php" that contains a relative path to the Zend library folder =require_once('Zend\Search\Document\...'); and the server does not know the path to the file. What needed to be fixed was actually my php.ini file, on the include_path i added ;C:\php5\pear;C:\Server\www\.....\ZendFramework\library..Now it shows empty page instead of internal server error.
+1 #Arend: The error reporting function is really useful! thanks
Zend's code relies on their autoloader. You get an error, since you don't initialize it, then in Zend_Search_Lucene it tries to instanciate a nonexistent class.
This should do the trick:
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
self::$zendLoaded = true;
As I cannot comment answers, I re-use Maerlyn's code :
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
self::$zendLoaded = true; cannot work as self is not defined her.
According to the ZF documentation, if your path is set correctly, these 2 lines should be enough.

Specific php question

I am fairly new to creating websites and I have done a website that I did in a sandbox server on my personal computer, it works brilliantly, but as soon as I try to put it on my companies server to go up on the internet it doesn't work anymore. What the site does is it takes fields from an html form and then does a sql search of a database that I set up and works fine. Then after the user is done with the form they hit submit and the sql is run and results are returned to the same page. The form action="" and then the results are returned. I am not sure if the companies server has php installed, but I don't know if that would be a problem or not because the browser should still be able to display the php code. I have no problems with the html form, it is just when I hit submit that the page is returned and nothing is displayed.
Any help is welcome.
"I am not sure if the companies server has php installed, but I don't know if that would be a problem"
It is. Browsers can do nothing with PHP. Maybe you shoud register for a free hosting provider and learn the basics of hosting there.
PHP is a server-side language (i.e., it runs on the server, not the user's machine). Therefore, it must be installed on the company's server for any PHP code to execute properly. Browsers cannot interpret PHP code, and moreover PHP code is executed before the browser even receives any information (PHP: Hypertext Pre-Processor). Check that the server has PHP installed before you continue. (Also be aware of versions and features. For example, to use many new PHP features your server must be running PHP5.)
If you find that PHP is installed: Check your PHP syntax; make sure that no headers are sent after text is sent to the page; and make sure that there are no loops or anything in your code that could cause the script to continuously run without printing to the page.
Also, since your page is returned when you hit 'Submit,' but no new information is shown, make sure that there is not a problem with the MySQL configuration (e.g., incorrect password, query syntax, etc).
First you have to be sure that server has PHP installed.
make a file named phpinfo.php with this line
<?php phpinfo() ?>
and call it.
If it won't print out PHP config, you have to install PHP first.
Otherwise the problem most likely in the form of PHP tags, and you are using <? instead of <?php.
To solve, you can either change tags or set short_open_tag to on in the php.ini
According to your comment
when I hit submit that the page is returned and nothing is displayed
I am going to make an assumption that you are getting a blank page. If that is correct, then I am also going to follow that up with the assumption that PHP is installed, there is an error in your script (my guess is the database connection), and that the error is not being displayed.
Add these lines to the top of your page and post back with the result:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Ya I think there is a problem in your database connection.First run your connect file whether it is showing some error or not.Plase add this line to your page:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
And please modify your php.ini file with this line:
display_errors = on

PHP5: calling external functions, and logging errors

I'm painfully new to PHP, and was trying to set up phpBB on my local site. I have a stock debian install of apache2 and php5. The phpBB installer ran fine, connected to the database and created all its tables with no problem. But when I tried to open the login page, I got a 0-byte response.
A little digging showed that it was never making it past the call to mysql_pconnect(). The php binary just quits without error or message. Nothing at all. I tried running the following code:
<?php
$id = #mysql_pconnect('localhost','myusername', 'mypassword', true);
print "id=".$id."\n";
?>
and the "id=" string never prints. It just does nothing. I don't know where to look to see what error happened, or what is going on at all. All i've installed is "mysql" using pear... perhaps I'm missing something else?
This has got to be a path problem somewhere. The mysql extension is built nicely at
/usr/lib/php5/20060613+lfs/mysql.so
Answer:
jishi: informed me that the "#" operator suppresses output, including error messages (#echo off, anyone?)
tomhaigh: extensions must be explicitly enabled in php.ini file. After adding the line "extension=mysql.so" to php.ini, the following code runs fine:
% cat d.php
<?php
ini_set('display_errors', true);
error_reporting(E_ALL | E_NOTICE);
$id = mysql_pconnect('localhost','myusername', 'mypassword', true);
print "id=".$id."\n";
?>
% php -c /etc/php5/apache2/php.ini d.php
id=Resource id #4
JOY!
Just noted that you're using a # in front of mysql_pconnect(). That suppresses all errors, which in this case is a pretty bad idea. Remove that and you would probably see the output.
Otherwise:
Check your php.ini, should be in /etc/php5/apache2/php.ini for debian.
Check for a line called display_errors, set that to true if you want error-output in your browser (not recommended for a production-system, but is useful during debugging and development).
Specify log_errors on for apache to log your errors to apaches error logfile, which by default in debian would be (unless other error-file is specified for the phpBB-site):
/var/log/apache2/error.log
try doing this:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL | E_NOTICE);
$id = mysql_pconnect('localhost','myusername', 'mypassword', true);
print "id=".$id."\n";
?>
and see what the response is
edit
From your comment it looks like the mysql module is not installed or enabled. You could have a look in your php.ini file and see if there is a line like
extension=mysql.so
If it is commented with a semi-colon, try removing it and restarting apache
Remove the "#" That is muting the error messages that mysql_pconnect is throwing.
Documentation
I sometimes have PHP going down a 'black hole' when it finds a function that it can't find.
Can you verify that the mysql extension is installed correctly?
You can do this by creating a php page like this:
<?php
phpinfo();
?>
Saving it in your webroot, and then accessing it. It should contain all the information about what your server is currently running in terms of PHP modules.
I think you have no mysql extensions installed for your PHP. Since PHP5 I think it is a PECL extension.
If you are working on windows, there should be a pecl.bat or something like this in your php directory. Just go there via console and enter
pecl download mysql
Then everything should work as expected.

Categories