How to connect to phpmyadmin database? - php

I'm trying to connect to a database in phpmyadmin. I'm new to the process and unsure how to connect to it.I'm using godaddy to host. I have this line of code:
$db = mysqli_connect("https://.....secureserver.net", "...", "....", "authenticationdb_");
I went to the table on phpmyadmin and copied the url, then copied the username (the first "...") and password (the second "...") that I used to login in to godaddy, and the name of the database is authenticationdb_.
This is not working and I'm not sure why. I was unsure if the username and password were the ones that I used to login in to godaddy but I don't know what else they would be since i accessed phpmyadmin thru godaddy.

<?php
$servername = "localhost";
$username = "username"; //your user name for php my admin if in local most probaly it will be "root"
$password = ""; //password probably it will be empty
$databasename = ""; //Your db nane
// Create connection
$conn = new mysqli($servername, $username, $password,$databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Related

How to fix error in dbconn file when upload php mysql project to free webhosting site?

I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1

Which username/password do I use to connect to mySQL

So, I'm SUPER new to mySQL. Having issues connecting to my database with PHP. Hoping someone can point me in the right direction.
I can login to our Cpanel using my username/password. Using the web gui I was able to create a database.
Now when I try to connect to the database (or even just the server for that matter) using PHP I get an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'servername' (using password: YES) in /home/ropepart/public_html/techportal/sandbox/mysqltest.php on line 8
Connection failed: Access denied for user 'username'#'servername' (using password: YES)
To me, it seems like this is a username/password error. But I an using the same username/password that I use to login to the web gui. Why can I successfully login there, but can't login with PHP?
Here's my code (taken pretty much directly from W3Schools):
<?php
$servername = "servername";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
(Hopefully it's obvious that I've changed my servername/username/password for the purposes of this post)
Check the details correctly, By deafult
host = localhost
username = root
password = (No password leave it as empty)
database = (your database)
<?php
$connection = new mysqli('host','username','password','your_database');
if($connection->connect_error || $connection->error){
echo "error";
}else{
echo "done";
}
?>
you should add database name.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo"
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
//if you are using xampp you shoul locate this file you get information from this
passwords.txt
MySQL database username/password is not necessarily same as cPanel username & password.
Here is what you should do:
Login to your cPanel
From dashboard click "MySQL® Databases"
Add a new user from there (https://prnt.sc/kpiby9). Just fill username
and password (note down the password).
Add that user with your database by selecting both from the dropdown
(https://prnt.sc/kpidqx)
Now, use this username & password to connect with the database.
mysqli_connect(serverhostname,username,password,dbname);

How to link up a database in a HTML base website in offline server (Xamp)?

I have created a website for my versity project...But I am asked to add a database in it..That's why I need to know how to add a database via xamp in a website.
Use this code to create a connection to your database from php
<?php
$servername = "localhost";
$username = "root";
$password ="db-password";
$dbname = "db-name";
//create connection
$conn = new mysqli($servername,$username,$password,$dbname);
//check connection
if ($conn->connect_error) {
die("connection failed" .$conn->$connect_error);
}
?>
use this link to understand how to create and execute queries.
https://www.w3schools.com/php/php_mysql_connect.asp

What user and password in phpmyadmin will I use in cpanel?

This is my very first time uploading a website. now I'm having a problem with my hosting which is CPANEL.
I'm using the code
<?php
date_default_timezone_set("Asia/Manila");
$servername = "localhost";
$username = "root";
$password = "";
$db = "exampledb";
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
for my database connection.
Now my problem is what code should I put in servername? instead of localhost?
and same to the username root and password?
On the hosting site in cpanel they normally give you the server name that you can use, the password you will need to create a db user also on cpanel, this will be your user name and password.
// localhost is fine or else you need to keep your server IP.you can find in cpanel.
$servername = "localhost";
// if you are using database in some other server you should provide IP
// you need to create a user with required privileges then need to provide username/password
// root also fine but better create a user
$username = "root";
$password = "";
// Here you need to mention which DB you want use for your application.
$db = "exampledb";

Upgrading from php5 to php7 having trouble running scripts [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 5 years ago.
I wonder if someone can help. I gave up web programming about 3 years ago. Since returning to programming about a week ago I realize I have forgotten quite a lot and so much has changed.
I signed on to a deal with php7 but my code is php5 and when I came to running my scripts nothing really works.
For example I couldn't even connect to the database. My database connection file for php5 was
<?php
$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "db_username_here";
// Place the password for the MySQL database here
$db_pass = "db_password_here";
// Place the name for the MySQL database here
$db_name = "name_of_database_here";
// Run the actual connection here
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I spoke to the server company and they told me everything has changed and to get my code compatible with php7. They then helped me get my database connected by giving me a sample file of how it should be which is this code.
<?
/* DATEBASE CONFIGURATION */
$dbHost = "ip Address";
$dbName = "name_of_database_here"; // Database name
$dbUser = "db_username_here"; // Database user
$dbPasswd = "db_password_here"; // Database password
function dbConnect(){
global $dbHost, $dbUser, $dbPasswd, $dbName;
mysql_connect( $dbHost, $dbUser, $dbPasswd ) or error( mysql_error() );
mysql_select_db( $dbName );
}
?>
I have always run my database $dbHost from localhost so I have guessed that because they haven't done the connection as localhost and done it as gobal with an ip address I have figured that the database is not on the same server as the website.
I then came to my database scripts for running from the browser to phpmyadmin and they also didnt work. Here is my database script php5.
<?php
require "connect_to_mysql.php";
$sqlCommand = "CREATE TABLE admin (
id int(11) NOT NULL auto_increment,
username varchar(24) NOT NULL,
password varchar(24) NOT NULL,
last_log_date date NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY username (username)
) ";
if (mysql_query($sqlCommand)){
echo "Your admin table has been created successfully!";
} else {
echo "CRITICAL ERROR: admin table has not been created.";
}
?>
I was wondering if someone could give me some advice on why the script isn't running
Many thanks, Gary
mysql_connect is removed from php7.
instead of that you can use either mysqli or pdo
example with mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
example with pdo
<?php
$servername = "localhost";
$username = "username";
$password = "password";
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();
}
For more reffer details https://www.w3schools.com/php/php_mysql_connect.asp

Categories