Monday, October 29, 2007

Connecting to a mySQL database using PHP

SHOW available tables in mySQL database

Similar to opening a file to write to it, you have to open a connection to mySQL before you can do anything. The syntax of this function is as follows:

mysql_connect("localhost OR hostname:port", "httpd OR username", "" or "password");

$mysql_access = mysql_connect("localhost", "username", "password");

The function mysql_close is unnecessary unless you are setting a persistent connection. To set a persistent mySQL connection use:

mysql_pconnect("localhost", "username", "password");

You would want to use persistent connections where you would have a lot of simulataneous connections from the same user through the script.

The following script below will open your mySQL database connection and show the tables available to you in your mySQL database. You must always open a connection to the mySQL database before doing anything else (you only need to open it at the beginning of a script, and then you can run multiple queries if you want). This is good for testing your ability to successfully access the mySQL database.

$mysql_access = mysql_connect("localhost", $user, $pw);
mysql_select_db($db, $mysql_access);

$result = mysql_query("SHOW tables", $mysql_access);
while($row = mysql_fetch_row($result))

in above script

$user is a mysql username.

$pw is mysql password.

$db is mysql database name

Friday, October 26, 2007

php programme first php script

The Most Basic Computer Program
So how do I do it in PHP? Well the two simplest ways are using Print or Echo.

PHP5 and mysql and apache in one pack-WAMP Server installation on windows

PHP5 and mysql and apache in one pack-WAMP Server installation on windows

You can install Apache Server, MySQL, and PHP in one step using WAMP. WAMP is a freeware package that bundles Apache, MySQL, and PHP into one executable. You can download WAMP from the project homepage. Once you download WAMP, double click the icon and begin the install process.
Click Next to begin:After agreeing to the WAMP license, select the destination location. Leave the default location as "c:\wamp" and click Next:Leave the default Start Menu shortcut as "WampServer" and click Next:Select automatically launch WAMP5 on startup. This will allow Vista to act as a server whenever it is started. Select the check box and click Next:WAMP will summarize your selections. Click Install and WAMP will begin the install process:WAMP will extract and install itself. The process should only take a few seconds, and WAMP will prompt you to choose a folder for your "DocumentRoot." Leave the default folder as "www" and click Ok:WAMP will prompt you to enter the SMTP server to be used by PHP to send emails. Leave the default value as "localhost" and click Next:WAMP will then prompt you to enter the default email address to be used by PHP to send emails. Put your email address in this field and click Next:If you have Firefox installed, WAMP will ask you if you would like to use Firefox as the default browser with WAMP. This is a personal preference, so feel free to choose "Yes" or "No." I will choose "Yes" and then click Next:You will likely be prompted by Vista whether the Windows Firewall should allow or block the features of WAMP. You want to allow all of the features of WAMP, so click Unblock:Congratulations, the installation process is complete, click Finish and Launch WAMP5 now:Open a browser and enter "localhost" as the URL, and you will see a summary of the installation process:now go to browser… and type in the address bar…..


and press inter..default wamp server page is open……
now u go to c:/wamp/www/myfolder

myfolder is ur web folder where u put on ur .php pages.

And now run ur php page like this…

Go to Browser address bar

http://localhost/myfolder/ index.php

now happy and enjoy… read the other post from my blog and enjoy…and expert in the php script…..

php on IIS 6 intallation

