php and mysqli working but cause a 'Aborted connection' warning - php

I hava a very simple test php:
<?php
require("conf.php");
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database);
mysqli_set_charset($connection,"utf8");
$query = "CALL `DB1`.`list_cities`();";
$result = $connection->query($query);
$connection->close();
?>
when I run, my mysql server return this:
[Warning] Aborted connection to db: 'DB1' user: 'user1' host: 'localhost' (Got an error reading communication packets)
but if I replace the '$query = CALL...' and use the actual SELECT instead of calling a storage procedure, there is no warning.
I tried to increase MySQL (Mariadb 10.5.18) max_allowed_packet to 1GB, increase the number of connections but it doesnt go away.
the code is working fine, but mysql is flooding my log with these warnings.

Related

How to properly connect and retrieve data from MySQL database on Linux

These days I'm switching from a Windows system to Linux. I installed the LAMP stack, and apparently there seems to be no malfunctions.
To be sure I wrote a trite script that connects to the database and performs a simple select query.
phpconnect.php:
<html>
<head>
<title>Test PHP Connection Script</title>
</head>
<body>
<h3>PHP Connect Test</h3>
<?php
$dbname = 'test';
$dbuser = 'my_user';
$dbpass = 'my_password';
$dbhost = 'localhost';
// connection to db
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// check connection
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: " . $conn->connect_error;
exit();
}
$sql = "SELECT * FROM user";
$result = $conn->query($sql);
$row = $result->fetch_array();
printf("%s %s (%s)\n", $row["firstname"], $row["lastname"], $row["age"]);
output:
I checked log file in /var/log/apache2/error.log and this is the error that comes up:
PHP Fatal error: Uncaught Error: Call to a member function fetch_array() on bool in /var/www/html/phpconnect.php:28\nStack trace:\n#0 {main}\n thrown in /var/www/html/phpconnect.php
I answer my question in case someone with the same problem is looking for the solution. Thanks to #ADyson for his help finding a solution, read his comments for details.
The problem arose from the fact that the new user created in mysql did not have the required privileges to access the database tables.
For this reason, when the statement that launched the query was reached, it returned a boolean value, that is false.
It was enough to modify the user's privileges in relation to the database to be queried.
You can do this by running the following command:
mysql> GRANT ALL ON database.table TO 'username'#'localhost';
Where:
database is the database name; you can write * to refer to all databases (not recommended)
table is the table name; you can also write * to refer to all tables in the selected database
After that, running the command FLUSH PRIVILEGES; will reload the grant tables in the mysql database enabling the changes to take effect without reloading or restarting mysql service.
mysql> FLUSH PRIVILEGES;

PHP Oracle connection

I am attempting to write some connection code with PHP to a Oracle database my school is hosting.
I'm using oci_connect() at the moment to make this connection, but it is failing.
$conn = oci_connect('username', 'password', 'hostname/SID');
I can access the oracle database through sqlDeveloper, as well as phpmyadmin, so I know the login information is correct.
I checked the oracle version with select * from v$version;, it shows as 12c Enterprise.
What is wrong with my php code for connecting? Is there a better way to make an oracle connection through PHP?
This is the test code I'm running, from http://php.net/manual/en/function.oci-error.php
<?php
echo "running";
$conn = oci_connect("username", "paswwrod", "address/SID");
if (!$conn) {
$e = oci_error(); // For oci_connect errors do not pass a handle
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
echo "ending";
?>
The string "running" gets echoed, but "ending" does not, the script just stops working when it attempts oci_connect()
have you also tried including the port number to the oracle db server like so?
$conn = oci_connect("user", "pass", "localhost:1234/xe");

odbc connection only working once?

I'm running the following PHP script to query an ODBC database:
<?php
$dsn = "ODBCconnection";
$db_user = "user";
$db_pass = "pass";
date_default_timezone_set('America/Chicago');
$conn = odbc_connect($dsn, $db_user, $db_pass);
if (!$conn)
{
exit("connection failed: " . $conn);
}
$query = "SELECT COUNT(column) as count FROM table WHERE criteria";
$test = odbc_do($conn, $query);
$count = odbc_result($test, "count");
echo "The count is: $count";
odbc_close($conn);
?>
The problem is not the query or the PHP that I can tell, it works exactly the way I want it to when I run it the first time it comes back with:
The count is: 750
However if I load the page again, it says:
Warning: odbc_connect() [function.odbc-connect]: SQL error: Failed to fetch error message, SQL state HY000 in SQLConnect in C:\xampp\htdocs\mailtest\scripts\testscript.php on line 11
connection failed:
If I load it again from that page, it just hangs up and times out after my PHP timeout expires. The only way to stop it is to stop and restart Apache, and then it acts the same way; works the first time, says it can't connect the 2nd, then just runs until it times out the 3rd.
I'm using "Oracle in OraClient 10g_home2" ODBC driver and running XAMPP 1.7.4. I've never run into something quite like this, it's like it just gives up after the first go, anyone ever seen this happen and know how to fix it?

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";
?>

Lost connection to MySQL server during query - PHP, MySQL

I have this php code, the files are hosted on another server and db else where
$hostname_xxx = "xxxdb.db.234141.mysqldbhosted.com";
$database_xxx = "xx11xx";
$username_xxx = "xx11xx";
$password_xxx = "xx332211xx";
$shasown = mysql_pconnect($hostname_xxx, $username_xxx, $password_xxx) or trigger_error(mysql_error(),E_USER_ERROR);
$your_ip = $_SERVER['REMOTE_ADDR'];
echo $your_ip;
$insertSQL1 = "INSERT INTO table (users_ip) VALUES ('$your_ip)";
mysql_select_db($database_xxx, $xxx);
$Result21 = mysql_query($insertSQL1, $xxx) or die(mysql_error());
The error I am getting is
Warning: mysql_pconnect() [function.mysql-pconnect]: Lost connection to MySQL server during query in /domains/4444.com/html/55.php on line 8
Fatal error: Lost connection to MySQL server during query in /domains/4444.com/html/55.php on line 8
Thanks
Jean
mysql_pconnect() creates a persistent connection to the database, whereas mysql_connect() does not. IF you are creating a persistent connection, you need only connect once throughout your session, so if you're creating a persistent connection more than once this may be the cause.
On shared servers it may be worth trying mysql_connect() over mysql_pconnect() and see if this corrects the issue at hand. Also, in your code you have:
$Result21 = mysql_query($insertSQL1, $xxx) or die(mysql_error());
But should be:
$Result21 = mysql_query($insertSQL1, $shasown) or die(mysql_error());
because $xxx was never a connection variable but $shasown is.
Personally I like to use mysqli_connect() as I find it to be a little faster.

Categories