PHP Read local .sq3 file for use on webpage - php

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.

Related

How to connect to mysql database with php?

I'm using Jetbrains and Mysql to work on this practical project, but when I connect to the mysql
database it gives me the following error:
C:\wamp64\bin\php\php5.6.40\php.exe C:\wamp64\www\Social_Network\Includes\connection.php
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'social_network'
Process finished with exit code 0
I made sure several times that the database name is the same name and there are
no spelling errors at all (I copy pasted it from the database)
Here's my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=social_network", $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();
}
//?>
Welll, there's little that can be done about it. MySQL thinks that the database does not exist.
is the server the correct one?
is the case sensitivity set correctly? "Social_Network" and "social_network" might be considered different.
can you access the database with those parameters using a different tool (e.g. HeidiSQL, SQLYog, SQLterm, in a pinch even phpMyAdmin)?
Actually, JetBrains PHPStorm has a SQL terminal utility that can diagnose the connection. You may want to use it (once it knows what database you're connecting to, it will also warn you of several possible errors such as using the wrong table name or column name).

Using C9. Cannot connect to MySQL data base using PHP

So I'm attempting an assignment for uni through cloud9 and I am struggling to connect to my database. when I run the application this appears instead of connecting. what i see when running the application
I will post my current code, but it has all been approved by the teacher. and my code in my other .php files have been copied pasted from the teachers files (whose worked on her computer but when I cloned it (and also created a new workspace with it copied pasted) with her exact work the same picture appears.
<?php
require 'functions.php';
// Error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);
/*MySQL connection settings */
$dbhost = "localhost";
$dbuser = "bmarino";
$dbpass = "";
$dbname = "movie_rentals";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//test if connection works
if (mysqli_connect_errno()) {
die ("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno . ")"
);
}
echo "connection successful";
?>
i was not the only person in my class to have this appear, and only one student was able to connect successfully (i'm not sure what they did)
Okay I figured it out. It was because I had my .php files in a folder, and for some reason when I took them out of the folder the preview loaded!
I would delete this question but I might leave it up in case anyone else encounters the same issue :)

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

Cannot connect to MYSQL in MAMP using PHP

I just installed MAMP and have created a MYSQL database. I can access it via PHPMYADMIN.
In my php page I have this, pasted directly from the MAMP webstart page--
$user = 'root';
$password = 'root';
$db = 'local_db';
$host = 'localhost';
$port = 3306;
$link = mysql_connect(
"$host:$port",
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);
The resulting page stops at this point, won't print anything below these instructions.
I've tried changing the port in the MAMP preferences. I also included or die("Could not connect"); after the first line, but still don't get any text after the link data in the page.
I checked online, and others with the problem at least see the die text. I don't get that.
I haven't changed any passwords or data other than mess with the port number.
Any help would be appreciated!
Please give the following a try, I have developed and tested it locally, functionality within has been documented to help you understand what is going on in every step.
/**
*
* Modern method of connecting to a MySQL database and keeping it simple.
*
* If you would like to learn more about PDO,
* please visit http://php.net/manual/en/book.pdo.php
*
*/
//Set up database connection constants, so they cannot be changed.
define('DBHOST','127.0.0.1'); //Change this to the ip address of your database
define('DBNAME','test'); // Change this to the database name you are trying to connect to.
define('DBUSER','databaseuser'); // Insure this user is not the root user!!!!
define('DBPASS','databasepassword'); // Insure this is not the root password!!!!
//Let's try to connect to the database first.
try {
//Initiate a new PDO object called $MYDB and pass it the proper information to make
//the connection
$MYDB = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME."", DBUSER, DBPASS);
//If we are successful show it :D for the test page, if this is for production you should not show this.
echo "Database connection was successful.";
//If this does not worth catch the exception thrown by PDO so we can use it.
} catch(PDOException $e) {
//Show that there was an issue connecting to the database. Do not be specific because,
//user's do not need to know the specific error that is causing a problem for security
//reasons.
echo "Oh, sorry there was an issue with your request please try again.";
//Since we had an issue connecting to the database we should log it, so we can review it.
error_log("Database Error" . $e->getMessage());
}
//Since this is 100% php code we do not need to add a closing php tag
//Visit http://php.net/manual/en/language.basic-syntax.phptags.php for more information.
If you have any issues with this please attempt to break it up into smaller pieces while reviewing the PDO documentation.

Having Trouble Connecting to MySQL Database

The following code successfully connects me to my mySQL database.
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");
I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?
You'll need to change your connection information here:
mysql_connect("localhost", "root", "")
to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.
You have not mentioned anything about MySQL database hosting.
If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here
First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.
**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**
Sample db_connect file:
<?php
$hostname = 'e.g: internal-ra.db-server-123';
$username = 'e.g: serverone1234d4';
$password = 'e.g: your_own_password';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
/*** echo a message saying we have connected ***/
}
catch(PDOException $e){
echo 'Exception -> ';
var_dump($e->getMessage());
}
?>
And try to use so less as possible the key DIE()

Categories