MySQL error User has exceeded the 'max_questions' resource - php

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1226
User 'bb99ddb719cd2f' has exceeded the 'max_questions' resource
(current value: 3600) in execute().
Earlier code was working file but suddenly it has started giving errors at every execute().
Here is my code:
Actual file:
<?php
$sql="SELECT count(job_status.is_approved) as is_approved
from job_status
WHERE job_status.job_id=:job_id and job_status.is_approved=1";
$sth=$conn->prepare($sql);
$sth->bindValue("job_id",$job_id);
try {
$sth->execute();
}
catch(Exception $e) {
Rollbar::report_exception($e);
}
$res=$sth->fetchAll();
$stats=$res[0]['is_approved'];
$status=$stats?'1':'0';
return $status;
Db connection file:
<?php
require_once('config.php');
try {
$dsn = "mysql:host=$DB_HOST;dbname=$DB_DATABASE";
$conn = new PDO($dsn, $DB_USER, $DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET NAMES 'utf8'");
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>

Some servers have query limit same is the case with heroku in cleardb i.e., 3600 QPH. So to resolve this issue we bought the paid version of cleardb to extend the query limit.

Related

Is it possible to connect to server management studio database from joomla?

I am trying a simple datababase connection from a joomla website, to a server management studio database.I have tried serveral different ways, srv, dbo etc.
It still displays errors in its simplest form.
Is it possible to establish such a connection?
Error messages:
Unable to load Database Driver: mssql
Uncaught Error: Call to undefined function mssql_connect()
Call to undefined function sqlsrv_connect()
simplest:
<?php
// Create a link to MSSQL
$con = mssql_connect('server1', 'name', 'pass');
// Select the database 'php'
mssql_select_db('database1', $con);
?>
way2:
<?php
$servername = "s1";
$username = "u1";
$password = "p1";
try {
$conn = new PDO("sqlsrv:Server=$servername;Database=db1", $username, $password);
// set the PDO error mode to exception
$setAttribute = $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

My file corrupt when using db connection

I have a download script which get id of file and search in database and find it's name. But when I include my db connection the files get corrupted on download.
when I comment my db connection and give file name manually file downloading work fine.
I test my db connection and there wasn't any excpetion or any html output , what do you think my problem is?
<?php
session_start();
try{
$db= new PDO("mysql:host=localhost;dbname=dbname","user","pass");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$db->exec("SET NAMES 'utf8'");
}catch (Exception $e){
//echo "something wrong in db.php";
echo $e->getMessage();
exit;
}
?>
I run my code on windows server IIS, if it does matter
Just try with this code..
error_reporting(E_ALL);
ini_set('display_errors','1');
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
It will return your error.
after few days struggling and headache finaly problem resolved by deleting try catch block

PDO access denied for user 'username'#'%'

I need to connect to a remote mysql server using a php page;the connection itself works as a charm, but the moment i try to create the database, as it doesn't exist yet, i get this error:
exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user '[myusername]'#'%' to database '[mydbname]'' in [myurl]/php/db_manager.php:76
As you can see, i have access denied to "%".
Now: what is "%"?
Furthermore:
Main file
private function createDB() {
if($this->cm->update("CREATE DATABASE IF NOT EXISTS " . $this->dbName . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", array())) { // Error here
$this->cm->update("USE " . $this->dbName . ";", array());
return true;
}
return false;
}
$this->cm is an instance of a correctly initialized PDO wrapper
PDO wrapper file
public function update($query, $values)
try{
$sql = $this->db->prepare($query);
$sql->execute($values);
return true;
} catch(PDOException $e) {
$this->l->error($e); // <- Error here
return false;
}
}
$this->db is a correctly instantiated, fully connected PDO object;These are the lines used to connect
$this->db = new PDO($connection, $this->db_user, $this->db_password, array(PDO::ATTR_PERSISTENT => true));
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
I have full access on the Mysql server
Access denied for user '[myusername]'#'%' to database '[mydbname]'
MySQL permissions are granular: not all users have full access to all databases on the server.
You need to sign in with an administrator and grant the appropriate permissions. For instance, to grant full access:
GRANT ALL
ON mydbname.*
TO 'myusername'#'%'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
... or you can be more selective to your liking:
GRANT SELECT, ALTER, CREATE, DELETE, DROP, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE
ON mydbname.*
TO 'myusername'#'%';
FLUSH PRIVILEGES;
Please check Privileges Supported by MySQL for a full list.
% is a wildcard explained in detail at Account Names and Passwords that means "connection from any host".
I had similar problem on xampp. changed my password to auto generated password(by Generate Password Button) and then copy + paste in config file. worked successfully!
code :
// config information
require_once "config.php";
// make connection string
$connection_string = "mysql:host=" . DB_HOST . ":" . DB_PORT . ";dbname=" . DB_NAME;
$connection = null;
// show connection string
echo $connection_string."</br>";
// try to connect to db or catch exceptions
try{
$connection = new PDO( $connection_string, DB_USER, DB_PASS );
}catch (PDOException $exc){
echo '<pre>';
print_r($exc);
echo '</pre>';
}
echo '</br>';
// connection status
if ( $connection ) {
echo "Connected Successfully!";
} else {
echo "Connection Failed!";
}
Result:
mysql:host=127.0.0.1:3306;dbname=php_pdo_db
Connected Successfully!
In the PDO query add the port after host, (in my case changed the port to 8111) and it should work.
code :
//write in database
try {
$dbh = new PDO("mysql:host=$db_host:8111;dbname=$db_name", $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec($database);
}
catch(PDOException $e) {
if ($e->getCode() == 2002) {
header('location: step3.php?_error=Unable to Connect Database, Please make sure Host info is correct and try again !');
exit;
}
elseif ($e->getCode() == 1045) {
header('location: step3.php?_error=Unable to Connect Database, Please make sure database username and password is correct and try again !');
exit;
}
elseif ($e->getCode() == 1049) {
header('location: step3.php?_error=Unable to Connect Database, Please make sure database name is correct and try again !');
exit;
}
}

PHP: execute query via odbc on MS-SQL database

From my PHP Skript I try to execute a query at a MS-SQL Database.
The connection can be established, but I got everytime error: "Segmentation fault".
What is the cause for this error?
the line which cause the error ist the FETCH statement!!
My PHP Code:
<?PHP
try
{
$db = new PDO("odbc:ms-sql","<user>","<passwort>");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo"Connection established";
echo"\n";
}
catch (PDOException $Exception)
{
echo " No connection established.";
echo"\n";
echo $Exception->getMessage();
exit(UNKNOWN);
}
try
{
$query_result = $db->query(select * from dbo.TEST where MAKE_DATE LIKE '%2014-08%');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $Exceptionsql)
{
echo " SQL-Query failed. \n\n";
echo $Exceptionsql->getMessage();
exit(UNKNOWN);
}
$result = $query_result->fetch(PDO::FETCH_ASSOC);
//close DB connection
$db = null;
var_dump($result);
?>
I guess that the string to execute the query is wrong.
I would be grateful if somebody could help me.

PHP PDO Doesn't insert new records

I do not understand what my problem seem to be.
I got the following PHP code:
include_once 'db.inc.php';
try
{
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
}
catch(PDOException $e)
{
echo 'Connection failed: ', $e->getMessage();
exit();
}
$title = htmlentities($_POST['title']);
$entry = htmlentities($_POST['entry']);
$sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";
$stmt = $db->prepare($sql);
$stmt->execute(array($title, $entry));
$stmt->closeCursor();
I do not receive any error of any kind and the script seem to have worked however it does not insert anything into the database.
No matter what I try it doesn't do anything.
edit
Sorted :)
I didn't know about $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);.
It gave me "Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'".
Turns out I wrote mysql:host=127.0.0.1;db_name=test1 instead of mysql:host=127.0.0.1;dbname=test1 in my config file.
Thank you very much for help!
Set PDO to throw exceptions when execution fails
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Categories