Oct
14
2008
3

MySQL issue with character set

Today i stumbled upon an issue with inserting text into a database, and the character set wasn’t taking it. No mather what i changed in the database, it was inserted wrong time after time.

Characters like â and é are shown wrong. In the variable it is correct, but in database it isn’t.

This is what i had:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

But when i looked into the database it wasn’t correct. Wrong encoding. The tabel was set UTF-8 so that couldn’t be wrong.

This was my solution:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" SET CHARACTER SET utf8 ");
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

Now it was correct inserted, so i could use it properly.

Written by Rene Pot in: PHP Problems, Sugestions | Tags: , , , , ,
Sep
26
2008
1

Work with function ezSQL

Since a couple of weeks i work with ezSQL. And i came to the conclusion this function is much more usefull than the normal mySQL tags. It is very easy in ezSQL to get 1 row, or multiple. And the output is in a simple array!

I’ll just show you with some code below.

<?php
 
// First connect to database
 
$db = new ezSQL_mysql(DB_USER,DB_PASSWORD,DB_NAME,DB_HOST); 
 
// Get items from database
 
$playlists = $db->get_results("SELECT * FROM playlist ORDER BY id DESC");
 
// And now just print it.
 
foreach ($playlists as $item) {
    echo('Name is: '. $item->name .'.');
}
 
?>

As you can see it works very easy with a database. Besides get_results ezSQL has some other functions.

<?php
 
$row = $db->get_row(" SELECT * FROM playlist WHERE id=5"); // Just get 1 row
 
$var = $db->get_var(" SELECT name FROM playlist WHERE id=5"); // Just get 1 variable
 
?>

Ofcourse there are many other features in ezSQL. I would say try it for yourself! Go get it at http://www.woyano.com/jv/ezsql

Thanks for reading this post! I hope it has been of good use.

Examples and documentation can be found Here

Written by Rene Pot in: Sugestions | Tags: , , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes