473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Not connecting with database , what is the universal code

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
Jun 2 '15 #1
2 1452
In connecting your database you have to set it properly with your localhost,username,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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $servername = "localhost"; // this is the default
  3. $username = "username"; // the default is root and use your username if you change it.
  4. $password = "password"; // if your database has no password just leave it blank "";
  5.  
  6. try {
  7.     $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
  8.     // set the PDO error mode to exception
  9.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     echo "Connected successfully"; 
  11.     }
  12. catch(PDOException $e)
  13.     {
  14.     echo "Connection failed: " . $e->getMessage();
  15.     }
  16. ?>
  17.  

this sample code is PDO, but you can also use the Object-Oriented , Procedural if want.

I hope it helps. :)
Jun 2 '15 #2
Exequiel
288 256MB
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.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //how to connect to database. . .
  3. $connect=mysql_connect("localhost","root","");
  4. mysql_select_db("my_database",$connect) or die("Unable to connect to Database");
  5. ?>
  6.  

This must be your main page or home page that needs to access the database.
I called this homepage.php
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. include 'connect_to_db.php';
  3.  
  4. echo "this is my home page.";
  5. ?>
Here in our homepage.php we used include to access or to include directly the connect_to_db.php 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.

Not: be careful to your page path.
Try this.
Jun 3 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Skybuck Flying | last post by:
I have finally figured out what the hell it is I invented :) It's another universal code: http://en.wikipedia.org/wiki/Universal_code I shall name this code: Skybuck's universal code...
0
by: Arda | last post by:
Hi all, I tried to connect a database(SQL) by both VS 2003 interface tools and normal typing. When I make the connection in local Web project it doesn't work and gives an error like this; ...
4
by: farhan | last post by:
Please let me know code used to simply connect a database and then add, delete, update records in asp.net using vb.net.
4
by: MasterChief | last post by:
I am trying to learn how to delete multple items in a database using the code behind file. I would just like somebody to tell me what is wrong with my code. I am new to connecting to the database...
1
by: dean.brunne | last post by:
Hi, How can I run a macro stored in another database using code?? I figure it would be something like: Dim db as Database Set db = ?????
0
by: dalaimanoj | last post by:
I have a form with buttons 'View', 'Save', 'Delete', 'Edit' and some textboxes to input data.. On clicking each buttons the database is connected and the respective action is done. For example...
1
by: sravani1 | last post by:
I am new to the php. i wrote the code to connect to the database. I will use the following code. <?php $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect:...
0
by: ezrockgw | last post by:
i want to make an application in j2me so that it interacts with database and keeps updating it. How can i connect my mobile to the pc which has the database?...Will it be through bluetooth which i...
0
by: svibuk | last post by:
creating a xml dcoument from database thr' code in vb6.0 & validating it against xsd using msxml4.0 everything working fine the xml file is formed from the code / the data i get during runitme...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.