Wednesday, December 17, 2008

How to find floor or ceiling value of a number using php

Following code will show you how to find floor and ceiling values of a floating number. To find floor you can use the php function floor() and ceil() to find ceiling value.


<?php

//examples of floor()
echo floor(7.2); // 7
echo '<br>';
echo floor(7.9); // 7

echo '<br><br>';

echo ceil(7.2); // 8
echo '<br>';
echo ceil(7.9); // 8

?>


Get custom programming done at GetAFreelancer.com!

Monday, December 15, 2008

How to find the power of a number (Exponential) using php

Following code will show you how to find the power of a number using php.
The function used to do this is pow() function.
pow(base, exponent)


<?php

//first parameter is the base
//second parameter is the exponent

echo pow(2, 2); // 4
echo pow(2, 3); // 8
echo pow(3, 2); // 9

?>



Get custom programming done at GetAFreelancer.com!

Saturday, December 13, 2008

How to list all tables in a mysql database using php

Following code will show you how to get all the table names inside a given mysql database. to do this you only need know about the mysql query "SHOW TABLES".
following example show you how to retrieve query the result using php.


<?php

//set your DB connection
mysql_connect('localhost', 'root', '');
//select your DB
mysql_select_db('test');

$query = "SHOW TABLES";
$result = mysql_query($query);

while($data = mysql_fetch_array($result))
{
echo $data[0];
echo '<br />';
}

?>


Get custom programming done at GetAFreelancer.com!

Thursday, December 11, 2008

How can I add html tags inside my php code

Can I add html tags inside my php code? Yes almost any html tag.
To use html tags inside php you can use echo to print them.
Look at the following code.


<html>
<head>
<title>Untitled Document</title>
</head>

<body>

<?php

echo '<strong>This is bold letters</strong>';

echo '<h1>This is heading letters</h1>';

echo '<div style="border: solid #000000;">
This is inside a div tag</div>';

?>

</body>
</html>


Like above example you can use any html tags.

Get custom programming done at GetAFreelancer.com!

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!