PHP file can't access MySQL database after changing server - php

So I have a PHP application running well on my local server, powered by Zend Server. After I uploaded it to the ubuntu server, it stops working, except for two php file, the index.php and register.php, which handles the user registration part. When I say working, it means I can view the page as in my local server but the internal process is not functioning. For example, when I try to register a user, it should tell me whether it is successful or not. Instead, it is a just a blank page.
I rule out the directory problem. All the path are relative and they are working fine. I also rule out the MySQL database problem as I can access the database and do everything with it. So I wrote the following script to test where php can access mySQL (the MySQL database is set up in the same host). So I wrote the following script:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require_once("config/connect_database_viewer.php");
echo $db_hostname;
echo "<br>";
echo "1";
echo $db_username;
$sql = "CREATE TABLE writer (UPC VARCHAR(15)) ENGINE MyISAM";
if ($db_server->query($sql)) {
echo "the database works";
} else {
echo "so it didn't even reach the server";
}
Fatal error: Fatal error: Class 'mysqli' not found in /var/www/viewer/angelo/config/connect_database_viewer.php on line 3.
But it doesn't make sense to me as I tried to install php5-mysql by this command:
sudo apt-get install php5-mysql
it comes back to me saying php5-mysql is already the newest version. And I checked the ubuntu page for the version, the php5-mysql module should include a mysqli extension. The php module is enabled too! Here is my connect_database_viewer.php file:
<?php
$db_hostname = '127.0.0.1';
$db_database = 'viewer';
$db_username = 'juvo1';
$db_password = 'juvo1';
$db_server = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($db_server->connect_error) die($db_server->connect_error)
?>

You need to check the error log. It'll tell you where the error happened in your PHP script. Try looking in
/var/log/httpd/error.log
and see what it says.

If only the first line of your test script is printed, your include script creates an error. The reason can be that your include fails (maybe due to rights problem) or something inside the script. I would include the
ini_set("display_errors", 1);
error_reporting(E_ALL);
to get the errors in the browser if your console (= error log?) does not mention anything else.

It appears that there are two problem in the process. The first one is that there are no mysqli module built-in for my version of php. People can install the php mysqli module with this command:
sudo apt-get install php5-mysqlnd
This stops the white page from showing up. But there is still warning about trying to connect via tcp. So I change the database conf php file from 127.0.0.1 to localhost. For more information, please refer to this link: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2).

Related

How do I configure MySQLi to recognize mysqli commands in PHP?

