CakePHP producing MySQL error - php

I have the situation where I use the TableRegistry of CakePHP in a script that acts like a server. It first works, but if there was no trafic for around one day, the server still runs but once the TableRegistry is used, it produces the following error:
Warning Error: Error while sending QUERY packet. PID=2536 in [..vendor\cakephp\src\Databse\Statement\MysqlStatement.php, line 39]
Googling for the error results in:
MySQL have limitation for size of data
Which cannot be the case, since the query and the corresponding result is really small. I have the feeling that the CakePHP Mysql connection somehow times out internally.
Here is my coding for reporducing the error:
namespace WebsocketServer;
//load the cakePHP classes
require_once '../config/bootstrap.php';
use Cake\ORM\TableRegistry;
class Server {
public function query() {
//any data fetch mockup (the
$data = TableRegistry::get('Screens')->find()->all()->toArray();
}
}
$server = new Server();
//querying works perfecly
$server->query();
//wait for a day
sleep(60*60*24);
//querying results in an error
$server->query();

Related

Migrating database causes CodeIgniter error '...undefined method: CI_DB_mysqli_driver::first_row()'

A fully functional CodeIgniter application has had it's MySQL database migrated from server A to server B.
Ever since the migration, the following code throws an error:
$part = $this->db->select('')
->where('id', $part_id)
->limit(1)
->get('part')
->first_row('array');
The exact PHP error:
Message: Call to undefined method CI_DB_mysqli_driver::first_row()
As per the documentation, first_row('array') most definitely is a valid method: https://www.codeigniter.com/userguide3/database/results.html#result-rows
Also, this application has worked in production for months, so the error is explicitly linked to migrating the database.
The question
What could have been misconfigured on the new database server that causes this error?
Note 1: After migrating the database, for some reason all table and column names that were previously camel cased (documentId e.g.), were somehow cast to lower case.
Note 2: Using query results as arrays, which is very common in CodeIgniter, throws an error such as Message: Cannot use object of type CI_DB_mysqli_driver as array.

Why isn't my mysqli_connect connecting? It says no hostname was specified when one is

