|
-
Nov 6th, 2008, 01:00 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] PHP and MYSQL - Connecting.. etc.
I am new to the world of PHP.
Q1. In ASP.NET there was a server side file I could include that would store the connection string rather then storing it in the actual page code, is there an equivalent (or better practice) for PHP?
Q2. I am designing the MySQL database and want to control/test the inputs.. in ASP.NET I would add a AddStudent function with alll the fields for parameters then audit the data before committing it. Is there an equivalent (or better practice) for PHP?
Thanks!
-
Nov 6th, 2008, 08:04 PM
#2
Frenzied Member
Re: PHP and MYSQL - Connecting.. etc.
Create a "constants" file where you define your database connection variables and store it outside your root directory (security)
Include this file in the scripts you want to make a db connection.
This might explain a good way.
The second sounds like a normal practice in all language, so yeah, thats the way to do it.
-
Nov 7th, 2008, 04:12 AM
#3
Re: PHP and MYSQL - Connecting.. etc.
The most common way to do that is have a dbconnect.php include, which sets up your connection using variables from that file or like Beijing said - a constants file.
In PHP & MySQL there are no connection strings, you simply call a connect method or function and pass in the host, user, pass and database name as arguments.
If you're coming from an ASP.NET background you are probably used to coding in an OOP style. So I would recommend having a look at the built in MySQL class called MySQL Improved Extension, it provides you with all the raw database functions.
What I do is create a data access class that extends the MySQLi class, and add my own data access functions - such as insertInto() which would build an insert statement based on an array of arguments. I only keep generic functions in this class - nothing specific to a single web app.
Then I would have a series of business classes, in your case I would have a Student object with Add, Delete method etc. So there are two clearly serperate layers: The raw data access class which I can move to any web app because it is generic and not app specific. And the business layer with the app specific objects/classes.
If your web app requires a data abstraction layer then have a look at PDO, which is a universal interface to a large selection of different DBMS such as MySQL, PostgreSQL, SQLite, Oracle and more.
Hope this helps
-
Nov 8th, 2008, 03:12 PM
#4
Thread Starter
Fanatic Member
Re: PHP and MYSQL - Connecting.. etc.
This was very helpful guys, I appreciate it.
-
Nov 8th, 2008, 09:57 PM
#5
Frenzied Member
Re: [RESOLVED] PHP and MYSQL - Connecting.. etc.
If you are going with mysqli (i guess most do?), you might want to get used to using prepared statements from the beginning.
Check it here
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|