PHP app not communicating with certain MYSQL databases - php

I have a PHP web app which has been working fine until today (it is sitting on an Windows/IIS server). When I attempt to access a page which connects to the MYSQL database, I get a blank page in firefox. In IE I get a 500 Internal Server Error. Normal PHP works fine, it is simply when I try to connect to the database. The mysqli connection is not giving any error messages.
The weird thing - I have phpmyadmin on the same server and it is working fine, I can see my database and interact with it.
I also have another database on the server which is working fine, the user attached to that database has read only access (as it is an archive database), but the PHP web app connecting to it works.
What have I tried?
Initially I thought it was a user issue, so I created a new user in phpmyadmin with full privileges and had the PHP app log on using it instead, same result.
I then thought that it was an issue with the database, so I copied the data and structure to a new database, same result.
The MYSQL.err log file has no errors and indicates that the server is accepting connections (which it is, as phpmyadmin is working).
I have even tried writing a test script:
<?php
//Debugging
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include "settings.php";
echo $HOST . "<br>";
echo $DBNAME . "<br>";
echo $DBUSERNAME . "<br>";
echo $DBPASSWORD . "<br>";
$CON = new mysqli($HOST, $DBUSERNAME, $DBPASSWORD, $DBNAME);
//Check the connection and report error if it failed
if ($CON->connect_errnum > 0)
{
die('Unable to connect to database [' . $CON->connect_error . ']');
}
$result = $CON->query("select user() AS us");
if (!$result)
die("There was an error running the query [" .$CON->error ."]");
}
while($row = $result->fetch_assoc())
{
echo $row["us"] . "<br>";
}
?>
If I don't have he code from the $result = $CON->query("select user() AS us"); down, it displays the 4 values. Once I include it, I get the 500 Internal server error page.
I am at a loss to figure out what is happening or where to start looking.

One of my support classes had a syntax error in it, this caused the page to fail before it got to the database connection. Because my error reporting was turned off, I could not see this (even though I thought it was on). I had to go in to PHP.ini and set display_errors = off to display_errors = on. This then showed the error message.

Related

frequent lost of connection to mySQL server

I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here

PHP mySQL Update doesn't work

I currently have a very big problem with PHP and mySQL. I moved a System I coded to a new Server. And while everything worked fine on the old Server, I had some problems on the new Server. Especially with mySQL. While I solved nearly all of them, I have one which I can't seem to get a hold on. And after 2 hours of trying i searched on the Internet for another two hours and updated my Syntax several times. But nothing seems to work. So now I'm here. I get a Connection to the database without a problem, but I can't update the values. I hope you can help me.
//Connect to mySQL Database
$verbindung = mysql_connect($server, $username, $passwort);
if (!$verbindung) {
echo "Couldn't connect: " . mysql_error();
}
$name=$_POST['fuehrer'];
$ident=$_POST['id'];
//Debugging
echo $name;
echo $ident;
$sql_befehl_0="UPDATE 'olgatermine' SET fuehrer = '".$name."' WHERE ID = '".$ident."';";
if (!mysql_query($verbindung, $sql_befehl_0)){
echo "Couldn't write to database";
}
//Close connection
mysql_close ( $verbindung );
What version of php use? Because in the newest versions of php the mysql functions are deprecated/removed, use instead mysqli.
Try to echo a mysqli_error at the end of the code, also mysql_error if your version of php accepts mysql functions.
If not version of php is the problem check this:
Wrong things what i see in your code..:
$sql_befehl_0="UPDATE 'olgatermine' SET fuehrer = '".$name."' WHERE ID = '".$ident."';"; // wrong
should be:
$sql_befehl_0="UPDATE `olgatermine` SET `fuehrer` = '".$name."' WHERE ID = '".$ident."';";
You need to run mysql_select_db('dbname') below line you do the mysql connection.
You can set at the first line of file:
ini_set('display_errors',1);
error_reporting(E_ALL);
to show all errors.

Need help sorting out MySQL permissions and connections