PHP is a really cool server side scripting interface with loads of options and modules and is relatively easier than ASP (the only other server side scripting language I've used). IIS 6 is also a cool web server with loads of new options. So here is how you configure php on IIS:(I'm assuming IIS is already installed on your box)
1. Download php from this location2. Unzip the package to some location(c:\php or whatever)3. Copy the php.ini-recommended file to your %systemroot% directory(usually C:\Windows) or to the %PHPRC% location, if %PHPRC% variable is set.4. Rename the file as php.ini5. Edit the file to make your config. I usually do the following(only minimal stuff):a. Open short tags ("" should work for me, I'm too lazy)b. Edit my extension_dir to the right location("c:\php\ext\" or whatever)** DO NOT FORGET TO PUT THE TRAILING BACKSLASH.**c. Enable my php_ldap etc...d. other stuff...error handling, session mgmt etc etc....please read the docs. :-)6. Open your IIS snap-in (inetmgr in "Run")7. Create your website/virtual directory.8. Enable php on the entire website or your virtual directory:a. Go to the directory tab, click configuration, click Add.b. Put in ".php" (without the quotes) in the extension box.c. Point the Executable file to "php5isapi.dll" (in your php directory)d. Give your verbs(usually GET,HEAD,POST,TRACE would do)e. Ensure "Check that file exists" is checked. (saves time and resources)9. Only for IIS 6:a. Click on "Web Service Extensions".b. Click "Add new web service extension"c. Give some name and add the php5isapi.dll file and select allow.10. Add the php directory to your path(sysdm.cpl in "Run",Environment Variables under the Advanced tab, under "System Variables", edit the Path variable and append the path to your php directory to it(like C:\php where C:\php is the location of your php source).11. Restart IIS and enjoy.
Update(thanks to 'anonymous' :-): One important point I forgot to mention - You need to give the user IIS is running as (usually IUSR_MACHINENAME) permissions on your php directory and files, other wise you will end up with 403 or 404 errors.
I've seen on a lot of posts where people mention that they need to copy some files to system32 directory, and I do not think that its required. Here is a filemon snapshot of the location my IIS is finding the extensions:

Each time you make any changes to the php.ini file, you need to restart your webserver.
UPDATE: If you get a prompt for username and password in Firefox even though you enabled just anonymous access, you need to enable the following:a. In your Firefox window,type in 'about:config' without the quotesb. You will find a text box called Filter, type in 'auth' without the quotesc. You'll find an entry called network-automatic-ntlm-auth.trusted-urisd. Double-click on it. It will open a small input window.e. Enter the name of the website (usually if you're having issues with http://localhost), enter localhost and click OK. (You can enter multiple sites by separating them with commas)

how to start php coding and setup and installation,

Install an Apache server on a Windows or Linux machine
Install PHP on a Windows or Linux machine
Install MySQL on a Windows or Linux machine


Why PHP?

PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP is FREE to download from the official PHP resource:
PHP is easy to learn and runs efficiently on the server side

PHP scripting----------

PHP is now officially known as “PHP: HyperText Preprocessor”. It is a server-side scripting language usually written in an HTML context. Unlike an ordinary HTML page, a PHP script is not sent directly to a client by the server; instead, it is parsed by the PHP binary or module, which is server-side installed. HTML elements in the script are left alone, but PHP code is interpreted and executed. PHP code in a script can query databases, create images, read and write files, talk to remote servers - the possibilities are endless. The output from PHP code is combined with the HTML in the script and the result sent to the user?s web-browser, therefore it can never tell the user whether the web-server uses PHP or not, because all the browser sees is HTML.
PHP’s support for Apache and MySQL further increases its popularity. Apache is now the most-used web-server in the world, and PHP can be compiled as an Apache module. MySQL is a powerful free SQL database, and PHP provides a comprehensive set of functions for working with it. The combination of Apache, MySQL and PHP is all but unbeatable.
That doesn?t mean that PHP cannot work in other environments or with other tools. In fact, PHP supports an extensive list of databases and web-servers. The rise in popularity of PHP has coincided with a change of approach in web-publishing. While in the mid-1990s it was ok to build sites, even relatively large sites, with hundreds of individual hard-coded HTML pages, today?s webmasters are making the most of the power of databases to manage their content more effectively and to personalize their sites according to individual user preferences.

Reasons for using PHP

There are some indisputable great reasons to work with PHP. As an open source product, PHP is well supported by a talented production team and a committed user community. Furthermore, PHP can be run on all the major operating systems with most servers.
The speed of development is also important. Because PHP allows you to separate HTML code from scripted elements, you will notice a significant decrease in development time on many projects. In many instances, you will be able to separate the coding stage of a project from the design and build stages. Not only can this make life easier for you as a programmer, but it also can remove obstacles that stand in the way of effective and flexible design.
Well-maintained open source projects offer users additional benefits. You benefit from an accessible and committed community who offer a wealth of experience in the subject, as fast and as cheap as possible. Chances are that any problem you encounter in your coding can be answered swiftly and easily with a little research. If that fails, a question sent to a mailing list or forum can have an intelligent, authoritative response. You also can be sure that bugs will be addressed as they are found, and that new features will be made available as the need is defined. You will not have to wait for the next commercial release before taking advantage of improvements, and there is no hidden interest in a particular server product or operating system. You are free to make choices that suit your needs or those of your clients and incorporate whatever components you want.

Why MySQL?

Choosing a database system depends on three main factors; the platform on which you work, your finances and what you want to achieve. The reason I chose MySQL is because, I work mostly on the Linux system and MySQL is free for Linux. Also, I am an ardent supporter of Open Source Software movement and firmly believe that the combination of Linux, Apache, MySQL and PHP (LAMP) is hard to beat.

What is MySQL?

MySQL is a simple, yet powerful Open Source Software relational database management system that uses SQL.

MYSQL(pronounced "My ess cue el") is an open source Relational Database Management System that uses Structured Query Language. Information is stored in "Tables" which can be thought of as the equivalent of Excel spreadsheets. A single MySQL database can contain many tables at once and store thousands of individual records. It's fast, reliable and flexible.

What Is PHP?

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

The endless possibilities of the PHP scripting language and a great community of users has made it one of the most popular open-source languages. For all you people living outside the UNIX world, Open Source means it doesn?t cost anything. You can use it as much as you want and where you want, and nobody will ever charge you thousands of dollars for licenses and support. Even though it was originally conceived as a set of macros to help coders maintain personal home pages, its name grew a lot more from its purpose. Since then, PHP?s capabilities have been extended, taking it beyond a set of utilities to a full-featured programming language, capable of managing huge database-driven online environments.

PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
PHP is an open source software (OSS)
PHP is free to download and use

A PHP file may contain text, HTML tags and scripts. Scripts in a PHP file are executed on the server.

That's a mouthful, but if we break the definition down into smaller pieces, it is easier to understand.
server-side: This means that PHP scripts execute on the Web server, not within the browser on your local machine.
cross-platform: Cross-platform means that PHP scripts can run on many different operating systems and Web servers. PHP is available for the two most popular Web server configurations (IIS running on Windows NT and Apache running on UNIX).
HTML embedded scripting language: This means that PHP statements and commands are actually embedded in your HTML documents. When the Web server sees the PHP statements in the Web page, the server executes the statements and sends the resulting output along with the rest of the HTML. PHP commands are parsed by the server much like Active Server Pages or Cold Fusion tags.