Houdini

Important: Basics Of Using PHP And MySQL

Recommended Posts

I generally notice confusion with new users to PHP and or MySQL and first of all I believe that unlike HTML which is automatically associated with a IE browser in a Microsoft system. HTML is automatically rendered with whatever browser is the default browser, be it Internet Expolrer Firefox Netscape or any other browser that has been set.

 

PHP is a different matter to view the output of a PHP file it must be run on a webserver, and if you do not have one set up on your local PC it simply will not work. (Note serverside langauge requies a server) HTML is client side and requires only a browser so it will display by double or single clicing the file itself and the program associated with it will open it, but not so with PHP or ASP (Window servers only) or Perl. You must have a working webserver like IIS PWS Apache Solaris and so on, and the file must be accessed with a browser that sends a request for a page that resides on the server like one of the above mentioned.

 

All webservers have a ROOT where they keep files and other subdirectories. Normally you will have an index.html or index.php file (the server looks for these type files and serves them upon request of that file. With a PHP file the server will look up what you typed into to address bar and try to PARSE it (unlike HTML) to do whatever it is supposed to do, if PHP is not set up properly it will produce either errors or unexpected results or both. The webserver must know where the executable file is and this is set up in the servers sonfiguration file, then there are dlls that the PHP or other langauge will need to perform certain operations when asked to do so, if these are not set they will fail with errors.

 

If you are using Xisto as a server usually you do not need to worry about this, but if you have some ideas about creating something that requires PHP on you site it would be best to place a webserver on your home PC or MAC and develop these programs there and when debugged to the point where you are satisfied then upload them to Xisto.

 

I am a regular contributor to PHP Builder Forums and Dev Shed here and see these question all the time, I used to answer and try to help, but now it seems that installation of these services. My PAT answer anymore is to just download and install either XAMPP available here or you could use WAMP available here

 

This is not a tutorial but a post to try to help those that are new to PHP to get started in the right direction. While it is possible to install a server and MySQL and PHP on your own (with much frustration and questions) the programs I suggested will help you if all you really want to do is write PHP and MySQL routines to develop a site. This is just my opinion for newbies and I am sure that there will be those that tell me you should actually learn something from doing it on your own, but when I see people getting more and more confused trying to do this then I really wonder if the satisfaction of self installation is really that much a benefit unless that is your desired career.

Share this post


Link to post
Share on other sites

See thats where I think Im making my mistake. I have Xammp installed on machine, but the main reason I did it was so I could see PHP (so I thought). I have been doing all my coding straight on my Xisto site. Now I know that I should be doing it through Xammp first.

Share this post


Link to post
Share on other sites

Correct, you (or at least I) should develop your new code on your localmachine and when it is working the way you want it to work then upload the now hopefully bug free script up to your working site. Another thing I do with new scripts is have two folders on the internet site one with my real content and another (call it test or something like that) if the script does something wrong or worse (it wrecks your site) then you can correct it while your real site goes merrily along.

The only difference you would have is a config.php file, one for your local machine like below

<?php$host="localhost";//this is default$user="root";//this is a default for a new MySQL$pass="";//this is the default (no password for 'root')$connect=mysql_connect($host,$user,$pass)  or die("Could not connect to server, MySQL said: ".mysql_error());?>
and for your Xisto it would be something like
<?php$host="localhost":$user="yourastahostusername";$pass="yourastahostpassword":$connect=mysql_connect($host,$user,$pass)  or die("Could not connect to server, MySQL said: ".mysql_error());//good to check for errors?>

Share this post


Link to post
Share on other sites

Correct, you (or at least I) should develop your new code on your localmachine and when it is working the way you want it to work then upload the now hopefully bug free script up to your working site. Another thing I do with new scripts is have two folders on the internet site one with my real content and another (call it test or something like that) if the script does something wrong or worse (it wrecks your site) then you can correct it while your real site goes merrily along.
The only difference you would have is a config.php file, one for your local machine like below

<?php$host="localhost";//this is default$user="root";//this is a default for a new MySQL$pass="";//this is the default (no password for 'root')$connect=mysql_connect($host,$user,$pass)  or die("Could not connect to server, MySQL said: ".mysql_error());?>
and for your Xisto it would be something like
<?php$host="localhost":$user="yourastahostusername";$pass="yourastahostpassword":$connect=mysql_connect($host,$user,$pass)  or die("Could not connect to server, MySQL said: ".mysql_error());//good to check for errors?>
Excellent suggestions and congratulations for the first post, i think its very helpful for all newbies. A time ago i installed WAMP in my PC for testing and the results were great the only thing was it installs by default other software that i dont need and were very dificult to uninstall.

In my case i always like to install by hand PHP, MySql and APACHE in my win 2kpro SP4 machine to have absolute control over this software with great results, when i was a newbie i had a lot of troubles :-( that cost me a lot of time but this help me at the same time a lot.

BTW, the above code dont have any code to select a database to use, so, to complete it its necesary to include first a variable with the name of the database and this in both cases:
$database="databasename";my_sql_select_db($database) or die("Error... no database selected, MySQL said: ".mysql_error());

Best regards,

Share this post


Link to post
Share on other sites

There is a reason for that include not selecting a database, and it is because I test IPB and PHP-Nuke with phpbb2 and phpbb2 (standalone BBS) and other software. If I included a database in my include file then it would be useless because it would only connect to that particular database, in my case it makes no sense and if I can't just do

<?phpinclude("connect.php");$select=mysql_select_db("ipb",$connect)  or die("Could not delect database ipb MySQL said: ".mysql_error());//more code...?>
If the code were in the confines of one particular script then it might make sense but as a contributor to a couple of PHP and MySQL related websites, sometimes I need to create a database similar to the one that they have and to see what kind of problems they are having and be able to solve it I will create a database and then run their code after removing the obvious errors to either find the problem with their code or fix it by seeing a simple error.

Does that make sense to you?
Edited by Houdini

Share this post


Link to post
Share on other sites

There is a reason for that include not selecting a database, and it is because I test IPB and PHP-Nuke with phpbb2 and phpbb2 (standalone BBS) and other software. If I included a database in my include file then it would be useless because it would only connect to that particular database, in my case it makes no sense and if I can't just do

<?phpinclude("connect.php");$select=mysql_select_db("ipb",$connect)  or die("Could not delect database ipb MySQL said: ".mysql_error());//more code...?>
If the code were in the confines of one particular script then it might make sense but as a contributor to a couple of PHP and MySQL related websites, sometimes I need to create a database similar to the one that they have and to see what kind of problems they are having and be able to solve it I will create a database and then run their code after removing the obvious errors to either find the problem with their code or fix it by seeing a simple error.

Does that make sense to you?
Of course it make sense, and i think that a more general way to do the database selection is by using an universal function with the database name as a parameter.

best regards,

Share this post


Link to post
Share on other sites

Do you mean by generalized like the MySQLI function of

$connect=mysqli_connect($host,$username,$password,$database);
That is pretty general and available with newest (or newer versions of MySQL) of PHP 5 but it will not work when in fact instead of selecting test_anita datbase instead of whoknows database so writing such a connection include would not help me at all. It is not like I have to query just one database, it could be any number of them even if I just need to connect to an existing one or create another new one.
Edited by Houdini

Share this post


Link to post
Share on other sites

I have the problem when people mistaken HTML with PHP all the time, I am sitting on IRC channels like #phphelp, #mysql and etc. on freenode and my own country irc server. I ask for support and give my support if I have the time and can and a lot of (in my opinion stupid) people come and ask why php is better than html. I used to point them to the truth how the things works, but now I usually put a big grin or a smile, sometimes ban them if they are irritating. In fact, I think those kind of people are lazy and can't read a little on how it is, I also usually say use google, but sometimes those guys say "what kind of support it is if you only can put a smile or say use google or read the php manual and even can start swearing" but why should I answer stupid questions ? I am not paid for that, all I want to do is help with serious problems.. I think you got the point, that most of those kind of people won't even bother to read the first post of this topic - this is the real problem.

Of course it make sense, and i think that a more general way to do the database selection is by using an universal function with the database name as a parameter.
best regards,


In my opinion, the best way is to create a database class, for example class Database { code } and call it $Database = new Database; and play with all the functions in it by using $Database->getContent($var); or call the values of the variables inside the class $Database->mError; :D

Share this post


Link to post
Share on other sites

Use a Database Interface class. There are several available and keeps everything nicely organized. However you will still have to include a file that creates your database class ..or simply re-declare your database object in every script. I think the include method is a nice way to accomodate everything. Especially if you're working on personal stuff.I'm not too familiar with working with large-scale site, but I use a Pearl-like dbi to handle all of my database calls. Just connect once in an include file, and make reference to the database handle inside my scripts that need to interact with the db. There's one dbi mentioned on here, and it might be worth looking into. Adodb or something like that. It looks like a very nice dbi to standardize all your db calls no matter the database.

Share this post


Link to post
Share on other sites

Use a Database Interface class. There are several available and keeps everything nicely organized. However you will still have to include a file that creates your database class ..or simply re-declare your database object in every script.
I think the include method is a nice way to accomodate everything. Especially if you're working on personal stuff.

I'm not too familiar with working with large-scale site, but I use a Pearl-like dbi to handle all of my database calls. Just connect once in an include file, and make reference to the database handle inside my scripts that need to interact with the db.

There's one dbi mentioned on here, and it might be worth looking into. Adodb or something like that. It looks like a very nice dbi to standardize all your db calls no matter the database.

I agree, use a database interface class is the best option in my opinion, and for that in all my future development i will -must- use someone, a few days ago i test the PEAR:DB class, and is very easy to use.

best regards,

Share this post


Link to post
Share on other sites

I have 3 textbox and 1 submit button. When I click the submit it will store the mysql database. And also shows the table format in browser.When I click the table definition value that it will show textbox field.These values retrive from database value.Pls send me sample program.Thank you.-question by selvam

Share this post


Link to post
Share on other sites

WAMP...thank you SO MUCH!!! You wouldn't believe the roadblocks I've faced. I've been desperately wanting to learn PHP for some time now, but never could get it to run on my PC. I figured I would learn something about it in college, but to no avail, and no one I seemed to know could give me any clear answer on this. Now I have another language to have fun with, and maybe soon you'll see the newest Facebook site with yours truly as the author!

Edited by Jonnyabc

Share this post


Link to post
Share on other sites

If you're looking for a Windows All-in-One Web Server (Windows Apache MySQL PHP). I highly recommend Uniform Server which is configured in a ready to use state in live production and is usually ahead of all the other All-in-One packages when it comes to updating their software.

 

If you're looking at competing with Facebook, be sure to check out the OpenSocial API which will help in creating your social network quicker, as well as being capable of interacting with other social networks that use the API too.

 

 

Cheers,

 

 

MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now