Currently, I'm trying to test connection to a database from one machine to another. One machine has MySQL installed on it and has a database set up and running, and I want to have another machine connect to it using mysqli in PHP. Both machines are Ubuntu, and I'm using PHP 5.6. I installed the mysql client on the machine I want to use to connect to the database, and when I try and connect to the database in php using the following code:
<?php
$mysqli = new mysqli('hostname', 'username', 'password', 'dbname');
if($mysqli->connect_errno) {
echo "Failed to connect to MySQL database.";
exit();
}
?>
it errors out, and shows a "This page isn't working" error page with a HTTP Error 500. I've tried the following:
sudo apt-get install php-mysqli (Got the message that it was installed and at standard version, I assume this is because I installed the mysql client)
phpenmod mysqli (This didn't change anything)
I'm not sure what else I should try. I believe I have PHP 5.6 and PHP 7.1 both installed, will this cause an issue? What steps can you recommend to troubleshoot and fix the issue of mysqli not working?
First, I would take a look at the error log on the machine you are trying to connect from. There should be some explanation for the 500 error.
Second, I believe you need to install the mysqlnd PHP extension on the machine you are connection from. The machine that is hosting the database is just doing that. So, you don't need to worry about having PHP install there.

Cannot Connect to Google Calendar PHP/API

Good day to you all :)
So I was using the following code to insert events into my Google Calendar. This all worked last week. This week, I have built a new computer and updated all PHP, MySQL and Apache. I am now getting the error "Cannot Connect to Calendar", which you can see is a custom error message at the end of this code example. The rest of the script deals with data processing and insertion.
I've checked the code with examples online and it matches. The username and password are correct and I can login via various browsers. Can anyone help me to get this back up and running and allowing me to connect to the Calendar?
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
$user = 'valid#email.address.com';
$pass = '<removed>';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$calendar_user = "valid%40email.address.com"; // Also tried with #
//$calendar_visibility = "private-<removed>";
try {
$client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service);
}
catch(Exception $e) {
// prevent Google username and password from being displayed
// if a problem occurs
echo "Could not connect to calendar.";
die();
}
For reference. It's a clean Windows 7 install now running the lastest (Nov 2012) version of WAMP. I am able to run local PHP scripts that connect, successfully, to remote databases. And the file 'Zend/Loader.php' is present with the correct path.
Any help greatly appreciated. TIA
------ UPDATE ----
Turned off the custom error message and get this:
Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message ' in C:\Program Files (x86)\wamp\www\www.domain.com\subdir\Zend\Gdata\App.php on line 709
Running on the latest WAMP. I've enabled the SSL Module but nothing has changed.
SOLVED
For the benefit of anyone else who finds this...
SSL was showing to be uncommented and fully supported in both PHPinfo() and the WAMP dashboard.
However, it appears that WAMP needed to be explicitly told that it was enabled. After insuring that you actually installed WAMP/PHP with SSL support then...
Click on the WAMP tray - PHP - extensions - [tick/enable] php_openssl.dll
Restart WAMP.
Now all works as it did in the past.
Just a guess but is php_curl enabled ?
I didn't try but I know there was few issues on Windows with some PHP dll (php_curl.dll for instance). You might give a try on a "real" environment (LAMP) if you are able to.

Wordpress installation - php and mysql are not talking to each other to access database?

I'm trying to get a wordpress local install working on my Mac.
I've recently set up a database, username and password using the MySQL client as per the instructions in the wordpress codex. That part is ok.
When I go to the local site on my computer I'm prompted by wordpress to set up the wp-config.php file. Putting in the same details that definitely work with the MySQL client returns an error.
Manually updating these details in the 'wp-config-sample.php' file and renaming it as 'wp-config.php' also doesn't work - I'm now greeted with a big bold message: "Error establishing a database connection".
From what I can gather, php and mysql are not communicating with each other properly.
I've changed the apache 2 configuration file found in 'etc/apache2/httpd.conf' and uncommented the line which loads the php5 module: LoadModule php5_module libexec/apache2/libphp5.so
and restarted the apache server. This has still not fixed my problem. I'm stuck on what to do next... help appreciated please!
It's been fixed now.
php was looking in the wrong place for the mysql.sock file, and couldn't communicate with the mysql database, meaning wordpress couldn't be set up.
On the command line I typed
ps aux | grep mysql
to bring up as much info about mysql that I could. From this I saw that the mysql socket on my computer is being used at /tmp/mysql.sock
Meanwhile, in the "wp-config.php" file, I changed the line define('WP_DEBUG', false); to define('WP_DEBUG', true); and refreshed my web browser that was showing my localhost setup. This now said a lot more info than "Error establishing a database connection" - infact I could see that the mysql.sock was not configured correctly at all.
Creating a 'phpinfo.php' file with just the single line of code <?php phpinfo(); ?> was the pivotal turning point for me.
Putting that 'phpinfo.php' file in my localhost directory and then viewing it from my web browser not only definitely confirmed that the mysql.sock file was not configured properly, but it also showed where the configurations were being loaded from on my machine: '/private/etc/php.ini'
The next thing I did was open up the 'php.ini' file in a text editor and doing a quick find and replace to update the correct location of mysql.sock.
Finally, a restart of the local apache server needed to be done to fully update all the new settings that I'd been fiddling about with.
sudo apachectl restart
Now everything works fine!
The Error-Message "Error establishing a database connection" indicates clearly, that the php-script can not establish the connection. This can have one of the following reasons:
Your host in the wp-config.php is not correct.
The mysql-server does not listen on that host
The user you set up in the mysql has no privileges to connect the way you are trying
The firewall blocks the access of the php-script to your mysql-host
Try to connect the database with the same settings of your wordpress using Sequel Pro (or any other GUI to connect a mysql database). If the connection works, the problem 2. and 3. isn't your problem.
Look in your firewall-log to make sure 4. is also not your problem.
If all problems are excluded (even 1. after a final check), try to run something like phpMyAdmin to connect your database, to make sure your php-installation has the needed drivers.