I'm writing a simple blogging web app for my portfolio and I've come across this strange problem. I wrote a PHP script to connect to a database and read and write data to a database by RESTful calls.
I've done this before in other programs with no problems, but in this one I get an error. I wrote a simple test page to check that the RESTful calls would work before I started using them in my main app. Instead of working, I got an error back from my PHP script.
The error goes like this:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/No-MySQL-hostname-was-specified' (2) in /home/releesquirrel/storage.electricsquirrel.net/SimpleBlog/php/model_SimpleBlog.php on line 35
The code leading up to line 35 goes like this:
class model_SimpleBlog {
// Properties
// Database Info
private $serverName = "mysql.electricsquirrel.net";
private $userName = "esqrl_client";
private $userPassword = "fakepassword";
private $dbaseName = "esquirrel_simpleblog";
// Methods
public function model_SimpleBlog() {
//
}
// Load the ten latest entries after an offset
public function loadEntries($offset) {
$result = false;
// Connect to the Database Server
$connection = mysqli_connect($serverName, $userName, $userPassword, $dbaseName);
I've changed the password for privacy but that's the code that's throwing the error. I'm completely stumped. I've used code similar to this with no problems before, and I've tried googling the error code with no luck.
Does anybody know why I'm getting this error and what I can do to fix my code?
In PHP, you need to refer to object variables with $this->:
$connection = mysqli_connect($this->serverName, $this->userName, $this->userPassword, $this->dbaseName);
$serverName, for instance, looks for a variable with that name in the scope of the function. Obviously none exists.
See the manual for more information on PHP object properties.

CodeIgniter with a PostgreSQL multiple schemas issue

I am developing an agent-port application in CodeIgniter with PostgreSQL by using mutiple schemas from a single database. The setup I have designed is working fine.
So the problem I am facing right now is when after some interval it starts showing the following errors on different pages, like:
Fatal error: Call to a member function result() on a non-object in
D:\xampp\htdocs\mangonet\application\modules\settings\models\adminmodel.php
on line 30
Fatal error: Call to a member function num_rows() on a non-object in
D:\xampp\htdocs\mangonet\application\modules\users\models\adminmodel.php
on line 56
Fatal error: Call to a member function row() on a non-object in
D:\xampp\htdocs\mangonet\application\modules\users\models\adminmodel.php
on line 86
Let me give one example
I try to find out a particular problem only if it always remains there. But when I refresh my page or remove that row() from the function
function get_item_by_id($table, $id)
{
return $this->db->get_where($table, array('id' => $id))->row();
}
it returns a black result and when I add it back, the problem gone, and it starts working fine.
I know the above fatal errors you can see above solutions are available. But my problem is a bit complex.
This sounds like a problem with database connections going away. I would recommend looking at debugging this on the application side. In particular I would start logging the following:
Database connection status on all problematic calls. In other words check and see if the database connection has closed or failed.
If the database connection has not failed, then the next thing to check is what the search path is. You can execute the following query to test it:
SHOW search_path;
It is possible that if this is related to multiple schemas, something is messing with that.

PHP 5.3.5 PDO FETCH_OBJ memory leak?

I'm currently developing an application in PHP which uses PDO. I'm writing an import which reads in a CSV file, checks the database for a record, and updates, deletes, etc....
Something which I've noticed is the memory being used by this script seems very high and it seems like it could be to do with the way I'm executing the query. see below for example query which is executed for each line in the CSV:
$qry = "SELECT * FROM company WHERE id = 1";
$sth = $this->prepare($qry);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_INTO, new Company());
$sth->fetch();
for the above memory_get_peak_usage() = 6291456
When using the below:
$qry = "SELECT * FROM company WHERE id = 1";
$sth = $this->prepare($qry);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_CLASS, "Company");
$sth->fetch();
for the above memory_get_peak_usage() = 524288
As you can see the difference is fairly big.
I guess I've 3 questions..
Is there a memory leak when using PDO::FETCH_OBJ in PHP 5.3.5?
Is there any difference between using FETCH_CLASS as opposed to FETCH_OBJ?
Has anyone else experienced the same issue?
Company Class is simple:
class Company {
function __construct(){}
/**classvars**/
public $_tablename = 'company';
public $transient;
public $id;
public $name;
/**endclassvars**/
}
Looking at the PHP changelog, there does appear to be a relevant fix in 5.3.4 where a memory leak was fixed in PDO FETCH_INTO.
From what you've said, I suspect that yes, this is the bug you're seeing. The solution, of course, is to upgrade -- there really is no point in sticking with an old patch release.
Even if this isn't the bug you're seeing, there have been a very large number of PDO fixes in the versions between 5.3.3 and now; I'm sure there's a good chance that at least some of them are relevant to you.
Note: the original answer was given before the OP changed PDO::FETCH_OBJ to PDO::FETCH_INTO
After that update I've tried to reproduce the behaviour using PHP 5.3.10-1ubuntu3.4. There where no significant difference in memory cosumption between both fetch modes. I've tested using a large MySQL table and a large SQLite database.
As #SDC mentioned the bug is known and was fixed after 5.3.5. (At least in 5.3.10 as I've seen).
Conclusion: You have to upgrade your PHP version.
Although the behaviour is interesting and should be investigated you are using PDO::setFetchMode() in a wrong way. When $mode - the first param - is PDO::FETCH_OBJ no second param is expected. If you use a second param the call to setFetchMode() will fail (returnin false) and the default fetch mode FETCH_BOTH will be used.
You can see this error when enabling PDO::ERRMODE_EXCEPTION :
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->query('....');
// the following line will trigger an exception
$stmt->setFetchMode(PDO::FETCH_OBJ, new Company());
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: fetch mode doesn't allow any extra arguments'
When you are expecting that result rows should objects of a specific class, then PDO::FETCH_CLASS is the working attempt. PDO::FETCH_OBJ will return objects from type StdClass
FETCH_INTO: Means fetching into an existing Object (that was created with new e.g.)
FETCH_CLASS: Means fetching into an new Object (the constructor is called every row)
Be careful, if the constructor in your Company class has dependencies, they are called for every row. Therefore the constructor should not contain functions or classes that do an DB connect e.g. only simple initializing...
How does your Company class look like?

Warning: mysql_query(): 7 is not a valid MySQL-Link resource

Similar questions to this my have been asked a lot of times before. But since I did not find a solution in any of the questions asked before me, I take the liberty of asking it again.
My program uses a class made by me which handles all database connections for the program. Several modules I've used before used the same class without fail but when I chose to do a new module using the same class, the warning shows as-
Warning: mysql_query(): 7 is not a valid MySQL-Link resource in wherever... on line 49.
The warning happens when I execute a MySQL query through a function I made. The function is as follows-
public function runquery($_query)
{
$result = mysql_query($_query,$this -> connection); //line 49
if (! $result) die(mysql_error());
else return $result;
}
The function belongs to a class named mysql and it has not been tampered with or made changes to. So the function should technically work as expected, as every other module relying on the same class for database connectivity works just fine.
The query execution is successful however and I manage to update tables with no problems (except the warning). The block of code in the main program where the runquery() function is called from is as follows-
$phpmyadmin = new mysql();
$phpmyadmin->connect('localhost', 'root', '');
$phpmyadmin->setdb('test_db');
$result = $phpmyadmin->runquery($Query);
unset($phpmyadmin);
So the mysql's functions work just as fine as ever and the query executes just fine. But the warning shows for a reason I cannot understand. Any help?
The symptoms suggest that the database connection has been closed or dropped. Look for unwanted mysql_close() calls in your code. Additionally, you can use the following functions to troubleshoot the issue:
is_resource() and get_resource_type() to confirm that $this->connection is a valid data type.
mysql_ping() to find out if the database connection is alive.
If it's a rare issue, log stuff into a file and wait until it happens again.
There should not be spaces.
$result = mysql_query($_query,$this->connection);

Categories