- Add MySql connection to a Configuration file
- 4 Answers 4
- Tutorial to create a login system using HTML, PHP, and MySQL
- Table of Contents
- 1) Building a Signup system
- Step 1: Creating Registration Form in HTML
- Step 2: Creating the MySQL Database Table
- Step 3: Creating Database Configuration File
- Step 4: Creating a Session File
- Step 5: Create Registration Form in PHP
- 2) Building a Login System
- Step 1: Creating a Login Form in HTML
- Step 2: Creating a Login System in PHP
- 3) Creating a Welcome Page
- 4) The Logout script
- Conclusion
- Where to put database connection settings?
- 4 Answers 4
Add MySql connection to a Configuration file
I wrote a php class which connects to a MySql DB, and then does an INSERT. I have done this, and it works. Now, i want to add the connection part of the code to a configuration file, where i could call it from here. How could i do this in PHP ?
what have you tried? also new code should not use the mysql_* functions you should learn and use PDO or mysqli.
@Tynarus I mean, to add the connection string in 1 file, and then access it where ever i want from another PHP file. (Something like a configuration file)
Use require_once to retrieve the configuration file? It will automatically run anything inside of the file you’re fetching. php.net/manual/en/function.require-once.php
Provided the configuration file is another PHP file, I should say. Apologies. HTML files work too, but then it would just display the HTML inside the file.
4 Answers 4
connect to a mysql database by using ph is very simple. you write the code in a ok way, although it is better to use MySQLi or PDO not MySQL function, because they may have some sql injection or other security problems. give your file a name like config.php and add it to whenever you want to use mysql connection. use require_once or include functions of php to use this.
yes. you must do that.however it is not necessary, it is just because standard programmers are advised not to use mysql function.this is just for security reasons, although it works ok.
also you can use a if else condition in your config file. you can set in it , what server you are working on. if you working on localhost,then you can make a connection fir this ,else if you are on a live server then make another connection for this. then your one config file works fine on both station and you will not need to edit it. if you find me helpful, please vote me:)
I don’t understand exactly but por put this parameters in a configuration file you do this Configuration File
$config['hostname'] = 'yourserver'; $config['user'] = 'youruser'; $config['password'] = 'password';
and then call this in your class connection
mysql_connect($config['hostname'],$config['user'],$config['password']);
or more easy build a construct with the parameters of conexion, and look as this
function __construct($host,$user,$password,$database)
a guide detail of advantages and disadvantages of pdo and mysqli [link]net.tutsplus.com/tutorials/php/… but PDO id a library general when you unknow the database server that use and mysqli is specific for mysql
As from a notice from PHP they wont be officially deprecating the mysql_* extension till at least PHP 5.5/6.0 so you have some time to rewrite your script before those function are completely gone or E_DEPRECATED errors start appearing. http://news.php.net/php.internals/53799
So use PDO or mysqli for new scripts:
Connect with PDO
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); >catch (Exception $e)< die('Cannot connect to database. Details:'.$e->getMessage()); > ?>
Right now, if the above code is in your config file then you can use include(‘config.php’) and then $con will contain your connection object.
As $con is not global scope you would need to pass it to your class via the construct.
db = $con; > function someMethod($valueA,$valueB)< $sql = "INSERT INTO TABLE ( colA, colB )VALUES( :colA, :colB )"; $query = $this->db->prepare($sql); $query->bindParam(":colA", $valueA); $query->bindParam(":colB", $valueB); $query->execute(); > > $aClass = new yourclass($con); $aClass->someMethod('someValueforA','someValueforB'); ?>
Tutorial to create a login system using HTML, PHP, and MySQL
This is a tutorial for creating a login system with the help of HTML, PHP, and MySQL. Your website needs to be dynamic and your visitors need to have instant access to it. Therefore, they want to log in as many times as possible. The login authentication system is very common for any web application. It allows registered users to access the website and members-only features. It is also helpful when we want to store information for users. It covers everything from shopping sites, educational sites, and membership sites, etc.
This tutorial is covered in 4 parts.
Table of Contents
1) Building a Signup system
In this part, We will create a signup system that allows users to create a new account to the system. Our first step is to create a HTML registration form. The form is pretty simple to create. It only asks for a name, email, password, and confirm password. Email addresses will be unique for every user. Multiple accounts for the same email address are not allowed. It will show an error message to the users who try to create multiple accounts with the same email address.
Step 1: Creating Registration Form in HTML
We will create a PHP file named register.php with the following code in it. This is a simple HTML form with some basic validation. If you are not familiar with HTML then you can get it from many online sites who give ready-made html5 login form templates.
Register
Please fill this form to create an account.