how to link mysql data base with my localhost website - php

I am beginner with creating websites
I have already created a website by html and ccs
and also i created the database by my sql queries
Now still link the database with my website in xampp server
But I have no idea how to do it
I search google i found one explanation that told me to create db_connection.php file in htdocs where my website files located and import my database file to myphp admin
I did all these steps
I don't know what is the next step
should i write any codes in each page code of my website or not
I hope someone help me
I will really thankful for him/her

In your db_connection.php you should write
$dbServername = "localhost"; //if you use xampp, it will be localhost
$dbUsername = "yourUsername"; //standart login is root
$dbPassword = "yourPassword"; //standart password is none, so leave it empty
$dbName = "yourDbName"; //place your db name here
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
Now you are connected to your db. Include this file where you need to perform query's.

Related

How to connect to database on freewebhostingarea?

Ok so i am making account system for my website. I am using freewebhostingarea as host. But i dont know what is servername(for connecting to database). Can you help me? Here is my code for connecting:
<?php
$servername = "";
$server = "root";
$password = "my password";
$dbName = "name";
$conn = mysqli_connect($servername, $server, $password, $dbName);
?>
I dont know what is $servername for freewebhoatingarea. Pls reply.
It's all the same actually . I want you to try this 4 steps
After you hit create databaase it will give you an option to name/rename your database so save that name to something like a notepad for your reference
(e.g) freehosting_db_(sampledatabase)
Next to that it that before you hit next on the creation of database kindly remember that password that you are going to make and like the name of the website save it to a notepad so you can remember the password
(e.g) xHsnaAXa123 -> password database
Mostly the server always localhost on SOME freehosting
The servername is automatically generated so look for it. It will be something like
(e.g) freehosting_db_(sampledatabase)_user
And to set that up
$servername = "freehosting_db_(sampledatabase)_user";
$server = "localhost";
$password = "xHsnaAXa123";
$dbName = "freehosting_db_(sampledatabase)";
PS: This is mostly how you setup a connection from your database to your server . But it is always depends on the the hosting that you are going to use . Godadday, Hostgator, 000webhostingapp, they are all alike on how setting it up .

Is the correct way to connect my website to my server?

I'm a total beginner when it comes to PHP, I have a fair grasp of the syntax but I'm not sure about the safest way to utilise it to connect to my server. I apologise that this is a sort of generic question rather than a code problem, since my code technically works.
I have a .php site doc with a basic comment submission form. The only way I can think of to connect to the server is to allow a "dummy" user with select only privelege to call a stored function to accept the comment.
If my dummy account is called siteuser then am I going round this the right way? This is the section of the PHP that I'm using to connect. I believe this code is only visible server side so nobody can ever see it and use the password or username to connect some other way? Or is there a sort of default string I can use in my php without creating the dummy user, seeing as the php and server is all hosted via the same provider?
$sqlserv = "localhost";
$sqlname = "siteuser";
$sqlpass = "mypassword";
$sqldbdb = "comments_table";
$conn = new mysqli($sqlserv, $sqlname, $sqlpass, $sqldbdb);
What i do is this to connect to my DB
db.php:
<?php
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('/somepath/config.ini');
//Mysqli Connection
$conn = new mysqli($config['host'], $config['user'], $config['pass'], $config['dbname']);
if($conn->connect_errno > 0){
die('Unable to connect to database [' . $conn->connect_error . ']');
//Set encoding
mysqli_set_charset($conn, "utf8") or die;
}
?>
and in config.ini:
[database]
user = johndoe
pass = someweirdpassword
dbname = the_name
host = localhost
both files have 700 permissions, so only user (and no one else can access it)
also the config.ini file is placed somewhere outside the public_html directory, i'm not totally sure if that helps or not but i do it that way.

Link PHPMyAdmin MySQL databases to a PHP file

I'm running into an issue while trying to link a MySQL database to a PHP file.
Im trying to setup this PHP login system from Github and currently I'm trying to setup the dbconf.php file. This is the code where I'm trying to link the database to.
<?php
//DATABASE CONNECTION VARIABLES
$host = "localhost"; // Host name
$username = "user"; // Mysql username
$password = "password"; // Mysql password
$db_name = "22445671_login"; // Database name
//DO NOT CHANGE BELOW THIS LINE UNLESS YOU CHANGE THE NAMES OF
THE MEMBERS AND LOGINATTEMPTS TABLES
$tbl_prefix = ""; //***PLANNED FEATURE, LEAVE VALUE BLANK FOR NOW***
Prefix for all database tables
$tbl_members = $tbl_prefix."members";
$tbl_attempts = $tbl_prefix."loginAttempts";
The MySQL Database is named 22445671_login
I am just looking for a way to link the MySQL database to my document
Any help is greatly appreciated, thanks.
As put into my original question before it was removed. My website is hosted at at-space which comes with a PHPmyAdmin account linked to my website, I dont know how to link the MySQL database since I don't know if the username and password are just username and password. And I dont know if the host would be 1. My Website 2. atspace or 3. PHPMyAdmin
Where is your database hosted?
You might need to replace "localhost" with the hostname of your server:
$host = "localhost"; // Host name
Make sure you have the correct username and password
Make sure the user has the necessary permissions
Not sure if PMA has this feature, but SIDU has this feature:
<?php
$url = 'http://example.com/sidu54/conn.php'
.'?conn[host]='. $host
.'&conn[user]='. $username
.'&conn[pass]='. $password
// OR .'&conn[pass]='. $encryped_pass .'&conn[penc]=1'
.'&conn[dbs]='. $db_name
.'&conn[eng]=PDO_mysql'
.'&conn[char]=utf8'
.'&cmd=Connect'
.'&url=db.php'
.'&id=1,'. $db_name;
echo 'Click here to manage database';

