Connect to database, problem with charsets

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
erikhedborg
New Member
New Member
Posts: 4
Joined: Fri May 15, 2009 7:39 am

Connect to database, problem with charsets

Post by erikhedborg »

Hello!

I have, in the same database as cmsms, created a table with a clients prize list on some books. To access the database I used a method i found, actually related to connecting to another database, and patched together a code, and put it in a user defined tag. It works exept for two thing - characters in swedish like Å Ä and Ö are replaced with some kind of question mark symbol, and new lines are not transfered.

This must have something to do with the code below since phpmyadmin shows the Å Ä and Ö characters as normal?

This is the code:

Code: Select all

global $gCms;
$conn =& $gCms->GetDb();

// Setup connection variables
$server = 'mysql.a10.levonline.com';
$userid = 'a1006001';
$password = '********';
$database = 'a1006000_db1';

// Connect to the database
$conn->PConnect($server, $userid, $password, $database);

  
  $conn->SetFetchMode(ADODB_FETCH_ASSOC); 
$conn->charSet = utf-8;

  $result = $conn->Execute('SELECT * FROM litteratur');
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  } else {
$result->MoveNext();
  while (!$result->EOF) {
    //print $result->fields['artnr']." ".$result->fields['benamning']."<BR>";
    
 

echo '
<table width="640" border="0" cellpadding="2">
  <tbody>
    
    <tr>
      <td colspan="4" bgcolor="#111111"><span style="color: #FF9900; font-weight: bold; font-family: Arial, Helvetica, sans-serif;">'.$result->fields['benamning'].'</span></td>

    </tr>
    <tr>
      <td width="97"><span style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; "><span style="font-size: 14px">art nr '. $result->fields['artnr'].'</span></span></td>
      <td width="419"><span style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; "><em>'.$result->fields['forfattare'].'</em></span></td>
      <td width="52"><span style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; ">'.$result->fields['vikt'].'</span></td>
      <td width="46"><span style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; ">'.$result->fields['pris'].'</span></td>

    </tr>
    <tr>
      <td height="151"><img src="'.$result->fields['bildaddr'].'" alt="" name="" width="96" height="130" /></td>
      <td><p style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; ">'.$result->fields['beskrivning'];

if(!($result->fields['beskrivning'])) echo $result->fields['beskrivning2'];

echo '</p>
      <p style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; "> </p></td>
      <td colspan="2"><span class="style2">'.$result->fields['prismedmoms'].'</span> <span style="font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; ">kr inkl moms</span></td>
    </tr>

  
    
  </tbody>
</table>';
$result->MoveNext();  //  Moves to the next row
 }  // end while
} // end else



// Re-connect to the CMSMS database
$conn->PConnect(
  $gCms->config['db_hostname'], 
  $gCms->config['db_username'], 
  $gCms->config['db_password'], 
  $gCms->config['db_name']
);
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Connect to database, problem with charsets

Post by calguy1000 »

a) This is the wrong forum
b) if your table is in the same database as the cmsms tables, then why are you opening another connection... You don't need to.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
erikhedborg
New Member
New Member
Posts: 4
Joined: Fri May 15, 2009 7:39 am

Re: Connect to database, problem with charsets

Post by erikhedborg »

I'm sorry, which board should I turn to?

The reason for me opening another connection is portability. The litterature list will eventually be moved to another database.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Connect to database, problem with charsets

Post by calguy1000 »

in which case it's not a CMSMS question at all.... it's a general programming question.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
erikhedborg
New Member
New Member
Posts: 4
Joined: Fri May 15, 2009 7:39 am

Re: Connect to database, problem with charsets

Post by erikhedborg »

Ah, sorry. Now realised!
Sonya

Re: Connect to database, problem with charsets

Post by Sonya »

erikhedborg wrote:

Code: Select all

$conn->charSet = utf-8;
You have a mistake in your script, utf-8 is a string, not a variable, it should be

Code: Select all

$conn->charSet = "utf-8";
Post Reply

Return to “Developers Discussion”