php with postgresql database

i wanna to connect to postgresql database using php but it display a blank screen when submit and doesnt execute the query
Thanks in advance
Update: this is my code
function execute_query($dbName,$query){
$host = "localhost";
$user = "postgres";
$pass = "";
$db = "test";
echo "before create connection";
$con = pg_connect ("host=$host dbname=$db user=$user password=$pass");
echo "After connection is created";
if (!$con) {
echo "not connected";
// die('Could not connect: ' . mysql_error());
}
$result = pg_query($con, $query);
pg_close($con);
return $result;
}
The output:
display the message "before connection" but doesn't display the message "After connection is created" or "not connected".
The problem is likely a PHP error that’s getting recorded to a log file somewhere. Locate the log file, or enable showing the log errors on your page by using the following at the top of your script:
ini_set('display_errors', '1');
error_reporting(E_ALL | E_STRICT);
This is a short-term solution and can’t be used in deployment (where you want to set display_errors to 0). For a long-term solution, you really want to locate the Apache or PHP error log and tail it.
To try to find the error log, run the following script:
<?php phpinfo(); ?>
In the Configuration > PHP Core section, look for error_log. If that’s not set, you can set it in your php.ini file. All errors will be recorded to that file, even if you have display_errors set to 0.
Add a php file to your server and put this in that file:
<?php
echo phpinfo();
?>
When you open that file from the browser, check if postgres support is setup for php.
you should something like this on the page:
pgsql
PostgreSQL Support enabled
PostgreSQL(libpq) Version 8.2.3
Multibyte character support enabled
SSL support disabled
Active Persistent Links 0
Active Links 0
Try checking your web server's error log (for instance, /var/log/apache2/error.log is a common location for the Apache2 log) and see if there is an error reported from PHP there.
You may want to set the php error reporting level in your ini file if this is a local development server.
It seems, that your php does not have postgresql support, cf. http://us.php.net/manual/en/ref.pgsql.php:
Note: Not all functions are supported
by all builds. It depends on your
libpq (The PostgreSQL C client
library) version and how libpq is
compiled. If PHP PostgreSQL extensions
are missing, then it is because your
libpq version does not support them.

mysql_connect() on localhost

I am trying to implement a little 'intranet' on my home network, but I am an apache/mysql configuration noob...
Running fedora 10, and have apache, mysql, and php set up as well as i know how. However, when I try using a standard form with POST, the php script seems to error out on the mysql_connect(...); line. I don't get an error message, in fact the browser page is blank, even in the view source window. Here is what I have:
$conn = mysql_connect("localhost:1186", "user", "password");
...
I have added the username and password in the mysqladmin tool, and i've tried "localhost" and "localhost:1186", as I saw the 1186 port referenced in my my.cnf file.
Obviously I have something configured wrong, any ideas?
The resulting page is blank when there's an error. One of the more common errors to make in your setup is to forget to install either the php-mysql or mysql-server packages. Verify that they're installed, and if not install either or both and then restart the httpd service, and start the mysql service if not running.
the default for a fresh mysql install would be
$conn = mysql_connect("localhost:3306", "root", "");
or just
$conn = mysql_connect("localhost", "root", "");
This is good for testing purposes but leaving the root account unprotected is a bad habit.

Categories