Can't get my PHP document to connect to my xampp server

I am very new to php, and am sorry if this question turns out to be too vague, but if there is any other information that you need from me let me know.
Anyway, basically what my problem is I have some simple php code that should call die() if it can't connect to the xampp server I have set up, however, even if I put in invalid info for the server or user it prints Successful. I really don't know where to go with this, so if anyone has any suggestions that would be great.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if ( !mysql_connect($dbhost, $dbuser, $dbpass) )
{
die(mysql_error());
}
else
echo 'Succesful';
?>
Ok for some reason it was just not working with the root user, so I created a new user who required a password and apparently that connected like it was supposed to. Thanks everyone for the answers.
Your code is correct pistolpete333. For your root user issue, that your not able to connect with root user you can check below file for your phpmyadmin configuration
C:\wamp\apps\phpmyadmin3.5.1\config.inc.php.
In above file you can check your host, username and password of phpmyadmin.
I have found that when trying to connect php to an xampp server there are usually only a few things that could cause the issue you are having.
1. mySQL service is not running
2. no specific database is selected to try to access
3. user does not exist in phpmyadmin.
When you pick a user to connect php to mySQL with you should always make sure that a password is set, that is usually the thing people miss. You can use root if you create another instance of it in the user list and require a password, or you can add a password to the root user already in the list, but the best option is to create a new custom user that only has access to the database you want to access with your php and make sure it has a password required. (make sure any user you use can connect with localhost; root can by default and I think most newly created users can as well).
Below is the php I use for websites I design that need php to access a DB.
<?php
// This configures the variables for database access
$dbhost = 'localhost';
$dbuser = '????';
$dbpass = '????';
$dbname = '????';
// This will connect to the server and then the correct database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
This is working because you have specified valid username, host and password to mysql. If you specify wrong password, you get mysql error. This code works and connects to the mysql server though you have not selected any database.
Try this code instead
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link); //foo is your database name
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>

PHP Include DB Conn - Password not accessible

I have a strange include file problem. I'm hoping its a simple fix due to being a 2-day-old, PHP noob.
I'm simply trying to include DB credentials in to my parent script, like this:
db-conn.php
<?php
$dbUsername = "xxx";
$dbPassword = "xxx";
$dbHostname = "localhost";
?>
parent.php
<?php
include("c:/inetpub/vhosts/mydomain.net/php-private/db-conn.php");
//echo $dbPassword;
//echo $dbUsername;
$dbHandle = mysql_connect($dbHostname, $dbUsername, $dbPassword) or die("Unable to load page content due to a connection fault.");
unset ($dbHostname, $dbUsername, $dbPassword);
echo "Connected to MySQL<br><br>";
?>
But I'm getting this error:
Access denied for user 'xxx'#'localhost' (using password: NO)
What is strange about this (for me) is that I can echo the username and hostname successfully but I cannot echo the password, it's empty. Yet if I go to my include file, copy the password from 'dbPassword' and paste it inline on my parent page, it works. This proves my password is correct, the username and hostname ARE included, I just can't access my password variable!
Can somebody pleeeease put me straight here. I've already pulled my hair out tonight with open_basedir and permissions for this include file!! _
*UPDATE - *
Problem solved, please see my answer below.
If db-conn.php and parent.php are in the same folder, you can directly use
include ('db-conn.php');
And I'm not sure about the use of unset ($dbHostname, $dbUsername, $dbPassword);
Also it's saying that no password has been set. So maybe you should use empty string for the $dbPassword variable $dbpassword = '';
Access denied for user 'xxx'#'localhost' (using password: NO)
is simply telling you that you are not giving password to mysql_connect()
You can 1. Check if you are including the correct db-conn.php file.
2. Add this to the top of your db-conn.php
global $dbUsername, $dbPassword, $dbHostname;
Try using 127.0.0.1 as a hostname. The problem is that sometimes PHP doesn't accept localhost as host, because of a missing line in the host.ini file.
Sorry for wasting time guys. I found the problem. I didn't give FTP users write access to the \php-private\ folder I created before the root, so when I was changing the file and uploading it, if I actually checked the FTP logs I would have seen that it failed with 'Access Denied'. I changed permissions and kapow, my CURRENT include file works fine! What a pleb!

Categories