473,549 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not connecting with database , what is the universal code

1 New Member
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 1459
weboutgateway
23 New Member
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.

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

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
1459
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 (pseudo name)
0
1183
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; Server Error in '/Logon' Application. ---------------------------------------------------- Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
4
1410
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
2159
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 through a code-behind. I have a gridview called gridview1. When the user checks a bunch of plans then should be able to hit delete and for each...
1
3374
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
1236
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 clicking on Save button inserts records, clicking on Delete button delete the specified record and clicking on View button the record from database table is...
1
1168
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: ' . mysql_error()); } // some code
0
1743
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 want or through Internet? I read a few posts which said u cant connect j2me directly, u have to connect through servlet....but will it require net...
0
1129
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 when i validate it it gives me the validation results but it seems that the entire xml doument is in one line though the xml document tags appears in...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.