September 6, 2009
Ok, this is a frequent problem to everyone. When a program [a script], for example, Wordpress or Drupal, created a file / directory, the permission and group assigned is usually 99 99 Group. It's a known issue. If someone wants to remove a file or a directory with 99 99 Group permission, then only the host support has the permission to do so.
To send a ticket, depends on the kind of host provider you have, usually take a day to complete. I don't like waiting for someone to solve my problem. I did the request a couple times with my host supporter. The very last time I requested to delete the directory, I didn't specificity it enough and he deleted the wrong directory [my old blog directory].It wasn't his fault but it took a few days for me to actually work on the recovery.
I like to do things as quickly as possible if I get a chance. So I decided to search for a script or something to help me to break through this 99 99 Group limitation. Here is the solution I used:
http://drupal.org/node/231401
There the author provided 3 scripts:
[1] Change the permission to 777 in order to delete them [for optional removing !!!!]
1 2 3 4 5
| <?php
$old = umask(0000 );
chmod("name of folder or flile", 0777 );
umask($old);
?> |
(more...)
Filed under:PHP&MYSQL Author:John Wong
No Comments »
April 13, 2009
I was surprise to see this method, really. I never know there is such a "cool" method in php.
Creating Multi-Line Strings
You find yourself wanting to print several lines of HTML code at once. It can be
very tedious to use quotation marks to indicate such strings (especially because
HTML also often uses the quotation mark symbol). PHP provides a special quoting
mechanism, which is perfect for this type of situation. The following line begins
assigning a value to the $verse variable:
$verse = <<
The <<
the symbol HERE. You can use any phrase you wish, but I generally use the word
HERE because I think of the three less-than symbols as up to. In other words, you
can think of the following as meaning verse gets everything up to HERE.
$verse = <<
You can also think of <<
value HERE.
You can write any amount of text between <<
inside the special text and PHP replaces the variable with its value, just like
in ordinary (quoted) strings. The ending phrase (HERE) must be on a line by itself,
and there must be no leading spaces in front of it.
example code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <html>
<head>
<title>Row Your Boat</title>
</head>
<body>
<h1>Row Your Boat</h1>
<h3>Demonstrates use of long variables</h3>
<?
$verse = <<<here
Row, Row, Row, your boat<br />
Gently Down the stream<br />
Merrily, Merrily, Merrily, Merrily<br />
Life is but a dream!<br />
<br /><br />
HERE;
print “<h3>Verse 1:</h3>”;
print $verse;
print “<h3>Verse 2:</h3>”;
print $verse;
print “<h3>Verse 3:</h3>”;
print $verse;
?>
</body>
</html> |
Filed under:PHP&MYSQL Author:John Wong
No Comments »
March 23, 2009
I was coding my phpbb mod and I had troubled with getting the rank title from the database. I went to Star TrekGuide for help, and it turned out to be a great idea.
get rank title
According to what Erik suggested, the way it works is to fetch it out once we get the correct and value from the table.
The problem I had was didn't get any value in return, so my codes failed to get either the correct row, or didn't get anything out from the table.
The correct way is to do something like as the following
1 2 3
| $sql = 'SELECT *
FROM ' . RANKS_TABLE . '
WHERE rank_id = ' . $user->data['user_rank']; |
So I got the concept and went ahead coded the rest on my code.
The resulting code is this:
1 2 3 4 5 6 7 8
| $sql = 'SELECT rank_title
FROM ' . RANKS_TABLE . '
WHERE rank_id = ' . $user->data['user_rank'];
$result = $db->sql_query($sql);
$rank_title = $db->sql_fetchfield('rank_title');
$db->sql_freeresult($result);
'RANK_TITLE' => $rank_title; |
Another fact I learned is the problem with free the sql result, or close down the query.
I was tricked by the fact that, in phpBB, the developers do not immediately close down the request all the time. Sometime $db->sql_freeresult($result); is used right after the $sql selection request, but in most cases, in the page footer, one single $db->sql_freeresult($result); will be enough to close all the requests.
Of course, it will still be good to close it right after each $sql. But for a larger application like phpBB as a whole, it is very reasonable to close them down all at once (not always all, most of the time, yes).
Reference from Erik
The $db->sql_freeresult($result); is used always. I think that you are tricked by the fact that it isn't always called right after the query, sometimes this isn't possible as you might want to re-use the result later on or just don't want to worry about it.
Due to the way phpBB is setup you can (but shouldn't) leave the call out at all as long you call the garbage_collection() function somewhere (this is done in page_footer()) as this function calls the $db->sql_close() method. This method will run $db->sql_freeresult($result) on all open queries before it closes the database connection.
Good coding practices however makes you to call it yourself.
Filed under:PHP&MYSQL, phpBB Mods (abandoned) Author:John Wong
No Comments »
February 19, 2009
It's possible to convert anything today. Hmm, this is not a new thing. But one of the major problem is people get lazy to alter every database table themselves.
First of all, let say you want to convert from big5_chinese_ci to utf8_general_ci, that's pretty easy in theory.
But you need at least two software to help you to edit the sql file.
[1] Export your database via phpmyadmin, and download it as an sql file format.
[2] Get an editor like EmEditor, open the sql, edit it, by replacing all the charset with utf8, be careful, some of them need to be replace as utf8_general_ci, some just utf8. scroll down and replace them, and in the end save it as utf8
[3] now, install your application again, in UTF-8 version.
[4] drop all the tables
[5] finally, import the original sql file (which is now utf8 after saving it), into phpmyadmin
[6] you need a script to alter the table, save it, name it as convert.php, and upload it to your root
[7] edit the database information in the convert.php, just the database information
[8] run the convert.php on your browser, copy and paste the code it generates.
[9] go to phpmyadmin , select that database, and go to SQL, and copy and paste, press ok to run the action
[10] in theory, if you did the editing correct in step [2], once you run those SQL queries, they will fail because all the tables are already collated in utf8_general_ci format.
This is just a theory on how to convert from one collation encode to UTF8.
I will always suggest you do utf8_general_ci, because it takes less resources, and is better with mutli-lang, and it;s generally faster than utf8_bin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?php
// original script (v1.0) by/from: http://www.phpwact.org/php/i18n/utf-8/mysql
// improved/modified (v1.03) by Bogdan http://bogdan.org.ua/
// this script will output all queries needed to change all fields/tables to a different collation
// it is HIGHLY suggested you take a MySQL dump/backup prior to running any of the generated queries
// this code is provided AS IS and without any warranty
set_time_limit(0 );
// collation you want to change to:
$convert_to = 'utf8_general_ci';
// character set of new collation:
$character_set= 'utf8';
// DB login information - *modify before use*
$username = 'database-user-name';
$password = 'database-password';
$database = 'database-name';
$host = 'localhost';
//-- usually, there is nothing to modify below this line --//
// show TABLE alteration queries?
$show_alter_table = true;
// show FIELD alteration queries?
$show_alter_field = true;
mysql_connect($host, $username, $password);
mysql_select_db($database);
$rs_tables = mysql_query(" SHOW TABLES ") or die(mysql_error());
print '<pre>';
while ($row_tables = mysql_fetch_row($rs_tables)) {
$table = mysql_real_escape_string($row_tables[0 ]);
// Alter table collation
// ALTER TABLE `account` DEFAULT CHARACTER SET utf8
if ($show_alter_table)
echo("ALTER TABLE `$table` DEFAULT CHARACTER SET $character_set;\r\n");
$rs = mysql_query(" SHOW FULL FIELDS FROM `$table` ") or die(mysql_error());
while ( $row = mysql_fetch_assoc($rs) ) {
if ( $row['Collation'] == '' || $row['Collation'] == $convert_to )
continue;
// Is the field allowed to be null?
if ( $row['Null'] == 'YES' )
$nullable = ' NULL ';
else
$nullable = ' NOT NULL';
// Does the field default to null, a string, or nothing?
if ( $row['Default'] === NULL )
$default = " DEFAULT NULL";
elseif ( $row['Default'] != '' )
$default = " DEFAULT '".mysql_real_escape_string($row['Default'])."'";
else
$default = '';
// Alter field collation:
// ALTER TABLE `tab` CHANGE `fiel` `fiel` CHAR( 5 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
if ($show_alter_field) {
$field = mysql_real_escape_string($row['Field']);
echo "ALTER TABLE `$table` CHANGE `$field` `$field` $row[Type] CHARACTER SET $character_set COLLATE $convert_to $nullable $default; \r\n";
}
}
}
?> |
Filed under:PHP&MYSQL Author:John Wong
1 Comment »
February 17, 2009
It's just for fun, i will integrate a similar one into my hello world program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| // Let me trying to run another calculation program
// no more salary now
// let gets this people vs bandwidth
#include <iostream>
using namespace std;
// let us do a gloabl variables
int visitorNumber, visitBotNumber, bandwidth, dataSize, transferTime;
signed int dataBWR = 4;
// let us begin the main body of this program
int main()
{
// asks some questions
int x;
cout << "How many visitors so far?\n"<< endl;
cin >> x;
cin.get();
int y;
cout << "How many bot visitors?\n"<< endl;
cin >> y;
cin.get();
int z;
cout << "What is the total amount of data size being uploaded?\n"<< endl;
cin >> z;
if (z >10000)
bandwidth = (x + y) + (dataBWR * z);
else
bandwidth = (x + y) * (dataBWR);
cin.get();
cout << bandwidth;
cin.get();
return 0;
} |
Filed under:C/C++, College Notes, PHP&MYSQL, Ubuntu, Uncategorized Category, WordPress Mods, phpBB Mods (abandoned) Author:John Wong
2 Comments »