Setting up MySQL database for website - php

I am making a website that will use a MySQL database on a friend's web hosting service. It supports PHP and MySQL but I'm not sure what file I am meant to create the database and table in. I understand the syntax so I just need to know where and which files to do it in. I guess you don't put it in the website's html files because it only needs to be created once.
I have the following code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE LeagueData";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

You don't have to create a file. You have to create the DB and structure from within MySQL (or you can load a .sql file into MySQL).
If he has phpMyAdmin use that. It's much easier.
Creating a table from within MySQL: http://dev.mysql.com/doc/refman/5.7/en/creating-tables.html
Importing an .sql file from the commandline: mysql -u username -p database_name < file.sql

Adding on to Patrick's answer:
You need to create your database first, and not through PHP. mysqli allows you to connect to a particular database (which you need to specify in your code), and then execute queries on that database.
First, create a database. You should consult your web-hosting provider on how to do this. Most web-hosts allow you to create a database through one of the CPanel modules. (You'll also have to create a "user", and assign that user to the newly created database.)
Once you have created the database, you will be able to connect to it via PHP. Your code to connect to the database should look like:
$conn = mysqli_connect($servername, $username, $password, $database_name);

Related

Phpmyadmin mysql server choice

I have installed Wamp server.
When I create databases and tables by php and mysql, all are created
in 'Mariadb'(server choice) in phpMyAdmin.
How to create database and table in 'mysql'(server choice) ?
Here is my code sample:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
In recent versions of WAMPServer it has installed both mariaDB and MySQL.
The default has become mariaDB.
In phpMyAdmin you can select the Server you want to be interacting with using the Server Choice dropdown
Alternatively if you know you are not going to use both database servers you can remove one of them and save yourself some memory.
To remove the one you dont want (It can be added back later quite easily) you simple use the WAMPSevrer menus like this
To remove mariaDB
right click wampmanager -> WAMP Settings -> and click on **Allow mariaDB**
and give WAMPServer a few seconds to configure and restart and the mariaDB server will have been removed and you will only ever be using MySQL. You will also no get the Server Choice dropdown on the phpMyAdmin login page as there will no longer be a choice.
To reactivate mariaDB later, just reverse the process and click Allow mariaDB again and it will be reconfigured and reinstalled, and the green tick will reappear by the name

PHP Read local .sq3 file for use on webpage

I have a local file (tf2stats.sq3) stored on my web server that includes name and points of players like so: https://i.imgur.com/u42zxha.png
My intention is to get the top 10 entries for POINTS and display them on a webpage as a leaderboard.
I can't find any examples on reading a .sq3 database without first connecting to a server.
Here's one of them:
<?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();
}
?>
The problem is that the game server with the database does not have mySQL set up, so I planned on periodically downloading the file over FTP and reading it locally.
Right now I have the files stored on a directoy of my website: https://i.imgur.com/yPHbWIm.png
I have php-mysql and php7.0-mysql packages installed on my web server which is using php7.0 - so far every attempt I've made to read the db gives me "No such file or directory"
I'm very new to php and sql, any help is greatly appreciated.

Can't connect my database on my live server

Hello I've been making a site on my local server and I finished it so I'm moving everything over to my live server. I've made a database in phpmyadmin on it and I would like to connect to it. I feel like I have the wrong inputs though because it gives me this error.
This is my code for my database connection.
<?php
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'user_data');
if (!$conn) {
die("Connection Failed: " . mysqli_connect_error);
}
I didn't actually enter password I just think I shouldn't show it anyway I think I just have something mixed up since I'm new at this.
Oh and the database is located within a database grouping should i input the path to it?
You need to tell it to connect to gener105_user_data instead of just user_data
return $conn = mysqli_connect('localhost', 'gener105_nate', '(Password)', 'gener105_user_data');

Does a PHP file that attempts to connect to a database have to exist on the same server as the DB?

For example, when trying to connect to server xy.xy.xyz.xyz like below, does the script have to exist on that server to work?
<?php
$servername = "xy.xy.xyz.xyz";
$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";
?>
No it doesn't. The DB server has to be configured to allow remote connections though.
You can check this for further details for MySQL.
No, what's most likely happening is that your MySQL database isn't allowing users to sign in if they are trying to access it from an outside IP address.
Run the following command through MySQL
SELECT *
FROM information_schema.user_privileges
WHERE GRANTEE LIKE '%username%';
Change the username value to whatever the actual user name value is. If you're seeing a bunch of rows where the GRANTEE column is 'username'#'localhost' and nothing that looks like 'username'#'%', then your user is being limited to only localhost (same machine) access.
If you want to grant your user non-local access, you can use the following:
GRANT ALL PRIVILEGES
ON mydatabasename.*
TO 'username'#'%'
IDENTIFIED BY 'mypassword'
In the above, % means my user can connect from anywhere. This example grants all administrator rights to your user, but you can change that if you wish.

Database connection phpmyadmin

I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO

Categories