Solved

Getting datatype on pdo Bind

Posted on 2010-09-21
7
377 Views
Last Modified: 2013-12-12
Hi,

I've go this method:

public function getTables($procname,$parameter) {
      //see : http://www.php.net/manual/en/pdo.prepared-statements.php example #10 and #11
      $vars = explode(",", $parameter);
      $num = count($vars);
      //create holders for calls
      for ($i = 0; $i <= $num; $i++) {
         $holder.="?";
         if ($i != $num)
         {
            $holder.=",";
         }
      }
      $stmt = DBO::prepare("EXECUTE " . $procname . " " . $holder);
      // TODO build bind, may need to change type for strings and ints and others
      // perhaps look to put into dbcalls
      for ($i = 0; $i <= $num; $i++) {
         $stmt->bindParam($i + 1, $vars[$i], PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT, PDO::ERRMODE_WARNING, 4000);
      }

// call the stored procedure
      try
      {
        $stmt->execute();
      } catch (PDOException $e)
      {

         //var_dump($e);
      }
      $data = ($stmt->fetchAll(PDO::FETCH_ASSOC));
      $stmt->closeCursor();
      return $data;
   }

Open in new window


It dynamically extracts data from a database, It has one problem though, it treats the id columns it finds as floats as opposed to integers, ie 2345 comes back a 2345.0, ideally the call should detect that the column is a id and display it as so. Does anyone know how how this could be done?

It could be done by post processing the data, but I would like to prevent that.
0
Comment
Question by:darren-w-
  • 4
  • 2
7 Comments
 
LVL 3

Assisted Solution

by:Progressed
Progressed earned 200 total points
ID: 33723948
and your sure that the ID is correctly in the DB?
0
 
LVL 13

Author Comment

by:darren-w-
ID: 33724103
yes
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33725912
what data type is the "id" field in the database?
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Author Comment

by:darren-w-
ID: 33726067
PK,numeric(18,0) this is in MS SQL
0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 300 total points
ID: 33726305
that is equivalent to decimal(18,0), since not storing any scale, I don't see why this would be chosen as the datatype over int, but thats beyond the case.

It is coming back with .0 because of the datatype. You could cast or convert the return value before returning it so that it displays the way you want, or you could use intval php function to convert it to an integer before displaying it.
0
 
LVL 13

Author Comment

by:darren-w-
ID: 33760657
Unfortunately I have now way of knowing what the data type of each field is going to be before the procedure is called,  but this will not be a problem in most cases as the data will be imported into a spreadsheet.

The id fields show up on the database as integers, will need to ask the db owner why they us this instead if an integer.

Have got around the problem using casting.
0
 
LVL 13

Author Closing Comment

by:darren-w-
ID: 33760661
Used casting in receiving script
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now