Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Important: Basics Of Using PHP And MySQL
Rating 5 V
Houdini
post May 26 2006, 06:30 PM
Post #1


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



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 astahost 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 astahost.

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.
Go to the top of the page
 
+Quote Post
lonebyrd
post May 26 2006, 10:43 PM
Post #2


Premium Member
Group Icon

Group: Members
Posts: 302
Joined: 23-February 06
From: Northeastern Connecticut USA
Member No.: 11,487



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 Astahost site. Now I know that I should be doing it through Xammp first.
Go to the top of the page
 
+Quote Post
Houdini
post May 27 2006, 11:33 AM
Post #3


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



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
CODE
<?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 astahost it would be something like
CODE
<?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
?>
Go to the top of the page
 
+Quote Post
TavoxPeru
post May 29 2006, 09:19 PM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(Houdini @ May 27 2006, 06:33 AM) *

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
CODE
<?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 astahost it would be something like
CODE
<?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:
CODE
$database="databasename";
my_sql_select_db($database) or die("Error... no database selected, MySQL said: ".mysql_error());

Best regards,
Go to the top of the page
 
+Quote Post
Houdini
post May 29 2006, 09:39 PM
Post #5


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



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
CODE
<?php
include("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?

This post has been edited by Houdini: May 29 2006, 09:40 PM
Go to the top of the page
 
+Quote Post
TavoxPeru
post May 30 2006, 12:30 AM
Post #6


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(Houdini @ May 29 2006, 04:39 PM) *

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
CODE
<?php
include("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,
Go to the top of the page
 
+Quote Post
Houdini
post May 30 2006, 01:26 AM
Post #7


Super Member
Group Icon

Group: Members
Posts: 572
Joined: 25-April 05
From: Nashville Tennessee
Member No.: 4,340



Do you mean by generalized like the MySQLI function of
CODE
$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.

This post has been edited by Houdini: May 30 2006, 01:27 AM
Go to the top of the page
 
+Quote Post
Quatrux
post May 30 2006, 08:45 AM
Post #8


the Q
Group Icon

Group: [HOSTED]
Posts: 1,051
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



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.

QUOTE(TavoxPeru @ May 30 2006, 03:30 AM) *

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; wink.gif
Go to the top of the page
 
+Quote Post
minnieadkins
post May 31 2006, 01:21 AM
Post #9


Premium Member
Group Icon

Group: Members
Posts: 292
Joined: 15-December 04
Member No.: 1,768



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.
Go to the top of the page
 
+Quote Post
TavoxPeru
post May 31 2006, 06:05 PM
Post #10


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(minnieadkins @ May 30 2006, 08:21 PM) *

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,
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL & PHP coding(9)
  2. Help With PHP Basics(9)
  3. Need Help With A PHP - MySQL Registration Script(13)
  4. Printing Out A Table(6)
  5. Need Some Help Using PHP & MySQL(4)
  6. [PHP + MySQL] Separating The Results By Pages(0)
  7. [PHP + MySQL] Encrypting Data(11)
  8. [php] Index.php?section=xx&pag=yy(6)
  9. How Do You Create A Secure Loging?(4)
  10. Need Help With Php/mysql And Web Servers Such As Asta's.(4)
  11. Php Basics(5)
  12. Need MySQL Alternative To The Syntax "or die()"(8)
  13. Re-order MySQL Table(11)
  14. PHP & MySQL: Displaying Content From A Given ID(6)
  15. How To Show Serial Nums In PHP Table For Contents Of MySQL DB(4)
  1. Php Mysql Errors(2)
  2. Sql Injection Prevention (passing Numerical Data Across Pages).(9)
  3. Php/mysql And Manual Page Caching?(4)
  4. Basics Of Php For Beginners - Suggestion(5)
  5. Too Many Connections?(4)
  6. Extracting Mysql Maths Using Php(2)
  7. Anyone Know Of A Really Good Mysql Class?(4)
  8. Warning: Mysql_num_rows()(1)
  9. Warning: Mysql_result(): Supplied Argument Is Not A Valid Mysql Result Resource In ...(4)
  10. Making A Link = Mysql_query(8)
  11. Making Something In Mysql Happen Only Once(10)
  12. Mysql Question(inserting Number From A Textfield)(3)
  13. Letting Users Add Mysql Data With Php(1)


 



- Lo-Fi Version Time is now: 11th October 2008 - 06:05 PM