Phpmyadmin mysql server choice - php

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

Related

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.

What is servername

i just wanted to insert data into database from a form, with php. i ran the code below in my Localhost using XAMPP and everything was fine but where i upload it to my host it didn't work.
Question is What shold i put for $servername and when should i look for it ?
There is my codes:
Register.php (in localhost)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$Name = $_POST['Name'];
$Username = $_POST['Username'];
$Password = $_POST['Password'];
$Email = $_POST['Email'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
header("Location:#");
}
//Inserting Data
try{
$sql = "INSERT INTO User (uName , uUsername , uPassword , uEmail) VALUES ('$Name' , '$Username' , '$Password' , '$Email')";
mysqli_query($conn, $sql);
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
$conn->close();
header("Location:#");
}
?>
If your MySQL database is on the SAME SERVER as your PHP script, then the usual logical approach is that your host is localhost. The same as you used on your local computer -- because they're on the same machine.
However, if your MySQL database is on ANOTHER SERVER seperate from your PHP scripts the you will need to access that server using a web address for your PHP to connect to yout MySQL.
We can't tell you what that is, and your server hosts (of your MySQL server) will be able to tell you and provide you with the correct login credentials.
I believe it would be more usual for MySQL and PHP to be on the same disk, especially for non-professional systems as your appears to be, so then the issue would be:
Are your login details set up correcty on your server? (same username/password)
Are there any MySQL errors or PDO errors (if you connect with PDO). Don't redirect on error, but instead output the error to a log file so you can read WHY the MySQL in your code didn't connect.
It is still possible for you to set your PHP to communicate with your localhost MySQL via a remote address (such as servername=$_SERVER['SERVER_NAME'];). (see note below)
Many online accounts (in things such as CPanel) will block you from accessing the MySQL as a root or at least will not give you the root MySQL password. Using root to access MySQL via PHP is NOT a good idea and you should instead set up a specific MySQL user for your PHP with only enough privileges that you need to read/write to the DB, and nothing more.
If your MySQL is remote (not localhost) then you may also need to supply a Port Number with the connection details. Usual port numbers are 3306 but this is something you'd need to know from your server hosts.
Immediately after a header(Location:); redirection instruction you should always set die(); or exit to stop PHP processing the rest of the script.
Your SQL insert data is highly suseptible to SQL injection and other SQL attacks and compromise. You should really, REALLY look into using MySQL Prepared Statements, you're already coding in OO style so you're almost there already.
Example remote connection from the manual
<?php
/***
* Remember 3306 is only the default port number, and it could be
* anything. Check with your server hosts.
***/
$conn = new mysqli('remote.addr.org.uk', 'username', 'my_password', 'my_databasa', '3306');
/***
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
***/
if ($conn->connect_error) {
error_log('MySQL Connect Error (' . $conn->connect_errno . ') '
. $conn->connect_error);
}
/***
* Upon failure, the above will output a connection error notice such as
* user not found or password incorrect. It won't explicity say these
* things but you should be able to deduce which from the notice
***/
echo "Success... \n" . $conn->host_info ;
$mysqli->close();
# : I seem to think that MySQL detects when the remote address given is the same as the server address and auto converts it to localhost, but I'm not sure on this.
The long and the short of it is that if your MySQL is on the same
server as your PHP it makes no sense to open up a network loop to send
data out just to get it back again. Use localhost instead.
I asked my host service providers about the "$servername" and they answered me that the "$serverneme" is localhost.

Setting up MySQL database for website

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);

php connecting to two databases on diff servers and using insert select

hi I need some help what I am trying to do is using insert select statement like this where databases are on diff servers.
insert into db1.copy2 (c1,c2) select c1,c2 from db2.copy2;
db1 is on amazon web service relation database and db2 is on local host(wamp) how can i make a PHP script that can accommodate two databases like that and that will allow me to execute a query like that. this script will execute on local host
thanks..
There are many pieces of software for syncronising or copying data between to database servers, maybe you should use one of those. Otherwise you could just do it in 2 steps
in pseudo code
$sql = "select c1,c2 from db2.copy2";
$Results = pdofetchall($sql);
foreach ($Results as $row){
$c1 = $row['c1'];
$c2 = $row['c2'];
$InsertSQL = "insert into db1.copy2 VALUES($c1,$c2) ";
executesql($InsertSQL);
}
Note this is pseudo code and defintitely cannot be copied and pasted, but give you the gist of what you need to do.
You have 3 options:
If you can connect the 2 servers via a vpn network or ssh connection so that the 2 servers can directly see each other, then you can use federated table engine:
The FEDERATED storage engine lets you access data from a remote MySQL
database without using replication or cluster technology. Querying a
local FEDERATED table automatically pulls the data from the remote
(federated) tables. No data is stored on the local tables.
This solution would enable the syntax you used in the question.
Again, you have to enable direct connection between the 2 servers and you set up replication between the 2 servers, essentialky making sure that the data available on the localhost is copied to the amazon server. The query would run on the amazon server and would query the copied data.
You combine data within php from the 2 data sources either by directly connecting to both instances or by setting up a web-based api on local server that can be used to transfer data from the local server to amazon.
you can do this easily if your 2 database on the same server, just make a connection to one database and pass your query easily:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "insert into db1.copy2 (c1,c2) select c1,c2 from db2.copy2;";
this will work for you

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