PDO Exception:SQLSTATE[HY000] [2006] MySQL server has gone away with xampp - php

I'm trying to connect to the database and fetching some records but I'm getting error:
Error i'm getting
I'm using Slim PHP framework and slim-twig to render views so my code for the connection file is:
<?php
use Payment\App;
use Illuminate\Database\Capsule\Manager as Capsule;
session_start();
require __DIR__ . '/../vendor/autoload.php';
$app = new App;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost:8080',
'database' => 'payment',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
require __DIR__ . '/../app/routes.php';
?>
I had also configured my php.ini file properties like max_execution_time & max_allowed_packets but it didn't work and i had also checked that no loop is causing this problem because it takes approx 3 min to show me this error,in this 3 min the it loads.so please anyone can tell me where i'm doing in my code?

Probably caused by your packets. The following settings should help:
Run this in your MySQL terminal.
set global max_allowed_packet=104857600

Related

PHP 7+ & PDO SQLSRV charset issue

PHP 7.2.8
PHP 5.6.37
Same code, different version of PHP. Many have the same problem than I have read.
Did someone finally find the solution?
Thanx in advance.
PS: I have tried with Medoo lib and without lib. Same problem.
Connection File:
require_once __DIR__ . '/config.php';
use Medoo\Medoo;
/* Connection to lin2db database */
$lin2db = new Medoo([
'database_type' => 'mssql',
'database_name' => $CONFIG['lin2db'],
'server' => $CONFIG['MSSQL_addr'],
'username' => $CONFIG['username'],
'password' => $CONFIG['password'],
'charset' => 'utf8'
]);
Query:
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../engine/DB_connection.php';
GLOBAL $lin2db;
$rows = $lin2db->select("user_auth", [
"account",
"password"
], [
"account" => 'builder'
]);
var_dump($rows);
Solution:
var_dump(bin2hex($rows));

Doctrine php DBAL not working

New to doctrine php and I followed each instruction to the point. Downloaded doctrine using composer as instructed on http://www.doctrine-project.org/projects/dbal.html and then on my index.php I added this http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#getting-a-connection only changing the test database credentials.
include_once 'vendor/autoload.php';
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'teastdb',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
and this does not work (shows blank page, I even inserted both right and wrong credential for testing purpose). What is wrong?
Here is a screenshot as well

Defining configuration values under specific namespaces?

I'm writing a framework, and have just added namespaces. I've had no trouble converting everything, save for configuration values.
I can define constants easily, so single values are not a problem...but what about an array of values?
Take this array, for instance, which is the configuration array to add a connection to Capsule...
[
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]
I have been using a file that looks like this for database configuration...
<?php
namespace BareBones;
use Illuminate\Database\Capsule\Manager as Capsule;
$BareBonesCapsule = new Capsule;
$BareBonesCapsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]);
$BareBonesCapsule->setAsGlobal();
$BareBonesCapsule->bootEloquent();
use Illuminate\Database\Schema\Blueprint as Blueprint;
use Illuminate\Database\Eloquent\Model as Eloquent;
I'm going to start booting Eloquent from my main App class. This will prevent a global variable from doing it. While it is unlikely that $BareBonesCapsule is going to be used, I would still like to keep my framework clean and keep everything in it's namespace.
I could declare a bunch of constants in the configurations file...
<?php
namesapce BareBones;
define("driver", "mysql");
define("host", "localhost");
define("database", "DB_NAME");
/* etc... */
This doesn't seem very clean, and I'm assuming there is a better way to do this. How do other frameworks handle this issue, and what alternative means of configuration do I have while maintaining a separation from the global namespace?
You can use .ini files and break them into sections like so:
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
From: http://php.net/manual/en/function.parse-ini-file.php
Symfony supports XML, PHP and YML and allows you to write your own config decoder.

Use Illuminate database

I want to use Illuminate database(https://github.com/illuminate/database). Not with Laravel, use only in my php file.
I do
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
But it seems not working, and don't show any error message. Do I need to require any file? The illuminate directory is in the same directory with my php file.
EDIT:
I can use query now. Like this
$users = Capsule::table('users')->where('votes', '>', 100)->get();
I don't know how to use model.
User.php
class User extends Illuminate\Database\Eloquent\Model {
}
My php file
require 'vendor/autoload.php';
require 'User.php';
$users = User::where('status', '=', 1)->get();
Got error
Fatal error: Call to a member function connection() on a non-object in /Users/someone/repos/test/vendor/illuminate/database/Illuminate/Database/Eloquent/Model.php on line 2472
SOLVED:
Everything works fine. Use #majid8911 example https://github.com/mattstauffer/IlluminateNonLaravel
Thank you everyone.
take a look at here I successfully did the same with this tutorial:
https://github.com/mattstauffer/IlluminateNonLaravel

Class 'Entity\User' not found in proxy

I had to move an existing site to another hosting (with the same software).
Now, when second user tries to do something with entities (programatically adding, editing or removing) the site fails with the following error:
Fatal error: Class 'Entity\User' not found in /home/.../www/includes/Objects/Proxies/__CG__EntityUser.php on line 9
Here is the config:
ini_set('include_path', ROOT_PATH.'/includes');
require_once(ROOT_PATH.'/includes/Doctrine/ORM/Tools/Setup.php');
Doctrine\ORM\Tools\Setup::registerAutoloadPEAR();
use Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration,
Doctrine\DBAL\Event\Listeners\MysqlSessionInit;
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config = new Configuration;
$driverImpl = $config->newDefaultAnnotationDriver(ROOT_PATH.'/includes/Objects');
$driverImpl->getAllClassNames();
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(ROOT_PATH.'/includes/Objects/Proxies');
$config->setProxyNamespace('Objects\Proxies');
// developer mode
//$config->setAutoGenerateProxyClasses(true);
$config->setQueryCacheImpl($cache);
$em = EntityManager::create(array(
'driver' => 'pdo_mysql',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'charset' => 'utf8',
'host' => DB_HOST,
'user' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME
), $config);
$em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8', 'utf8_unicode_ci'));
As I said I didn't changed nothing in code...
When the site in single-user state - everything is fine..
What can it be? Thank you
Sometimes I have had to manually create the proxies with ./doctrine orm:generate-proxies (on the command line tool)
But make sure your file permissions are correct as sometimes they are trying to be auto-generated but the server won't allow it.

Categories