Tuesday, December 30, 2008

Why sometimes Mysql Update does not work with php mysql_affected_rows()

Sometimes when you write a mysql update query it does not work properly with mysql_affected_rows()
You try to look for a syntax error, try to get the error from mysql_error() yet no error pops up. you just cannot see whats wrong with your mysql update query.

From my experience this could happen because your new updating values are same as the old values, mysql will not update that row, hence mysql_affected_rows() will not work because no rows were affected.

www.php.net/mysql_affected_rows - look at return values

Get custom programming done at GetAFreelancer.com!

Friday, December 26, 2008

What is Cannot modify header information in php

Warning: Cannot modify header information - headers already sent by ......
This error is a shocker for a new php programmer. when you do some basic redirection and you bump into this error you are left clueless.

In the simplest form this happens when you output somthing into the screen before using the header() function.


<?php

echo 'hello and I doomed the header';

header("Location: login.php");

?>


above code will get that error because you have already output something to the screen.(If you are testing in your local machine this error is not shown)

This error is also generated by a unsuspecting white space. This could be a white space from a include file before the header() function.

Get custom programming done at GetAFreelancer.com!

Sunday, December 21, 2008

How to make a HTML input Text Box transparent

When you have a input text box on top of a image, you usually need to make the textbox transparent so that users can see the image right through the input text box.
This can be done by adding the following style code
style="background-color:transparent;"
This can be added to any input html control.

<input type="text" name="name" style="background-color:transparent;" />


Get custom programming done at GetAFreelancer.com!

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!