Friday, December 5, 2008

How to find the table structure of a mysql table in php

You can use sql command describe to get the table structure of a mysql table.
It will return six information for each table attribute (column).
Field, Type, Null, Key, Default, Extra



<?php

//include your DB connection

$query = "describe your_table_name";
$result = mysql_query($query);

//loop through each table column attribute
while($data = mysql_fetch_array($result))
{
echo $data['Field'].', '; //field name
echo $data['Type'].', ';//feild type
echo $data['Null'].', ';// null or not
echo $data['Key'].', ';//primary key or not
echo $data['Default'].', ';//default value
echo $data['Extra'];// like auto increment
echo '<br />';
}

?>


Get custom programming done at GetAFreelancer.com!

Wednesday, December 3, 2008

How to check if a given value is numeric (integer) using php

Following code will show you how to validate a variable to see if its a numeric integetr or a string. you can use is_numeric() and is_int() functions to validate this. Use
is_numeric() when validating form data, which will always be strings.

is_int() will check if the variable type is integer. so form data will always be false.

is_numeric() will check if the variable contain all numbers, use this function to validate form data.


<?php

$num1 = 48;
$num2 = '48';
$num3 = 'hello';

if(is_int($num1))
{
echo 'This is integer';
}
else
{
echo 'Not integer';
}

//check for above other $num2 and $num3 variables.

/*
USE is_numeric() IF YOU ARE VALIDATING FORM DATA
INSTEAD OF is_int()

*/

?>


Get custom programming done at GetAFreelancer.com!

Tuesday, December 2, 2008

What is the difference between mysql_fetch_array() and mysql_fetch_assoc() - which is faster?

Lets see an example.

Lets think we have a table with following fields,
id
name
age

Now when you use mysql_fetch_array() it will return both numeric array and an associative array.


while($data = mysql_fetch_array($result))
{
//will return

$id = $data[0];
$id = $data['id'];

$name = $data[1];
$name = $data['name'];

$age = $data[2];
$age = $data['age'];

}


when you use mysql_fetch_assoc() it will return only an associative array.


while($data = mysql_fetch_assoc($result))
{
// will return

$id = $data['id'];
$name = $data['name'];
$age = $data['age'];

}



So mysql_fetch_assoc() is comparatively faster because its only return a associative array.

Get Free Sinhala IT Learning Videos Kuppiya.com


Get custom programming done at GetAFreelancer.com!

Monday, December 1, 2008

Get content in 2 fields with the same field name in two mysql tables using php

Following code will help you understand how to get values from 2 mysql tables where there are same field names in each table.
This problem usually occurs when you join two tables together.

Example: lets take two tables

Table1
id
s_name
age


Table2
id
p_name
age


In above two tables there is a id field in both.

$query = "select * from Table1, Table2"

The thing in here is not to use mysql_fetch_assoc(), use mysql_fetch_array()

$result = mysql_query($query);
while($data = mysql_fetch_array($result))
{
when you loop through to get the values just remember it will be formatted like following.

}

Table1 id => $data[0]
Table1 s_name = > $data[1]
Table1 age => $data[2]

Table2 id = > $data[3]
Table2 p_name => $data[4]
Table2 age = > $data[5]


Get Free Sinhala IT Learning Videos Kuppiya.com


Get custom programming done at GetAFreelancer.com!

Sunday, November 30, 2008

undefined method 'scaffold' - Ruby on Rails error

When I first got the error undefined method 'scaffold' I was so frustrated because I was new to Ruby on Rails and was following a tutorial. After some search on the net I found out that method scaffold is no longer supported in Rails 2.0 and above.
Tutorials out there in the net really need to be updated because newcomers are stuck when they get errors in the tutorial it self.

Get Free Sinhala IT Learning Videos Kuppiya.com


Get custom programming done at GetAFreelancer.com!

Saturday, November 29, 2008

InstantRails 2.0 - Default Database is SQLite3, How to change into mysql

InstantRails 2.0 or higher versions comes with the SQLite3 as the default database,
So a command like this rails my_app will take SQLite3 as the default database for the Rails application.

To use mysql try the following command:
rails -d mysql my_app

extra parameter -d will define database

Get Free Sinhala IT Learning Videos Kuppiya.com


Get custom programming done at GetAFreelancer.com!

Friday, November 28, 2008

Ruby on Rails quick guide with InstantRails

Lets get started quickly on Ruby on Rails basics with InstantRails, First we'll see how to add a new Rails application using InstantRails ruby console.

1) Start up your InstantRails.

2) Right Click the InstantRails system tray icon and goto Rails Applications -> Open Ruby Console Window

3)you will be in rails_apps folder(default) in the command line.

4)type rails my_new_site on the command line

This will create a new my_new_site rails application inside the rails_apps folder, all the files needed will be created for you as well, just navigate to your rails_apps folder and view newly created my_new_site folder.



5) now lets start our web server to run our newly created RoR application using the command line

6) type cd my_new_site (change directory to my_new_site)

7) type ruby script\server
Now your web server is started and you can run your new application.




8) open a browser and type http://localhost:3000/ in the navigation bar.



To learn how to install InstantRails click here

Get custom programming done at GetAFreelancer.com!

Thursday, November 27, 2008

How to install Ruby on Rails - Instant Rails - All in one Package

If you are going to learn Ruby on Rails (RoR), Instant rails will make your RoR installation much more easier, like WAMP Server for php, Instant Rails is all in one package containing Ruby, Rails, Apache, and MySQL. All the things have been configured for you that you only need download and unzip the Instant Rail folder to your C: drive.

1) Download Instant Rails from here Download the latest zip file (InstantRails-2.0-win.zip when I write this)

2)Unzip the Instant Rail into C:\InstantRails

3)Double click the InstantRails.exe (Icon with big red I)

4)Now InstantRails system tray icon will run on you Taskbar

5)To see if all went well, Right Click the InstantRails system tray icon and goto Rails Applications -> Open Ruby Console Window



6)Type ruby --version in the opened command line window, then you will get the installed ruby version.

OK that's it. In my next blog post I will cover some basics when working with InstantRails for your RoR development.

Get Free Sinhala IT Learning Videos Kuppiya.com


Get custom programming done at GetAFreelancer.com!