Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:22:07+00:00 2026-05-23T09:22:07+00:00

After taking some advice from people on here in a previous thread, I’m trying

  • 0

After taking some advice from people on here in a previous thread, I’m trying to convert my MySQL to PDO, but am running into some issues.

Here is my original MySQL connection class:

class DbConnector {

    public static function getInstance() {
        static $instance = null;
        if ($instance === null) {
            $instance = new DbConnector();
        }

        return $instance;
    }

    protected $theQuery;
    private $link;

    function DbConnector() {

        $host = 'localhost';
        $db = '';
        $user = '';
        $pass = '';

        // connect to the db
        $this->link = mysql_connect($host, $user, $pass);
        mysql_select_db($db);
        register_shutdown_function(array(&$this, 'close'));
    }

    public function find($query) {
        $ret = mysql_query($query, $this->link);
        if (mysql_num_rows($ret) == 0)
            return array();
        $retArray = array();

        while ($row = mysql_fetch_array($ret))
            $retArray[] = $row;

        return $retArray;
    }

    public function insert($query) {
        $ret = mysql_query($query, $this->link);

        if (mysql_affected_rows() < 1)
            return false;
        return true;
    }

    public function query($query) {
        $this->theQuery = $query;
        return mysql_query($query, $this->link);
    }

    public function fetchArray($result) {
        return mysql_fetch_array($result);
    }

    public function close() {
        mysql_close($this->link);
    }

    public function exists($query) {
        $ret = mysql_query($query, $this->link);
        if (mysql_num_rows($ret) == 0)
            return false;
    }

    public function last_id($query) {
        return mysql_insert_id($query);
    }

}

Here is the function that I’m writing:

function getRandomSubmission() {
    global $db;

    if(!empty($_GET['id'])){
        $submission_id = $_GET['id'];

        $query = $db->find("
            SELECT
                *
            FROM
                `submissions`
            WHERE id = '{$submission_id}'
            LIMIT 1
        ");

    }
    else {
        $query = $db->find("
            SELECT
                *
            FROM
                `submissions`
            ORDER BY RAND()
            LIMIT 1
        ");
    }


    if($query) {
        return $query[0];
    }
    else {
        $query = $db->find("
            SELECT
                *
            FROM
                `submissions`
            ORDER BY RAND()
            LIMIT 1
        ");
    }
}

Here is the PDO connector:

$host = 'localhost';
$username = '';
$pass = '';
$db = '';

try {
    $dbh = new PDO("mysql:host=$host;dbname=$db", $username, $pass);
} catch (PDOException $e) {
    echo $e->getMessage();
}

Here is what I’ve tried to convert it to, but it’s just plain wrong. I think I need to be returning a PDO associative array in the 2nd if statement, but am not sure.

function getRandomSubmission() {
    global $dbh;

    if(!empty($_GET['id'])){
        $submission_id = $_GET['id'];

    $stmt = $dbh->prepare('
            SELECT
                *
            FROM
                `submissions`
            WHERE
                `id` = ?
            LIMIT 1
            ');

    $stmt->bindParam(1, $submission_id, PDO::PARAM_INT);
    $stmt->execute();
    }
    else {
    $stmt = $dbh->prepare('
            SELECT
                *
            FROM
                `submissions`
            ORDER BY RAND()
            LIMIT 1
        ');

    $stmt->execute();
    }


    if($stmt) {
        return $stmt[0];
    }
    else {
    $stmt = $dbh->prepare('
            SELECT
                *
            FROM
                `submissions`
            ORDER BY RAND()
            LIMIT 1
        ');

    $stmt->execute();
    }
}

The original one works as intended, however (I realize I left the connection details blank).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:22:07+00:00Added an answer on May 23, 2026 at 9:22 am

    You need to call fetch method of the PDOStatement object:

    return $stmt->fetch()
    

    Read about the fetch style, really you don’t need FETCH_BOTH 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am taking over some applications from a previous developer. When I run the
I'm writing some C code to parse IEEE 802.11 frames, but I'm stuck trying
Some apps display an Add a caption in the Preview Screen after taking/selecting an
I'm trying to extract text from arbitrary html pages. Some of the pages (which
After taking some small courses and using WPF and C# a bit I've decided
After taking a few years off from programming, I decided to start learning vb.net.
After taking quite a long break from active coding I am just starting to
I am a student and after taking some introductory programming courses in Java, C,
After taking a look at some old code: //Nothing like a destructor!! function destroy()
After taking a look at this SO question and doing my own research, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.