Thursday, November 13, 2008

PHP Source Code: How to search in database

//connect to MySQL
$connect = mysql_connect(localhost,"root","password")
or die ("Hey loser, check your server connection.");
//make sure we’re using the right database
mysql_select_db("your schema") or die(mysql_error());
$s=$_POST['t1'];
$q1="select column_name from table_name where br like '$s%' ";
$query=mysql_query($q1);
$num=mysql_numrows($query);
$i=0;
while($i<$num)
{
$rst=mysql_result($query,$i,"br");
echo $rst;
echo "
";
$i++;
}
?>

Sunday, November 9, 2008

PHP Tutorial: some basic example for SQL and PHP

//$link = mysql_connect(“localhost”, “root”, “123”)
or die(“Could not connect: “ . mysql_error());
// for connecting with MySQL NB: you shoul use your own password and username
//mysql_select_db(‘moviesite’, $link)
or die ( mysql_error());
//for selecting the schema in database here the selected schema is moviesite
//$peoplesql = “SELECT * FROM people”;
// select the table in schema
//$result = mysql_query($peoplesql)

or die(“Invalid query: “ . mysql_error());
// to query

Saturday, November 8, 2008

PHP Tutorial: how PHP Fits with MySQL

With the onset of PHP5, you need to take a few extra steps to convince PHP and MySQL to play well
with each other. Before your MySQL functions will be recognizable by PHP, make sure to enable MySQL
in your php.ini file, which we covered in Chapter 1.
You can use MySQL commands within PHP code almost as seamlessly as you do with HTML.
Numerous PHP functions work specifically with MySQL to make your life easier; you can find a comprehensive
list in Appendix C.
Some of the more commonly used functions are:
❑ mysql_connect ("hostname", "user", "pass"): Connects to the MySQL server.
❑ mysql_select_db("database name"): Equivalent to the MySQL command USE; makes the
selected database the active one.
❑ mysql_query("query"): Used to send any type of MySQL command to the server.
❑ mysql_fetch_rows("results variable from query"): Used to return a row of the entire
results of a database query.
❑ mysql_fetch_array("results variable from query"): Used to return several rows of
the entire results of a database query.
❑ mysql_error(): Shows the error message that has been returned directly from the MySQL server

PHP Tutorial: how to show a welcome message in your page

Try it out:

Welcome to my site


echo "welcome to my site";
echo “Today is “;
echo date(“F d”);
echo “, “;
echo date(“Y”);
?>

PHP Tutorial: Passing variable through cookies

Cookies are tiny bits of information stored on your Web site visitor’s computer. There appears to be
some sort of paranoia about using cookies, so many people choose to disable this feature in their Web
browsers. In theory, cookies can be intercepted to gain information such as a person’s IP address and
operating system, but cookies are primarily used for storing information only. A few ad campaigns have
developed technology to use cookies to track your browsing habits, and many people see this as an invasion
of privacy. Also, because cookies are stored in a commonly named directory, anyone with access to
someone else’s computer (either via a hack or physical location) can potentially open cookie files and glean information about the owner. Because of these possibilities it’s not a good idea to store any potentially
private information on a computer.

So why do developers use cookies, anyway? The advantage to storing information in a cookie versus a
session is longevity. Sessions alone can’t store information for more than the length of time the browser
window is open. Like the elusive and mean-spirited video game that loses all high scores once it’s
unplugged, once a browser closes, all session information is lost. Cookies, on the other hand, can live on
a person’s computer until the developer has decided it’s been long enough and they automatically “die.”
It is because of this longevity that cookies are fabulous for storing information such as a visitor’s username
or language preferences. These are the pieces of information that users won’t have to retype every
time they visit your site, but if for some reason someone did get wind of the information, it wouldn’t be
the end of the world.
We mentioned earlier that sessions alone can’t store information for very long. However, you can alter
this limitation if you use sessions in conjunction with cookies. If your sessions are passing variables
using cookies, you can set the life of these cookies to longer than the life of the browser using the
session.cookie_lifetime configuration in your php.ini file. Keep in mind, however, that not only
will the session information be stored on the person’s computer, but the session ID also will be stored,
and that can cause you problems later on.
To set a cookie, you use the appropriately named setcookie() function. When setting a cookie, you can
determine that the following information be set along with it:
❑ Cookie name (this is mandatory).
❑ Value of the cookie (such as the person’s username).
❑ Time in seconds when the cookie will expire. (This time is based on a Unix timestamp, but you
can set it using the syntax time()+60*60*24*365, which keeps the cookie alive for a year. This
is optional, but if it is not set, the cookie will expire when the browser is closed.)
❑ Path (the directory where the cookie will be saved—the default is usually sufficient; this is
optional).
❑ Domain (domains that may access this cookie—this is optional).
❑ Whether a cookie must have a secure connection to be set (defaults to 0; to enable this feature
set this to 1).
You make each of these settings as follows:
setcookie(‘cookiename’, ‘value’, ‘expiration time’, ‘path’, ‘domain’,
‘secure connection’);
As you can probably guess by now, those values will be referenced in the script as So why do developers use cookies, anyway? The advantage to storing information in a cookie versus a
session is longevity. Sessions alone can’t store information for more than the length of time the browser
window is open. Like the elusive and mean-spirited video game that loses all high scores once it’s
unplugged, once a browser closes, all session information is lost. Cookies, on the other hand, can live on
a person’s computer until the developer has decided it’s been long enough and they automatically “die.”
It is because of this longevity that cookies are fabulous for storing information such as a visitor’s username
or language preferences. These are the pieces of information that users won’t have to retype every
time they visit your site, but if for some reason someone did get wind of the information, it wouldn’t be
the end of the world.
We mentioned earlier that sessions alone can’t store information for very long. However, you can alter
this limitation if you use sessions in conjunction with cookies. If your sessions are passing variables
using cookies, you can set the life of these cookies to longer than the life of the browser using the
session.cookie_lifetime configuration in your php.ini file. Keep in mind, however, that not only
will the session information be stored on the person’s computer, but the session ID also will be stored,
and that can cause you problems later on.
To set a cookie, you use the appropriately named setcookie() function. When setting a cookie, you can
determine that the following information be set along with it:
❑ Cookie name (this is mandatory).
❑ Value of the cookie (such as the person’s username).
❑ Time in seconds when the cookie will expire. (This time is based on a Unix timestamp, but you
can set it using the syntax time()+60*60*24*365, which keeps the cookie alive for a year. This
is optional, but if it is not set, the cookie will expire when the browser is closed.)
❑ Path (the directory where the cookie will be saved—the default is usually sufficient; this is
optional).
❑ Domain (domains that may access this cookie—this is optional).
❑ Whether a cookie must have a secure connection to be set (defaults to 0; to enable this feature
set this to 1).
You make each of these settings as follows:
setcookie(‘cookiename’, ‘value’, ‘expiration time’, ‘path’, ‘domain’,
‘secure connection’);
As you can probably guess by now, those values will be referenced in the script as $_COOKIE[‘cookiename’]

PHP Tutorial: Passing variable through session

Passing Variables with Sessions

Asession is basically a temporary set of variables that exists only until the browser has shut down (unless
you set this up differently in your php.ini file, which is another story altogether). Examples of session
information include a session ID and whether or not an authorized person has “logged in” to the site. This
information is stored temporarily for your PHP programs to refer back to whenever needed.
Every session is assigned a unique session ID, which keeps all the current information together. Your
session ID can either be passed through the URL or through the use of cookies. Although it is preferable
for security reasons to pass the session ID through a cookie so that it is hidden from the human eye, if
cookies are not enabled, the backup method is through the URL.
This setting is determined in your php.ini file. If you would like to force the user to pass variables
through cookies (instead of allowing a backup plan), you would set the following line in your file:
session.use_only_cookies = 1
To begin a session, use the function session_start(). Because we assume you have register_globals
set to “off,” you should not use the session_register() function you may have seen in other PHP
scripts. Make sure before using sessions that your php.ini file has been modified to show a valid path
in the session.save_path variable, as described in Chapter 1.
First, you need to decide what information will be stored in your session. Anything that has been stored
in a database can be retrieved and stored temporarily along with your session information. Usually, it is
information such as username and login information, but it can also be preferences that have been set at
some point by the user. An SID (session ID) will also be stored in the session array of variables.

Monday, November 3, 2008

PHP tutorial: making a simple hit counter

try out this:
//hit_counter01, php
$counter,_file = "./count.dat";
if(!($fp = fopen($counter_file, "r"))) die ("Cannot open $counter_file.");
$counter = (int) fread($fp, 20);
fclose($fp);

$counter++;

echo "You're visitor No. $counter.";

$fp = fopen ($counter_file, "w");
fwrite ($fp, $counter);
fclose($fp);
?>

NB:at first you must create a file named "count.dat" in your working directory