Ελληνικά και MySQL: μια ιστορία αγάπης και μίσους

Συζητήσεις για την βάση δεδομένων MySQL και το phpMyAdmin

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
kchrist
Δημοσιεύσεις: 8
Εγγραφή: 28 Δεκ 2005 01:29

Ελληνικά και MySQL: μια ιστορία αγάπης και μίσους

Δημοσίευση από kchrist » 29 Δεκ 2005 00:54

Καλησπέρα σε όλους.

Χρησιμοποιώ ένα εξαίσιο PHP Framework για τον σχεδιασμό admin panel: το PHPBase Web Framework.

Στο admin panel, μπορεί κανείς να σχεδιάσει XML φόρμες καταχώρησης δεδομένων σε MySQL db, οι οποίες ως tags του XML έχουν τα πεδία συγκεκριμένων πινάκων.

Οι φόρμες είναι 3 ειδών: add, list, edit.
Παρ' ότι τα ελληνικά αποθηκεύονται σωστά μέσω της φόρμας add και φαίνονται σωστά μέσω της list, όταν γίνεται επεξεργασία των δεδομένων η φόρμα edit δείχνει τα ελληνικά κινέζικα :)

Υποψιάζομαι ότι πρόβλημα είναι στον τρόπο που αποθηκεύονται τα δεδομένα στην db.
Έχω PHP/4.3.11 και η σύνδεση στηn db γίνεται μέσω του database.php:

Κώδικας: Επιλογή όλων

<?php
/*
	PHPbase web framework
	copyright &#40;c&#41; 2003 @authors

	$Id&#58; database.php,v 0.0.1 dd/mm/yyyy hh&#58;mm&#58;ss author Exp $
	database manipulation
*/


// return types
define&#40;"DB_RT_ARRAY",0&#41;;		// return row as an assoc aray where fieldnames are keys
define&#40;"DB_RT_OBJECT",1&#41;;		// return row as an object where fieldnames are properties

/**
* database manipulation
*
* @library	Database
* @author	kv9 &#91;Dan Rusanu <mars@sercom.ro>&#93;
* @since	PHPbase 0.0.1
*/


class CDatabase&#123;
	/**
	* database type
	*
	* @var string
	*
	* @access private
	*/
	var $type;

	/**
	* database connection id
	*
	* @var resource
	*
	* @access private
	*/
	var $conn_id;

	/**
	* name of the current selected database
	*
	* @var string
	*
	* @access private
	*/
	var $current_db;

	/**
	* number of queries per session
	*
	* @var int
	*
	* @access private
	*/
	var $num_queries;

	/**
	* specifies if there were any modifications to the database &#91;write queries&#93;
	*
	* @var bool
	*
	* @access private
	*/
	var $modif = FALSE;

	/**
	* initializes module and connects to the database
	*
	* @param array $connect_params connection parameters
	*
	* @return void
	*
	* @acces public
	*
	* @see Connect
	*/
	function CDatabase&#40;$connect_params = ""&#41; &#123;
		$this->name = "database";

		$this->type = $type;

		if &#40;$connect_params != ""&#41;
			$this->Connect&#40;$connect_params&#41;;
	&#125;

	/**
	* connects to the database
	*
	* @param array $connect_params connection parameters
	*
	* @return void
	*
	* @access public
	*/
	function Connect&#40;$connect_params = ""&#41; &#123;
		extract&#40;$connect_params&#41;;
		//resource mysql_connect &#40; &#91;string server &#91;, string username &#91;, string password &#91;, bool new_link !!! &#91;, int client_flags &#93;&#93;&#93;&#93;&#93; &#41;
		$this->conn_id = mysql_connect&#40;$server,$login,$password,TRUE&#41; or die&#40;"CDatabase&#58;&#58;Connect&#40;&#41; error " . mysql_error&#40;$this->conn_id&#41;&#41;;

