Codeigniter : Problem inserting accents in Mysql

By : Manu
Source: Stackoverflow.com
Question!

I'm trying out code igniter, and I came across an error message while trying to insert a new row in my Mysql database.

The text I'm trying to insert is in French, and contains some accents.

Here's my code :

	$data= array(
		'title' => $this->input->post('title'),
		'date' => $this->input->post('date'),
		'mytext' => $this->input->post('mytext')
		);

	$this->db->insert('blog', $data);

This code seems to work fine (I've inserted a few "test test" entries), but when I try to enter something with accents, for instance "Il était là", I get an error message :

A Database Error Occurred

Error Number: 1366

Incorrect string value: '\xE9tait ...' for column 'mytext' at row 1

I've looked on the code igniter forums for a fix, but all they say is to change the collation to UTF8, in mysql. I've tried that, but I keep having the same problem.

By : Manu


Answers

Try to uft8-encode it before you insert to the database.

utf8_encode($string)


First: Try to avoid <xsl:for-each>. It's a bad choice most of the time.

Second: Select only those nodes that you want to output and your row alternation will work:

<xsl:template match="/case-studies">
  <xsl:apply-templates select="case-study[
    category = $catName 
    and
    category/@solution = $solName
  ]">
    <xsl:sort select="name" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="case-study">
  <tr>
    <xsl:if test="position() mod 2 = 1"> 
      <xsl:attribute name="bgcolor">#e7e7e7</xsl:attribute>
    </xsl:if>    
    <td class="cell1" />                
    <td class="cell2" style="padding-top:2px;">
      <xsl:text>» </xsl:text>
      <a href="{url}"><xsl:value-of select="name"/></a>
    </td>
    <td class="cell3">
      <xsl:apply-templates select="solutionType" />
    </td>
  </tr>
</xsl:template>

<xsl:template match="solutionType">
  <div class="clearRight">
    <xsl:value-of select="."/>
  </div>
</xsl:template>

Edit:

You can use an <xsl:key> to speed up the selection process, but this will only have a positive effect if you query the same data repeatedly during the same transformation process.

<xsl:key name="kCaseStudy" 
         match="case-study" 
         use="concat(category, category/@solution)" 
/>

<!-- no need to be in "/case-studies" context this time -->
<xsl:template match="/">
  <xsl:apply-templates select="key('kCaseStudy', concat($catName, $solName))">
    <xsl:sort select="name" />
  </xsl:apply-templates>
</xsl:template>

<!-- ... code that uses "key('kCaseStudy', ...)" again ... -->
By : Tomalak


He was referring to a project type in VS that supports managing databases, including (if you set it up correctly), versioning your database publishes.

Check out this article on Database Projects in VS

By : Joseph


This video can help you solving your question :)
By: admin