hello am mr. md.tajuddin. i am using XAMPP i prepared students database in phpmyadmin and user_regist table but i am unable to connect my programme with student database.. i used maximum connecting php codes how ever its not connected.. pls give solution to overcome this problem
Not connecting with database , what is the universal code
Collapse
X
-
In connecting your database you have to set it properly with your localhost,usern ame,and the password
I just make an example for you to easily understand ion how to connect your database from your program.
in a new file create a connection.php
then after you create copy the code below and change the servername, username and the password and try link this to your files.
Code:<?php $servername = "localhost"; // this is the default $username = "username"; // the default is root and use your username if you change it. $password = "password"; // if your database has no password just leave it blank ""; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
this sample code is PDO, but you can also use the Object-Oriented , Procedural if want.
I hope it helps. :)Last edited by Rabbit; Jun 2 '15, 04:14 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data. -
THis is the best and easiest way on how to connect to your database.
This must be your code on how to connect to database.
I call it connect_to_db.p hp
Code:<?php //how to connect to database. . . $connect=mysql_connect("localhost","root",""); mysql_select_db("my_database",$connect) or die("Unable to connect to Database"); ?>
This must be your main page or home page that needs to access the database.
I called this homepage.php
Here in our homepage.php we used include to access or to include directly the connect_to_db.p hp so that in every page we do not need to create a code for connection to database, using include we can display a page to another page and accessed on whats that page have.Code:<?php include 'connect_to_db.php'; echo "this is my home page."; ?>
Not: be careful to your page path.
Try this.Comment
Comment