Asked  1 Year ago    Answers:  5   Viewed   13 times

I'm attempting to learn and use PDO in PHP. I've come across an issue in the query() method.

I'm attempting to use $sth = $db->query("SELECT * FROM titles ORDER BY RAND() LIMIT 1"); to randomly select a title for a website, but for some reason, $sth is always false. It works when I use prepare() and execute(), but I'm trying to find what's wrong in query().

Here's my entire function that is being called:

function getTitle($db)
    {
    if($db)
        {
            $db->exec("USE " . $dbsite); 
            $sth = $db->query("SELECT * FROM titles ORDER BY RAND() LIMIT 1");
            $title = $sth->fetch(PDO::FETCH_ASSOC);

            $db->exec("UPDATE titles SET count = count + 1 WHERE id = " . $title['id']);

            return $title['title'];
        }
    else
            return 'Home - Database Offline';

}

 Answers

5

Use PDO's errorinfo() function to find out why.

if( ! $sth = $db->query("SELECT * FROM titles ORDER BY RAND() LIMIT 1") ) {
  die(var_export($db->errorinfo(), TRUE));
}

Late Update

In the interest of making my old answers better, setting PDO to throw exceptions on error is far more manageable than checking every function return.

$dbh = new PDO($connstr, $user, $pwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Or, more concisely:

$dbh = new PDO($connstr, $user, $pwd, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
Thursday, April 1, 2021
 
Juicy
 
2

docs

you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

class ProjectController extends Controller
{
    /**
     * All of the current user's projects.
     */
    protected $projects;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->projects = Auth::user()->projects;

            return $next($request);
        });
    }
}
Thursday, April 1, 2021
 
dzm
 
dzm
5

The following works for me:

<?php

$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "pass");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$stmt = $pdo->prepare("INSERT INTO `null_test` (`can_be_null`) VALUES (:null)");
$stmt->bindValue(":null", null, PDO::PARAM_NULL);

$stmt->execute();

Pass in PHP's null, with type of PDO::PARAM_NULL. Also, make sure your prepare emulation is set to false. That might help.

Thursday, April 1, 2021
 
1

lastInsertId() is a method of the PDO class, not the PDOStatement class.

This should work:

$groupID = $dbo->lastInsertId();
Thursday, April 1, 2021
1

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

http://us3.php.net/manual/en/language.types.string.php#language.types.string.casting

If you want to print a boolean for debugging you can use var_dump or print_r.

Thursday, April 1, 2021
 
qitch
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :