-3

Can i rewrite this function for PDO or mysqli use? If so, please provide an example.

function dbquery($query) {
    global $mysql_queries_count, $mysql_queries_time; $mysql_queries_count++;

    $query_time = get_microtime();
    $result = @mysql_query($query);
    $query_time = substr((get_microtime() - $query_time),0,7);

    $mysql_queries_time[$mysql_queries_count] = array($query_time, $query);

    if (!$result) {
        echo mysql_error();
        return false;
    } else {
        return $result;
    }
}

function dbrows($query) {
    $result = @mysql_num_rows($query);
    return $result;
}

function dbarray($query) {
    $result = @mysql_fetch_assoc($query);
    if (!$result) {
        echo mysql_error();
        return false;
    } else {
        return $result;
    }
}

<?php $result = dbquery("SELECT * FROM ".DB_THREADS." INNER JOIN ".DB_USERS." WHERE thread_lastuser=user_id ORDER BY thread_lastpost DESC LIMIT 5");
if (dbrows($result) != 0) {
while ($data = dbarray($result)) { 
<?php echo $data['thread_id'];
<?php } } ?>

Thanks

Jack
  • 1
  • Jack, this is not a "code it for me" website. You can try your own and then ask questions for what did not turn out well for you or the errors you did get. – hakre Jul 16 '11 at 00:38
  • Which function would you like rewritten, because unless I'm mistaken, you've posted ***3*** functions! – Dan Lugg Jul 16 '11 at 00:39

2 Answers2

3

Yes, you can! Here is something to get you started:

Community
  • 1
  • 1
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
  • Sorry, not asking if you can rewrite all of it.. Just provide an example. Can the functions above be edited and work as is for PDO usage without changing the query of the code. – Jack Jul 16 '11 at 00:44
  • 1
    If you check my links you will know everything you need to know about pdo within 30 minutes. __If you give a man a fish you feed him for a day, if you teach a man to fish you feed him for a lifetime.__ ... And yes, the query can be the same. – Sascha Galley Jul 16 '11 at 00:50
3

Here's your code rewritten to use PDO:

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    die();
}

function dbquery($query) {
    global $db, $mysql_queries_count, $mysql_queries_time; $mysql_queries_count++;

    $query_time = get_microtime();
    $result = $db->query($query);
    $query_time = substr((get_microtime() - $query_time),0,7);

    $mysql_queries_time[$mysql_queries_count] = array($query_time, $query);

    if (!$result) {
        echo $db->errorCode();
        return false;
    } else {
        return $result;
    }
}

function dbrows($query) {
    global $db;
    $result = $db->exec($query);
    return $result;
}

function dbarray($results) {;
    $result = $results->fetch(FETCH_ASSOC);
    if (!$result) {
        echo $results->errorCode();
        return false;
    } else {
        return $result;
    }
}

<?php $results = dbquery("SELECT * FROM ".DB_THREADS." INNER JOIN ".DB_USERS." WHERE thread_lastuser=user_id ORDER BY thread_lastpost DESC LIMIT 5");
if (dbrows($results) != 0) {
while ($data = dbarray($results)) { 
<?php echo $data['thread_id'];
<?php } } ?>

I've changed as little as possible, just to show you the functioning of PDO, so I didn't make some improvements that would be possible to it. For more information, take a look at the documentation for PDO.

EdoDodo
  • 8,220
  • 3
  • 24
  • 30