Sciphr

Apr 5

MySQL Quicklaunch Trick

You can launch and log into mysql with a very simple batch file!

The Trick
Not a trick really, just a couple of simple DOS Commands placed in a batch file and a little shortcut to that file. Basically, the batch file will contain the commands that will instruct the operating system to run mysql and to use the predefine credentials to log in. Along the way, we will also modify the working directory, title and maybe even the color scheme: )

Let’s Do It
Open notepad or your text editor of choice and type the following command(s):


MYSQL -uusername -ppassword

That’s really all you need to launch and log into mysql but we can do better:

CD \
TITLE My Title
COLOR 0D
CLS
MYSQL -uusername -ppassword

The first command CD sets the working directory to \ the root
The second command TITLE assigns desired text to the console’s title bar.
The third command COLOR sets the background and foreground color.
The fourth command CLS clears any commands on the console windows.
The most important command MYSQL takes two parameters -u and -p

What Next?
Now that you know about the commands, you can edit them to your specifications. The most important ones are the username and password parameters.
Now just go ahead and save the file as mysql.bat and place it on the desktop or any other folder you desire as long as you remember where you saved it: )

You can now browse to the batch file and click or double click to run it.

Not Fast Enough!
You’re right, we need to create a shortcut to the batch file or place it on the desktop so we can run it as quickly as possible. You can also add the batch file or shortcut to your start menu and your quick launch bar!

More Help!
If you’re not sure what your mysql usename is, odds are that it’s root

Color attributes are specified by TWO hex digits.
The first corresponds to the background and the second corresponds to the foreground. Each digit can be any of the following values:
    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White


If you have any questions regarding this article, feel free to reach out on twitter
~ Kode @Sciphr


Objects can be tricky

Object oriented programming has gained popularity and is now being used by many beginning programmers and developers. However, a pretty good understanding of the underlying workings of objects is often ignored or misunderstood.

PHP pseudo example:
Suppose you want to create an database abstraction class and you want a method that will take care of creating an instance of mysqli class, connect to the dbms, select a database and then assign the instance to a class property to use in other methods.


class mydb;
property dbc;
method open :
  dbc = new mysqli : DBHOST, DBUSER, DBPASS;
  dbc connect errno ? throw error | dbc select db : DBNAME;
  dbc errno ? throw error | return true;
;

The previous example is used even in some of the most popular frameworks, libraries and templating engines. The problems is that dbc will only be created if a mysqli class is instantiated successfully which in turn will only happen if DBHOST, DBUSER, DBPASS are correct. So, when we check if there was any connection errors:
dbc connect errno ? throw error | dbc select db : DBNAME;
We say that if there is a connection error number other than 0, let’s show some kind of predefined error message otherwise, attempt to select a database. However, if the connection fails PHP won’t show our predefined error but a built-in fatal error because dbc will not be accessible. So, a much better way to do would be:
class mydb;
property dbc;
method open :
  dbc = new mysqli : DBHOST, DBUSER, DBPASS;
  mysqli connect errno ? throw error | dbc select db : DBNAME;
  dbc errno ? throw error | return true;
;

By using the procedural function available through mysqli to check for connection errors, we can check for errors even if we can’t access the property through the dbc object, which is far more intuitive in my opinion!!

If you have any questions regarding this article, feel free to reach out on twitter
~ Kode @Sciphr


Sciphr now on tumblr: )

My goals is to share design, programming, development and technology related tips, tricks and resources. ~ Kode @Sciphr


Page 1 of 1