group by field1, field2
with rollup
Internet is vague. It has nothing left to be covered now. However, things get messed up sometimes and I forgot where I used to find the materials I had just searched for some days ago. So, I am now gathering all those into this blog. This is just my bookmark or my scrapbook or anything you wish to say. If you think these are helpful, you can take references, no any rights to be defined here. No styles and no colorful designs, just an area to jot down some scraps and all. Enjoy
DELIMITER $$ CREATE FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000)) RETURNS VARCHAR(1000) DETERMINISTIC BEGIN DECLARE temp VARCHAR(1000); DECLARE ch VARCHAR(1); DECLARE i INT; SET i = 1; SET temp = ''; IF original REGEXP pattern THEN loop_label: LOOP IF i>CHAR_LENGTH(original) THEN LEAVE loop_label; END IF; SET ch = SUBSTRING(original,i,1); IF NOT ch REGEXP pattern THEN SET temp = CONCAT(temp,ch); ELSE SET temp = CONCAT(temp,replacement); END IF; SET i=i+1; END LOOP; ELSE SET temp = original; END IF; RETURN temp; END$$ DELIMITER ;Note:
select regex_replace('[^a-zA-Z0-9\-]','','2my test3_text-to. check \\ my- sql (regular) ,expressions ._,');
$set = implode(',',$where);
$this->db->where('1 <=', "FIND_IN_SET(`".$field."`,".$this->db->escape($set).")",false);
Posted on 11.12.04
CREATE TABLE document (Note that MySQL uses a non-standard name "utf8" to mean UTF-8. The COLLATE command is used to tell how to sort the data when using the SORT BY command. Also note that you should always use VARCHAR instead of CHAR with UTF-8. (UTF-8 uses variable sized numbers for different characters. For instance, Latin letters use 1 byte codes, while Japanesee characters are 3 bytes. Using CHAR(10) would force the database to reserve 30 bytes, because it doesn't know ahead of time which length with be used, so it reserves the maximum.)
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
unicodeText VARCHAR(45) NOT NULL
)
CHARACTER SET utf8 COLLATE utf8_general_ci;
SET NAMES 'utf8';Using PHP and XHTML to encode
1 <?phpBecause we are using MySQL 4.1 with PHP 5 it makes sense to use the MySQLi extension. I like using the OO version of the extension. Line 2 is the connection to the database. Line 3 tells the database to expect UTF-8 data. Lines 4-6 update a row in the database with the Unicode data we send through the form. (Make sure to have some sample data in row 1 of the database when you test this code) Lines 7-8 pull the data back out of the database. Lines 11-12 are important. XHTML defaults to using UTF-8 encoding, unless you specifically tell it otherwise. Using the correct Doctype declaration will alert your browser to use UTF-8 encoding. You will see a lot of things if you search on Google about how you need to put tags about UTF-8 in all sorts of places, like the form, or the form controls, or in META tags. If you are serving valid XHTML this is not necessary. The rest of the code is the form. Lines 18 and 21 will let you compare the raw POST data with what the database returns.
2 $DB = new mysqli('localhost', 'user', 'root', 'dbname');
3 $DB->query("SET NAMES 'utf8'");
4 if (!empty($_POST['ta'])) {
5 $DB->query("UPDATE document SET unicodeText='{$_POST['ta']}' WHERE ID=1");
6 }
7 $result = $DB->query("SELECT unicodeText FROM document WHERE ID=1");
8 $return = $result->fetch_object();
9 $result->close();
10 ?>
11 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
12 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
13 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
14 <head>
15 <title>Test</title>
16 </head>
17 <body>
18 <p>Posted: <?php echo $_POST['ta'];?></p>
19 <form enctype="multipart/form-data" method="post" action="test2.php">
20 <fieldset>
21 <textarea name="ta"><?php echo $return->unicodeText;?></textarea>
22 <input type="submit" />
23 </fieldset>
24 </form>
25 </body>
26 </html>
(PHP 4, PHP 5, PECL mysql:1.0)
mysql_fetch_object — Fetch a result row as an object
Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
The result resource that is being evaluated. This result comes from a call to mysql_query().
The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.
An optional array of parameters to pass to the constructor for class_name objects.
Returns an object with string properties that correspond to the fetched row, or FALSE if there are no more rows.
mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
| Version | Description |
|---|---|
| 5.0.0 | Added the ability to return as a different object. |
Example 1428. mysql_fetch_object() example
mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}
mysql_free_result($result);
?>
Example 1429. mysql_fetch_object() example
$row = mysql_fetch_object($result);
/* this is valid */
echo $row->field;
/* this is invalid */
// echo $row->0;
?> Performance: Speed-wise, the function is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row() (the difference is insignificant).
Note: mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).
Note: Field names returned by this function are case-sensitive.
Note: This function sets NULL fields to PHP NULL value.
$sql = "SELECT Pic FROM Pictures WHERE ID=$id";
if(!($result=@mysql_query($sql))) showerror;
$data=@mysql_fetch_array($result);
if (!empty($data['Pic']))
{
//Change content type to your file type
header("Content-Type: image/gif");
print $data['Pic'] ;
}