Posted on Sep 19 2008 by gi Filed under: dirname, script, shell, which, PHP, MySQL, Server side
When a shell script is executed, the default directory would normally be the current directory of the user when the script is called. If you get into a situation wherein you want to determine the location of the shell script when it was called by the user, you can do the following within the shell script:
fullpath=`which $0`
scriptdir=`dirname ${fullpath}`
The scriptdir variable would contain the directory where the running script is stored. The which program returns the full path of the called script (e.g. /usr/bin/mybashscript.sh) while the dirname program returns the directory portion of the argument which was passed to dirname.
One use for it would be if for example you need to know where the shell script was stored because you need to call another script that was also stored in that directory when it was installed.
Posted on Aug 27 2008 by gi Filed under: Bash, database, empty, MySQL, PHP, MySQL, Server side
I wrote a bash shell script that deletes all the tables in a mysql database and I thought I’d just post it here in case anyone might find it useful. It accepts one required argument which is the database name and one optional argument which is the username with DROP/CREATE DATABASE privileges to dbname .
#!/bin/bash
if [[ ${1} == '' ]]
then
echo “”
echo ” `basename ${0}` {dbname} [{user}]”
echo “”
echo ” {dbname} – is the name of the mysql database to zap”
echo ” {user} – optional, user ‘root’ is used as default”
echo “”
exit 1;
else
read -p “Are you sure you want to empty ${1} [Y/N]? ” ans
if [[ ${ans} == "y" || ${ans} == "Y" ]]
then
user=${2:-”root”}
echo -n “Using mysql user ‘${user}’, ”
mysql -u ${user} -p -e “drop database ${1};create database ${1};” && echo “${1} emptied.”
exit 0;
else
echo “Aborted.”
exit 1;
fi
fi
Note: Oh and be careful in using the script ‘coz it will erase all the data in your database so make sure you have backups.
Posted on Aug 27 2008 by gi Filed under: database, empty, MySQL, PHP, MySQL, Server side
This morning I was doing some tests and I found myself having to use a web-based mysql tool called phpmyadmin to delete all the tables in a mysql database.
Since I was to do it several times, I decided to use the command line interface program mysql. I was kinda surprise that I didn’t find any single command to drop all the tables, anyway deleting all the tables can be done by issuing the following command:
sh$ mysql -u username -p -e “drop database dbname; create database dbname;”
You will need to specify a username with drop/create privileges for the dbname database. It will then prompt for the user’s password when you hit enter on the command line.
Posted on Aug 21 2008 by gi Filed under: C, PHP, Server side, PHP, MySQL, Server side
In an effort to improve private torrent trackers the Project Gazelle team has created a reliable, lightweight and secure codebase. It was designed from the ground up to be secure against SQL and XSS injection attacks and at the same time be able to handle thousands of users.
The source code, which is currently undergoing beta, is available via the Project Gazelle SVN. The source code comes with a C++ based tracker, PHP frontend and complete administration panel.
You can find more info at the Project Gazelle web site.
Posted on Jun 25 2008 by gi Filed under: call_user_func, lookup table, PHP, MySQL, Server side
I’ve don my share of coding in Assembly language and one of the constructs that is pretty efficient to use is lookup tables for calling subroutines or functions. One of the things I like PHP is its ability to do the same thing.
For example, if you want to perform exception/error handling based on a status code, you can use lookup tables instead of using a switch() construct. If you use a switch() construct, you’ll have something like:
switch( $status ) {
case 0: do_action_1(); break;
case 0: do_action_1(); break;
default: do_default();
}
If you use a lookup table, your source code will have something like:
$lookup = array( 'do_action_1','do_action_2');
if ( $status >= 0 && $status < count($lookup) ) {
call_user_func($lookup[$status]);
}
I’m sure there are other uses of lookup tables and the combination of using array() and call_user_func() in PHP offers a lot of flexibility in coding.
Posted on Jun 23 2008 by gi Filed under: browser detection, mobile, PHP, wordpress plugin, PHP, MySQL, Server side
With all the thrusts nowadays towards using mobile devices to surf the Net, it would be great if your WordPress blog is mobile device friendly. The concept is actually simple and you can incorporate a mobile friendly version of your blog by understanding some simple things.
The main thing that you need to know is that when visitors to your WordPress blog (actually any web site but let’s use WordPress for this example) browses your site, you can identify the type of computer or mobile device that they are using. Once you have identified the device, it’s easy to create a separate CSS stylesheet for your mobile visitor.
Continue reading …