if &#40;$default != ""&#41;
			$this->SelectDB&#40;$default&#41;;
	&#125;

	/**
	* closes the database connection
	*
	* @return void
	*
	* @access public
	*/
	function Close&#40;&#41; &#123;
		mysql_close&#40;$this->conn_id&#41;;
	&#125;

	/**
	* selects and sets the current database
	*
	* @param string $database
	*
	* @return void
	*
	* @access public
	*/
	function SelectDB&#40;$database&#41; &#123;
		mysql_select_db&#40;$database,$this->conn_id&#41; or die&#40;"CDatabase&#58;&#58;SelectDB&#40;&#41; error"&#41;;
		$this->current_db = $database;
	&#125;

	/**
	* queries the database
	*
	* @param string $query	actual sql query
	*
	* @return resource or NULL
	*
	* @access public
	*/
	function Query&#40;$query,$db = ""&#41; &#123;//debug&#40;$query&#41;;
		$this->num_queries++;
		//$this->SelectDB&#40;$this->current_db&#41;;
		//echo "<br>$query";
		if &#40;$db&#41;
			$result = mysql_db_query&#40;$db ,$query,$this->conn_id&#41;;// or die&#40;mysql_error&#40;&#41;&#41;;
		else
			$result = mysql_query&#40;$query,$this->conn_id&#41; or die&#40;$query . mysql_error&#40;&#41;&#41;;

		if &#40;in_array&#40;substr&#40;$query,0,strpos&#40;$query," "&#41;&#41;,array&#40;"INSERT", "UPDATE", "DELETE"&#41;&#41;&#41;
			$this->modif = TRUE;

		return $result;
	&#125;

	function FetchObject&#40;$result&#41; &#123;
		return mysql_fetch_object&#40;$result&#41;;
	&#125;

	function FetchRow&#40;$result&#41; &#123;
		return mysql_fetch_row&#40;$result&#41;;
	&#125;

	function FetchArray&#40;$result,$result_type = MYSQL_ASSOC&#41; &#123;
		return mysql_fetch_array&#40;$result,$result_type&#41;;
	&#125;

	function NumRows&#40;$result&#41; &#123;
		return mysql_num_rows&#40;$result&#41;;
	&#125;

	function AffectedRows&#40;&#41; &#123;
		return mysql_affected_rows&#40;$this->conn_id&#41;;
	&#125;

	function InsertID&#40;&#41; &#123;
		return mysql_insert_id&#40;$this->conn_id&#41;;
	&#125;

	function NumQueries&#40;&#41; &#123;
		return $this->num_queries;
	&#125;

	function QFetchObject&#40;$query&#41; &#123;
		return $this->FetchObject&#40;$this->Query&#40;$query&#41;&#41;;
	&#125;

	function QFetchRow&#40;$query&#41; &#123;
		return $this->FetchRow&#40;$this->Query&#40;$query&#41;&#41;;
	&#125;

	function QFetchArray&#40;$query&#41; &#123;
		return $this->FetchArray&#40;$this->Query&#40;$query&#41;&#41;;
	&#125;

	/**
	* returns the number of rows from a table based on a certain &#91;optional&#93;
	* where clause
	*
	* @param string $table			table in which to count rows
	* @param string $where_clause	optional where clause &#91;see sql WHERE clause&#93;
	*
	* @return int row count
	*
	* @access public
	*/
	function RowCount&#40;$table,$where_clause = ""&#41; &#123;
		$result = $this->FetchRow&#40;$this->Query&#40;"SELECT COUNT&#40;*&#41; FROM `$table` $where_clause;"&#41;&#41;;
		return $result&#91;0&#93;;
	&#125;

	/**
	* fetch an array w/ rows from the database
	*
	* @param resource $result	sql query result
	* @param int $return_type	row return type &#91;can be DB_RT_ARRAY or DB_RT_OBJECT&#93;
	* @param string $key		key the returned array by a certain row field &#91;defaults to ""&#93;
	*
	* @return array with rows or NULL if none fetched
	*
	* @access public
	*/
	function FetchRowArray&#40;$result,$return_type = DB_RT_ARRAY,$key = ""&#41; &#123;
		$ret_val = array&#40;&#41;;
		$i = 0;

		// dont panic. its just ternary operators in action &#58;&#93;
		while &#40;$row = &#40;&#40;$return_type == DB_RT_ARRAY&#41; ? $this->FetchArray&#40;$result&#41; &#58; $this->FetchObject&#40;$result&#41;&#41;&#41;
			$ret_val&#91;&#40;&#40;$key == ""&#41; ? $i++ &#58; &#40;&#40;$return_type == DB_RT_ARRAY&#41; ? $row&#91;"$key"&#93; &#58; $row->$key&#41;&#41;&#93; = $row;

		// see if any rows were fetched and return accordingly
		return &#40;count&#40;$ret_val&#41; != 0&#41; ? $ret_val &#58; NULL;
	&#125;

	/**
	* FetchRowArray wrapper
	*
	* @param string $query	sql query to send to FetchRowArray
	* @param int $return_type
	* @param string $key
	*
	* @return array
	*
	* @access public
	*
	* @see CDatabase&#58;&#58;FetchRowArray
	*/
	function QFetchRowArray&#40;$query,$return_type = DB_RT_ARRAY,$key = ""&#41; &#123;
		return $this->FetchRowArray&#40;$this->Query&#40;$query&#41;,$return_type,$key&#41;;
	&#125;

	/**
	* returns an array w/ the tables fields
	*
	* @param $table database table from which to get rows
	*
	* @return array
	*
	* @access public
	*/
	function GetTableFields&#40;$table&#41; &#123;
		$fields = $this->QFetchRowArray&#40;"SHOW FIELDS FROM `$table`"&#41;;
		$ret_val = array&#40;&#41;;

		foreach &#40;$fields as $field&#41;
			$ret_val&#91;&#93; = $field&#91;"Field"&#93;;

		return $ret_val;
	&#125;

	/**
	* fetches a row from a table based on a certain id using the SELECT SQL query
	*
	* @param string $table		table in which to perform select
	* @param int $id			row id to fetch
	* @param string $fields		comma separated list of row fields to fetch &#91;defaults to `*' all&#93;
	* @param int $return_type	row return type DB_RT_ARRAY|DB_RT_OBJECT &#91;defaults to DB_RT_ARRAY&#93;
	*
	* @return array w/ the fetched data or NULL if id not found
	*
	* @access public
	*/
	function QuerySelectByID&#40;$table,$id,$fields = "*",$return_type = DB_RT_ARRAY&#41; &#123;
		// build query
		$query = "SELECT $fields FROM `$table` WHERE `id` = '$id'";

		// fetch row
		return &#40;$return_type == DB_RT_ARRAY&#41; ? $this->QFetchArray&#40;$query&#41; &#58; $this->QFetchObject&#40;$query&#41;;
	&#125;

	/**
	* complex fetch row array w/ WHERE/LIMIT/ORDER SQL clauses and page modifier
	*
	* @param string $table			table to fetch rows from
	* @param string $fields			comma separated list of row fields to fetch
	* @param string $where_clause	SQL WHERE clause &#91;use empty to ignore&#93;
	* @param int $start				limit start
	* @param int $count				number of rows to fetch
	* @param bool $pm				page modifier. if set to TRUE &#91;default&#93; $start becomes the page
	* @param string $order_by		field&#91;s&#93; to order the result by &#91;defaults to void&#93;
	* @param string $order_dir		order direction. can be ASC or DESC &#91;defaults to ASC&#93;
	* @param int $return_type		row return type &#91;DB_RT_ARRAY&#40;default&#41;|DB_RT_OBJECT&#93;
	*
	* @return array w/ fetched rows or NULL
	*
	* @access public
	*/
	function QuerySelectLimit&#40;$table,$fields,$where_clause,$start,$count,$pm = TRUE,$order_by = "",$order_dir = "ASC",$return_type = DB_RT_ARRAY&#41; &#123;
		// check if $count is empty just to be safe
		$count = &#40;$count == ""&#41; ? 0 &#58; $count;

		// recompute $start if page modifier set
		$_start = &#40;$pm == TRUE&#41; ? &#40;&#40;&#40;$start == 0&#41; ? 1 &#58; $start&#41; * $count - $count&#41; &#58; $start;

		// setup order clause
		$order_clause = &#40;$order_by != ""&#41; ? "ORDER BY $order_by " . &#40;in_array&#40;$order_dir,array&#40;"ASC","DESC"&#41;&#41; ? "$order_dir " &#58; ""&#41; &#58; "";

		// setup where clause
		$where_clause = &#40;$where_clause != ""&#41; ? "WHERE $where_clause " &#58; "";

		// limit clause
		$limit_clause = &#40;$start >= 0&#41; ? "LIMIT $_start,$count" &#58; "";

		// build query
		$query = "SELECT $fields FROM `$table` &#123;$where_clause&#125;&#123;$order_clause&#125;&#123;$limit_clause&#125;";

		// fetch rows
		return $this->QFetchRowArray&#40;$query,$return_type&#41;;
	&#125;

	/**
	* builds and performes a SQL INSERT query based on the user data
	*
	* @param string $table	table in which to perform insert
	* @param array $fields	associative array w/ the row fields to be inserted
	*
	* @return void
	*
	* @access public
	*/
	function QueryInsert&#40;$table,$fields&#41; &#123;
		// first get the tables fields
		$table_fields = $this->GetTableFields&#40;$table&#41;;

		if &#40;count&#40;$fields&#41; == 0&#41; &#123;
			$names&#91;&#93; = "id";
			$values&#91;&#93; = "''";
		&#125; else
			// prepare field names and values
			foreach &#40;$fields as $field => $value&#41;
				// check for valid fields
				if &#40;in_array&#40;$field,$table_fields&#41;&#41; &#123;
					$names&#91;&#93; = "`$field`";
					$values&#91;&#93; = "'$value'";
				&#125;

		// build field names and values
			$names = implode&#40;",",$names&#41;;
		$values = implode&#40;",",$values&#41;;

		// perform query
		$this->Query&#40;"INSERT INTO `$table` &#40;$names&#41; VALUES&#40;$values&#41;"&#41;;

		return $this->InsertID&#40;&#41;;
	&#125;

	/**
	* builds and performs a SQL UPDATE query based on the user data
	*
	* @param string $table			table in which to perform update
	* @param array $fields			associative array w/ the fields to be updated
	* @param string $where_clause	update where clause &#91;see SQL WHERE clause&#93;
	*
	* @return void
	*
	* @access public
	*/
	function QueryUpdate&#40;$table,$fields,$where_clause&#41; &#123;
		if &#40;is_array&#40;$fields&#41;&#41; &#123;
			// first get the tables fields
			$table_fields = $this->GetTableFields&#40;$table&#41;;

			// prepare query
			foreach &#40;$fields as $field => $value&#41;
				// check for valid fields
				if &#40;in_array&#40;$field,$table_fields&#41;&#41;
					$pairs&#91;&#93; = "`$field` = '$value'";

			// build and perform query
			if &#40;is_array&#40;$pairs&#41;&#41;
				$this->Query&#40;"UPDATE `$table` SET " . implode&#40;", ",$pairs&#41; . " WHERE&#40;$where_clause&#41;"&#41;;
		&#125;
	&#125;

	/**
	* builds and performs a SQL UPDATE query based on the user data
	*
	* @param string $table	table in which to perform update
	* @param array $fields	associative array w/ the fields to be updated
	*
	* @return void
	*
	* @access public
	*/
	function QueryUpdateByID&#40;$table,$fields&#41; &#123;
		$id = $fields&#91;"id"&#93;;
		unset&#40;$fields&#91;"id"&#93;&#41;;

		$this->QueryUpdate&#40;$table,$fields,"`id` = '$id'"&#41;;
	&#125;
