Solved

Getting datatype on pdo Bind

Posted on 2010-09-21
7
379 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 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