Solved

Getting datatype on pdo Bind

Posted on 2010-09-21
7
378 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.
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 …

895 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

13 Experts available now in Live!

Get 1:1 Help Now