PHP PDO Fetch_INTO

The first field returned by the following code contains some unknow characters when loading into an array which prevents a match. Inserting a “dummy” field as the first field in the array allows the match I’d like to know what I’m doing wrong

Thanks

Frank Polan
Function snippett -

  try {
      $obj = new  softwareRecordsClass($this->DBH, $this->eP, $this->dateClass) ;
      $query = "SELECT s.PackageId, s.Vendor, s.Manufacturer, s.Cost, s.PackageName, s.PackageModifier,
                       s.Versionnumber, s.Media, s.MediaLocation, s.DateObtained,
                       s.Comments, s.FileName
                FROM software s WHERE s.PackageId = :PackageId" ;
      $STH  = $this->DBH->prepare("$query");
      $STH->bindparam(':PackageId', $id);
      $STH->setFetchMode(PDO::FETCH_INTO, $obj);
      $STH->execute();
      if ($STH == false)  {
          $message = "select failed #122110";
          $this->eP->showError($message);
      }   // end if
      $row = $STH->fetch();
      $fields = $this->setFields('details', $row);
      return $fields;
  }   // end of try

------------- end of snippet --------------

------ load array snippet ---------------
$fA = array();
$fA[‘filler’] = “Filler”;
$fA[‘PackageId’] = $row->PackageId;
$fA[‘Vendor’] = $row->Vendor;
$fA[‘Manufacturer’] = $row->Manufacturer;
$fA[‘Cost’] = $row->Cost;
$fA[‘PackageName’] = $row->PackageName;
$fA[‘PackageModifier’] = $row->PackageModifier;
$fA[‘Versionnumber’] = $row->Versionnumber;
$fA[‘Media’] = $row->Media;
$fA[‘MediaLocation’] = $row->MediaLocation;
$fA[‘DateObtained’] = $row->DateObtained;
$fA[‘Comments’] = $row->Comments;
$fA[‘FileName’] = $row->FileName;

When you create your PDO object pass an additional argument for UTF-8 setting.


new PDO('mysql:host=localhost;dbname=exampledb', 'foo', 'bar', array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8') //<-- here
)

Also make sure your htmlis set to utf8.

I made the changes, and it appears to run okay, but I’m still getting the extra characters in the first element in the array
Heres the PHP & HTML code. Have I missed something?
Thanks
Frank Polan

printf(“<meta content=‘text/html; charset=utf-8’ http-equiv=‘Content-Type’ />”);


$DBH =
new PDO($dsn, $user, $password, array( PDO::MYSQL_ATTR_INIT_COMMAND => ‘SET NAMES utf8’)) ;//<– here) Also

//
$DBH->setAttribute( PDO::ATTR_PERSISTENT, true );
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}

What are those extra characters that you are getting?
Perhaps someone else will recognise them from a similar experience that they have had.

They’re showing up in an XMLHttp response string. So far I haven’t figured out how to actually see them. In the javascript Alert() they just show as spaces preceeding the actual data. If I check the length of the variable it’s longer than the actual text.

Thanks

Frank Polan

You can use charCodeAt to examine what they are.

For example:


var str = 'some string',
    charcodes = '',
    i;
for (i = 0; i < str.length; i += 1) {
    charcodes += str.charAt(i).charCodeAt() + ' ';
}
alert(charcodes);

I ran the code and got the codes that preceeded the actual data - 13 10. However in this context they don’t seem to be relevant. 13 is Enter and 10 wasn’t listed in the table I checked. The data is coming from a SELECT statement

Thanks

Frank Polan

13 is the code for a carriage return, which is
10 is the code for a linefeed, which is \

Windows machines use \r
to denote a new line.
Linux machines use
to denote a new line.
Apple machines use \r to denote a new line.

It looks like the \r
is coming from a blank line at the start of your data.

Okay, that makes sense. Here is the code that creates the XMLHttp response.
The characters are being prepened to $obj->PackageId. Can you see anything I’m doing that might be causing it?

Thanks

Frank Polan


  try {
      $query = "SELECT s.PackageId, s.Vendor, s.Manufacturer, s.Cost, s.PackageName, s.PackageModifier,
                       s.Versionnumber, s.Media, s.MediaLocation, s.DateObtained,
                       s.Comments, s.FileName
                FROM software s WHERE s.PackageId = :PackageId" ;
      $STH  = $this-&gt;DBH-&gt;prepare("$query");
      $STH-&gt;bindparam(':PackageId', $id);
      $STH-&gt;setFetchMode(PDO::FETCH_INTO, $this);
      $STH-&gt;execute();
      if ($STH == false)  {
          $message = "select failed #122110";
          $this-&gt;eP-&gt;showError($message);
      }   // end if
      $obj = $STH-&gt;fetch();
      $responseText = sprintf("PackageId=%u&Vendor=%s&Manufacturer=%s&Cost=%u&
                               PackageName=%s&PackageModifier=%s&Versionnumber=%s&
                               Media=%s&MediaLocation=%s&Dateobtained=%s&Comments=%s&FileName=%s",
                               $obj-&gt;PackageId, $obj-&gt;Vendor, $obj-&gt;Manufacturer, $obj-&gt;Cost,
                               $obj-&gt;PackageName,$obj-&gt;PackageModifier,$obj-&gt;Versionnumber,
                               $obj-&gt;Media,$obj-&gt;MediaLocation,$obj-&gt;DateObtained,
                               $obj-&gt;Comments,$obj-&gt;FileName);
       
      return $responseText;;
     // end of try
  }

Let’s try and narrow down where the problem is happening.

Do you still get \r
when $responseText is just an empty string?

If the \r
problem remains, it’s being introduced from somewhere else.
If the problem goes away though, then we should take a closer look at the above code.

Paul

I put in a logging routine in the PHP code. The “empty” XMLHttp response text looks okay in PHP. But the Javascript ALERT still shows the \r
so it appears to be something I’m doing there.

I’ll do so further testing tomorrow and let you know if I find it

Thanks

Frank Polan

The most likely cause is a newline before <?php or after the ?>

If it’s the latter, as it quite likely is, a good safety measure is to remove the ?> from the end of the code completely. That will set you right.

Paul

Never did figure out what was causing problem but it seemed to be related to my calling sequences -

PHP page1 -> javascript(AJAX) -> PHP page2 -> PHP Class and return back up the chain

Changed to

PHP page1 -> javascript(AJAX) -> PHP page2 (included previous class processing here) and return back up chain

and the problem went away

Thanks for your help

Frank Polan