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

  • SEARCH
  • Home
  • 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 589941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:28:35+00:00 2026-05-13T15:28:35+00:00

Trying to get a grasp of using PDO, and I’m using some pre-made functions

  • 0

Trying to get a grasp of using PDO, and I’m using some pre-made functions to make things simpler for when I want to do a query. First one connects, second runs the query.

Unfortunately it won’t let me INSERT rows using dbquery(). SELECT works fine, just can’t seem to get anything else to work.

Here’s the code:

function dbConnect() 
  {
  global $dbh;

  $dbInfo['database_target'] = "localhost";
  $dbInfo['database_name'] = "mysqltester";
  $dbInfo['username'] = "root";
  $dbInfo['password'] = "password";

  $dbConnString = "mysql:host=" . $dbInfo['database_target'] . "; dbname=" . $dbInfo['database_name'];
  $dbh = new PDO($dbConnString, $dbInfo['username'], $dbInfo['password']);
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $error = $dbh->errorInfo();

  if($error[0] != "") 
    {
    print "<p>DATABASE CONNECTION ERROR:</p>";
    print_r($error);
    }
  }

function dbQuery($queryString) 
  {
  global $dbh;

  $query = $dbh->query($queryString);
  $i = 0;

  foreach ($query as $query2) 
    {
    $queryReturn[$i] = $query2;
    $i++;
    }

  if($i > 1) 
    {
    return $queryReturn;
    }
    else
    {
    return $queryReturn[0];
    }
  }
  • 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-13T15:28:35+00:00Added an answer on May 13, 2026 at 3:28 pm

    PDO::query Only works with queries that return a result set (e.g. SELECT)

    For INSERT/UPDATE/DELETE see PDO::exec

    If you are going to be inserting user provided data into your DBMS I strongly suggest using the prepared statement functionality of PDO to provide automatic escaping to prevent SQL injection.

    e.g.

    <?php
    $stmt = $dbh->prepare("INSERT INTO tester1 (name, age) VALUES (?, ?)");
    $stmt->execute(array('James',25));
    

    See PDO::prepare and PDOStatement::execute

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

Sidebar

Related Questions

I'm new to RhinoMocks, and trying to get a grasp on the syntax in
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {
Im trying to get into some basic JavaFX game development and I'm getting confused
I am trying to get a grasp on how to handle updates to a
I am trying to get a grasp on pointers and their awesomeness as well
I am new to hibernate queries, and trying to get a grasp on how
I'm new to Solr search and trying to get a grasp on how to
I'm new to OOP and i'm trying to get a grasp on it. What
I am a CS student trying to grasp some C++ basic concepts. I am

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.