Solved

Getting datatype on pdo Bind

Posted on 2010-09-21
7
383 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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 …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

691 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