Page 1 of 1

Connect to database, problem with charsets

Posted: Mon Oct 19, 2009 12:35 pm
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']
);

Re: Connect to database, problem with charsets

Posted: Mon Oct 19, 2009 3:03 pm
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.

Re: Connect to database, problem with charsets

Posted: Mon Oct 19, 2009 4:38 pm
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.

Re: Connect to database, problem with charsets

Posted: Mon Oct 19, 2009 4:45 pm
by calguy1000
in which case it's not a CMSMS question at all.... it's a general programming question.

Re: Connect to database, problem with charsets

Posted: Mon Oct 19, 2009 5:21 pm
by erikhedborg
Ah, sorry. Now realised!

Re: Connect to database, problem with charsets

Posted: Tue Oct 27, 2009 2:40 pm
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";