Pdo Error: SQLstate[Hy000]: General Error: 2031

PDO error: SQLSTATE[HY000]: General error: 2031

You cannot use ->bind* and ->execute($params). Use either or; if you pass parameters to execute(), those will make PDO forget the parameters already bound via ->bind*.

How to debug SQLSTATE[HY000]: General error: 2031 in prepared statements

The $query_array is already an array so when you run $stmt->execute(array($query_array)) you are making into a multidimensional array. This:

$stmt->execute($query_array);

should be all you need.

As to the bind issue you aren't using the PDO syntax

  • http://php.net/manual/en/pdostatement.bindparam.php

You are using the mysqli syntax with sssiii.

  • http://php.net/manual/en/mysqli-stmt.bind-param.php.

PDO error: General error: 2031

Just making my comment an answer:

If dbconnect is an instance of PDO then query both creates a prepared statement and then executes it all in one go. So its not getting the parameters bound initially. Use PDO::prepare instead of PDO::query.

Got error SQLSTATE[HY000]: General error: 2031 when trying to execute if statement

if (isset($_GET['id'])) {
try {
$id = $_GET['id'];
$requestedstatus = "SELECT change_request FROM timesheet WHERE id = :id";
$results = $db->prepare($requestedstatus);
$results->bindValue(':id', $id);
$results->execute();
$requestedresult = $results->fetch(PDO::FETCH_ASSOC);
foreach ($requestedresult as $thatshitsrequested)
echo $thatshitsrequested;
if(!$user->isChangeRequested($thatshitsrequested)){
$isrequested = "SELECT * FROM timesheet WHERE id = :id";
$statement = $db->prepare($isrequested);
$statement->bindValue(':id', $id);
$statement->execute();
$edited = $statement->fetch(PDO::FETCH_ASSOC);
}
else {
$notrequested = "SELECT approve_status FROM timesheet WHERE id = :id";
$statement = $db->prepare($notrequested);//submit_day, time_in, time_out, approve_status
$statement->bindValue(':id', $id);
$statement->execute();
$edited = $statement->fetch(PDO::FETCH_ASSOC);
}

} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "Something went wrong!";
exit;
}

PHP Uncaught PDOException: SQLSTATE[HY000]: General error: 2031

Array_shift was changing the url so it didn´t work.

Solution:

      public function zpracuj($parametry)
{
$naparsovanaURL = $this->parsujURL($parametry[0]);

$naparsovanaURL2 = $naparsovanaURL;

if(empty($naparsovanaURL[0]))
$this->presmeruj('clanek/uvod');
// kontroler je 1. parametr URL
$tridaKontroleru = $this->pomlckyDoVelbloudiNotace(array_shift($naparsovanaURL2)) . 'Kontroler';

if (file_exists('kontrolery/' . $tridaKontroleru . '.php')){
$this->kontroler = new $tridaKontroleru;
$naparsovanaURL = $naparsovanaURL2;
}

elseif (SpravceStranek::dotazNaStranku($naparsovanaURL)>0) {
$this->kontroler = new StrankyKontroler();

Laravel - SQLSTATE[HY000]: General error: 2031

selectRaw and whereRaw allow you to use placeholders and pass an array of values as the second argument:

selectRaw('*, st_distance_sphere(homes.geopoint, point(?, ?)) as dist', [$data['lng'], $data['lat']])

Laravel DB::select returns General error: 2031

I recommend you to read the select docs from Laravel Query Builder.

The Query Builder provides a complete API to make it easier and safer. Your query would look like:

$results = DB::table("users")->select("id")->where("email",$email);

EDIT

The problem is that you're trying to pass a named parameter, but inside the string you didn't named it.

As the doc example shows:

$results = DB::select('select * from users where id = :id', ['id' => 1]);

Your query should look like:

$sql = 'SELECT id FROM users where email=:email';
$results = DB::select($sql, ['email' => $email]);


Related Topics



Leave a reply



Submit