So I'm trying to set up a PHP site that accesses and writes to a database. This should be relatively simple but I've become swamped with a mess of permission issues and I'm new to PHP so I'm not sure how to fix them all.
First off, this warning occurs when trying to access the database:
Warning: mysqli_connect(): The server requested authentication method unknown to the client [mysql_old_password]
The connection also fails due to this:
Warning: mysqli_connect(): (HY000/2054): The server requested authentication method umknown to the client
In my research I found that this warning was related to password hashing due to versioning of PHP (link)
However attempting to run the solution on that page gives me the following error:
Error Code: 1044. Access denied for user 'user'#'host' to database 'mysql'
And I don't know how to gain permissions in that database.
Here's my php code for reference (sensitive info removed):
$con=mysqli_connect("IP:port","user","password","database_name");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Succesfully connected";
echo "<br />";
}
$result = mysqli_query($con, "SELECT * FROM OWNERS") or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo $row['email'] . " " . $row['type'];
echo "<br />";
}
Can someone help me out with this one? I know it's a lot but it seems like all of the issues are related and I'm not sure where to start with fixing them. (I'm using XAMPP to run the php and MySQL workbench to run queries)
I suppose the mysql is running on a different machine in your network. try this to see if it works:
CREATE USER 'user'#'IP' IDENTIFIED BY 'your password';
GRANT ALL ON database_name.* TO 'user'#'IP';
if the user is already in your system but with different IP then you should create it again using the IP of your mysql server. you could also try % instead of IP in create user and grant statements but it is not recommended in production.

Moving website to another local host to try - no errors, but no data is shown

This is my first time transferring my local dev site to another local site to test out, and can't access the database in the new site. Both computers are running XAMPP on Macs. The test site has successfully installed XAMPP, and has placed database files under Applications/XAMPP/xamppfiles/var/mysql. The test user is able to start Apache and MySQL, and see database files in PHPMyAdmin. The test user is also able to access the local web site with no error messages.
EDIT: The database files were copied over by copying/zipping the file on my end, and unzipping/dropping the copied files on the test end.
However, the local web site does not seem to see the database. Pages that are supposed to return data just show the default "sorry, we're sold out" message or show "image not found" graphics, and the admin is unable to log in.
The index page calls
include_once "common/base.php";
which consists of
<?php
// Set the error reporting level
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Start a PHP session
session_start();
$_SESSION['docroot'] = $_SERVER['DOCUMENT_ROOT'] . '/mysite/';
// Include site constants
include_once $_SERVER['DOCUMENT_ROOT'] . "/inc/constants.inc.php";
if ( !isset($_SESSION['token']) )
{
$_SESSION['token'] = md5(uniqid(rand(), TRUE));
$_SESSION['token_time'] = time();
}
// Create a database object
try {
$dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME;
$_db = new PDO($dsn, DB_USER, DB_PASS);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
?>
If there was a connection problem, I would expect to see an error message returned, which I can verify by changing the constants in inc/constants.inc.php to bogus values. So I don't believe there's a connection problem.
If you were in my position, what would you check?
EDIT: I just found out about the Export function in PHPMyAdmin. I'm going to try deleting the db files that were copied over, exporting the files from PHPMyAdmin, and then importing them in the test machine.
EDIT: I just had the test machine do a simple
$sql = "SELECT FirstName From users";
$stmt = $_db->prepare($sql);
$stmt->execute();
$aaData = array();
while($row = $stmt->fetch(PDO::FETCH_NUM)){
$aaData[] = $row;
}
$stmt->closeCursor();
print_r($aaData);
on the index page, it shows the correct list of users. So the connection is good,
and there's an issue elsewhere. Would y'all say that I still need to delete the copied db and use the "correct" way (export/import), or is that a waste of time?
FINAL EDIT: I did redo the move using Export/Import and everything is working well now. Thanks to all.
First off, zipping files in the mysql data directory and moving them is not the best way to go. It can work if you have completely flushed/shut down the mysql server before moving them. You are better off using mysqldump or a similar tool to move entire databases at a time.
It sounds to me like you moved over your app's database files, but you probably forgot to add the user/password credentials to the new MySQL setup. You mentioned you use phpMyAdmin, so when you login on the other machine click the "Privileges" tab and make sure it matches up with the Privileges tab on your dev machine.
If it's not that, then it is entirely likely that by zipping and copying the files some/all of the tables were corrupted. Try using phpMyAdmin's Export and Import capabilities to move the database over. If the data is very large let me know and I can give you some command line entries to run.
Hope this helps.

Access denied for user 'www-data'#'localhost - how to deal with that?

I face with a strange problem yesterday. I have server running Debian with installed PHP 4.4.4-8 and mysql 5.5.9. That server serves a several websites.
For some reason randomly I get that error "Access denied for user 'www-data'#'localhost' (using password: NO)" when I try to load the webpage.
If I hit refresh the page loads normally, but afer several clicks that message appears again. Username which that page use to connect to mysql server is not www-data.
Does anyone has faced similar problem ?
www-data is the Debian user that runs apache and php. If you attempt a query when you don't have a valid connection, php/mysql will attempt to create a connection using <unix-user>#localhost with no password. This is where www-data#localhost (using password:NO) is coming from.
The most likely reason that this has started happening now (though it has been running fine for 2-years prior) is that your db load has increased to the point where some connections are unable to succeed (probably due to max_connections, or max_user_connections; though this can also result from other limits like memory, threads, etc). When this happens, your call to mysql_connect will emit an error message, and return FALSE. If you fail to detect this failure, then your next mysql call (probably mysql_query, or mysql_select_db) will attempt the connection to www-data#localhost -- thus causing the problem you're seeing.
I suggest enabling error reporting, and error display (as suggested by #DarkMantis) :
ini_set('error_reporting', E_ALL|E_STRICT);
ini_set('display_errors', 1);
Also, be sure that your call to mysql_connect is not preceded by a # sign; and make sure to check the return value. It should look something like this:
$cxn = mysql_connect('localhost','yourusername','yourpassword');
if( $cxn === FALSE ) { die('mysql connection error: '.mysql_error()); }
It sounds like the query that is causing the error happens when something specific is called. This could mean that when the query is called, you aren't connected to the database with the correct username/password.
Try to ensure that you are definatly connected, use or die(mysql_error()); at the end of all your query variables to debug them.
Also, use the following two lines at the top of your php file:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
That will show you any little php errors that may occur within your class/file which you may not have picked up before.
If your still having a problem after this, please post your PHP code and I will take a look at it directly.
Thanks!
i faced the same problem.
The problem was in my config.php!
I simply changed the $dbserver from
"127.0.0.1" -> "localhost".
Now the connection works again!
For absent-minded people, this error may happen when mysql_query() is called after mysqli_connect(), when it should be mysqli_query().
Use password 'NO'
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Deactivating safe mode fixed it for me:
Notice: mysql_connect(): SQL safe mode in effect - ignoring host/user/password information in /var/www/html/test.php on line 4
For solve this problem. I had to change the connection script Using
(MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Categories