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 3666306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:51:51+00:00 2026-05-19T01:51:51+00:00

EDIT: I’m marking the answer below correct because of our conversation below. Feel free

  • 0

EDIT:

I’m marking the answer below correct because of our conversation below. Feel free to post an answer to the unanswered parts and I’ll be sure to check back here. Thanks.

=-=-=-=-=-=-=-=-

I’m trying to have one function handle all prepared statements. In my older code, I was mysql_fetch_assoc-ing through select queries. I’d like to just return an array if I’m doing a select and maybe lastInsertId or affectedRows or something if I’m doing an Insert Update or Delete.

My question is, aside from parsing the sql for ‘select’ or ‘insert’ or adding another parameter to the function like $type = ‘Select’ is there something in the PDO class that lets you know if there’s data?

I saw somewhere that you can test if the value returned by fetch or fetchAll is true. Is that what I’m supposed to do?

I’m open to any feedback on the function, too.

function pdo_query($sql,$data)
{
$stmt = $dbh->prepare($sql);
$first_row = true;

if(is_array($data)){
    foreach($data as $row){
        if(is_array($row)){
            foreach($row as $param => $value){
                if($first_row){
                    //http://bugs.php.net/43130 parameters can't have dashes
                    $stmt->bindParam($param, ${substr(str_replace('-','_',$param),1)});
                }
                ${substr(str_replace('-','_',$param),1)} = $value;
            }
            $stmt->execute();
            $first_row = false;
        }
    }
    while ($row = $stmt->fetch()) {
        $return[] = $row;
    }
}
return $return;
}

Edit:
I haven’t tried this yet, but is there any problem testing for $stmnt->fetch()? Also, if I did want to automate getting lastInsertId() or affected rows, it doesn’t seem like I can figure out what type of query I’m doing after the fact. Here’s where I’m at:

if ($rows = $stmt->fetchAll()) {
        return $rows;
    } else if (some_way_of_determining_if_an_insert_happened) {
        return $dbh->lastInsertId();
    } else {
        return some_way_of_returning_affected_rows
    }
}
  • 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-19T01:51:52+00:00Added an answer on May 19, 2026 at 1:51 am

    Don’t feel too smart.

    Make a set of methods.

    • A general of them – called query, that returns a resource type.
    • A set of helpers, which by using this query method returns a scalar value, a row, a column and a set of rows.
    • Utility methods, to get number of affected rows, returned rows, insert id and such from given resource.

    Also a method to produce SET statement would be useful for use with insert and update methods. See this one for the example

    Examples:

    //SELECT helpers:
    $username = $db->getOne("SELECT name FROM users WHERE id=?",$id); //getting scalar
    $userinfo = $db->getRow("SELECT * FROM users WHERE id=?",$id); //getting row
    $newsdata = $db->getAll("SELECT * FROM news LIMIT ?,?",$start,$rows); //getting array
    
    //Select for mass emailing
    $res = $db->query("SELECT * FROM notify WHERE topic_id = ?",$topic);
    $num = $db->numRows($res);
    while ($row = $db->next()){
      mail(...);
    }
    
    //insert
    $res = $db->query("INSERT INTO users SET name = ?",$name);
    $id  = $db->lastId();
    
    //delete
    $res  = $db->query("DELETE FROM users WHERE id = ?",$id);
    $rows = $db->affectedRows();
    

    However, I am not sure about affectedRows() and lastId methods, as them takes link identifier as a parameter, not resource identifier….

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

Sidebar

Related Questions

No related questions found

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.