Create a random alphanumeric string with mysql query

Why we need this?

We used to generate random strings for generating passwords or generating keys which are used for accessing sensitive information through a URL. When you want to generate a unique random key for a table, it is always better to generate the key in mysql rather than the server side scripting language. Generating random key in mysql procedure, avoid multiple key generation and checks the existence of generating a key, which reduces multiple mysql connection calls.

MySQL Custom Function to generate alphanumeric random string

Use the below code to create a custom function which generates a random key:

Note: The maximum length of random string that can be generated by the above function is 100. You can increase the limit by changing the function return type.

Using custom function in a procedure:

Implement the created custom function generate_random  :

 

How to add new action link column for Yii CGridView

It is very simple to create custom columns in CGridView. While defining the columns, you have to define an array with specific values for the custom column. To create a column which points to another controller with ID parameter, use following code:

To add header for the column, modify the same array with an additional parameter for the header.

The entire widget for CGridView in view file will look like this:

 

CodeIgniter Form Validation – How to get all error fields as an array

CodeIgniter by default is not providing you the error fields for a form as an array. To add this feature to your codeigniter form_valiation library, you have to extend the system library and overload as form_validation library.

Create the file: application/libraries/My_Form_validation.php file and add the following code:

In your controller use the following code to get your error fields as an array:

 

Tips to change your MySQL Procedure through command shell

Drop your procedure before modifications

There is no alter command like table alter in mysql for modifying your MySQL procedure code. So you have to drop your procedure and then create the same procedure for altering your procedure. To drop the procedure, execute the following line of code in your command shell:

Note: Mysql ALTER PROCEDURE can be used to change the characteristics of a stored procedure only, not the parameters or body of the procedure.

Change the delimiter

Normally body of the procedure consists of semicolons (;) for the end of the line (statement). To execute the procedure as a single statement, you have to change the delimiter for the console (command line interface). You can change the delimiter to ‘/’ by executing the code below:

Execute the CREATE PROCEDURE code

Now you can type/copy your procedure code. An example procedure code is given below for your reference:

Note the last line of the procedure code. Your procedure will get executed only when you add the changed delimiter  / . If there are no issues with your code, your procedure will add successfully to the server.

Change to delimiter back to normal

You already added the procedure to the database, but make sure that you changed your delimiter back to the normal. Execute the following code for changing the delimiter to the default semicolon.

 

How to make Yii form fields mandatory manually

When you generate the model and CRUD files, usually all rules in the model is generated automatically and you are not bothered about the rules. But if you want to manually make fields in a form mandatory, you have to write a simple rule in the model rules function – array. Suppose I am having username, email and password in my form and I want all fields to be mandatory. The rules function will be similar to the below code:

Note the last array in the rule, where the first element has all fields (separated by comma) in the form which are required. The second element in the array is the validation class, which is “required” in our case.

Change your Yii view directory name for your controller

This article will assist you to use custom folder names rather than generating folder names for your controller. Suppose you have a model named: “MySample“, your generated controller name will be: “MySampleController”; your view folder name will be “views/mySample“. If you want to change your view name from views/mySample to views/sample, add the below function to your controller and change the folder name.

As simple as that 🙂

Run your admin panel website under port 9000 on the apache web server

It is very simple to run your admin panel website or your projects in different ports with Apache web server. The simple instruction to do so is given below.

Open your httpd.conf file and add the following lines of code at the end of the line:

Restart your server. Access your application at: http://localhost:9000

For other ports, simply replicate the code in your httpd.conf file and execute your different projects in different ports.