&#125;
?>
Έχετε κάποια ενδιαφέρουσα σκέψη ή πρόταση;

Άβαταρ μέλους
cordis
Administrator, [F|H]ounder, [C|S]EO
Δημοσιεύσεις: 27616
Εγγραφή: 09 Οκτ 1999 03:00
Τοποθεσία: Greece
Επικοινωνία:

Ελληνικά και MySQL: μια ιστορία αγάπης και μίσους

Δημοσίευση από cordis » 29 Δεκ 2005 10:24

τι charset έχει η φόρμα edit;

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7">
Δεν απαντάω σε προσωπικά μηνύματα με ερωτήσεις που καλύπτονται από τις ενότητες του forum. Για ο,τι άλλο είμαι εδώ για εσάς.
- follow me @twitter

kchrist
Δημοσιεύσεις: 8
Εγγραφή: 28 Δεκ 2005 01:29

Ελληνικά και MySQL: μια ιστορία αγάπης και μίσους

Δημοσίευση από kchrist » 29 Δεκ 2005 11:53

cordis έγραψε:τι charset έχει η φόρμα edit;

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7">
Τόσο η edit, όσο και οι υπόλοιπες φόρμες βασίζονται σε template system και έχουν:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7">

Απάντηση

Επιστροφή στο “MySQL”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες