0

I'm trying to create a database using a PHP page loaded on localhost on my PC using the following code:

// Connect to MySQL
$linkdb = mysql_connect('localhost', 'root', '***********');
if (!$linkdb) {
    die('Could not connect: '.mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('movie_db', $linkdb);
if (!$db_selected) {
    // If we couldn't, then it either doesn't exist, or we can't see it.
    $sql = 'CREATE DATABASE movie_db';
    if (mysql_query($sql, $linkdb)) {
        echo "Database movie_db created successfully\n";
    } else {
        echo 'Error creating database: '.mysql_error()."\n";
    }
}
mysql_close($linkdb);

As I loaded the page on the browser the first time at

localhost/~"my name"/

I got this

Database movie_db created successfully

meaning that the database was created. The problem is that I can't find the database anywhere. When should it be stored? Is there anyway to save it in the same folder of the .php file?

Oiproks
  • 764
  • 1
  • 9
  • 34
  • Normally MySQL stores its files at a predefined location that depends on your operating system (Windows, Linux, ...). [This question](http://stackoverflow.com/q/2091350/5830574) gives useful hints on this. In short: it _is_ possible to store the files in the same folder as your .php files but definitely not recommended. – PerlDuck Mar 05 '16 at 19:32
  • Thanks for the answer. I see why is not recommended, but I'm gonna put this script on a server online and I will need to download that database file. It would be easier if it was placed in the same folder as the php. – Oiproks Mar 05 '16 at 19:34
  • I see. But if you want to _backup_ your DB then [mysqldump](http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html) is your friend. Google for it to see numerous examples how to use it. It's really simple. – PerlDuck Mar 05 '16 at 19:48
  • 1
    Use a tool like MySQL Workbench, HeidiSQL or phpMyAdmin to export and import a MySQL-database – Paul Spiegel Mar 05 '16 at 19:49
  • Thanks @PerlDog I'm gonna take a look at that after dinner. – Oiproks Mar 05 '16 at 19:56
  • @PaulSpiegel, as I explained to the guy who answered below, I'm not gonna interact with the server at all. I'm just gonna put the file up there, call the php page from my computer, the page will generate a db with certain data in a certain position, I will download the db from that position. – Oiproks Mar 05 '16 at 19:57
  • Why don't you just run your creation script on the remote (online) server? – Paul Spiegel Mar 05 '16 at 20:01
  • That is what I will do when the entire script will be complete. Now I'm just testing it in local, and I was trying to understand some things. – Oiproks Mar 05 '16 at 20:03
  • Just to get it right: The _only_ way you can interact with your website is by uploading a .php script (plus .css etc.) and then call that script via your browser? But – enjoy your meal! (as I will now) – PerlDuck Mar 05 '16 at 20:07
  • It is not a website. Long story short: I made an Android app which collects data from several websites parsing their html codes. I decided to reduce the connection to 1. I made a php script, set in php file w/o html or css, which collects all the parts I need and `echo`them. Still I will have to parse on my app. I was thinking to reduce the load storing all the important parts in a small db which my app will. Does that sound crazy/ridiculous/unreachable? – Oiproks Mar 05 '16 at 20:22

2 Answers2

2

Even though your question refers to MySQL, I'd like to suggest SQLite which seems to fit your use-case better. It stores the database in a single file and can be accessed directly through sqlite_xxx() functions or via PDO.

Here's some example code of what that would look like:

$folder = 'sqlite:/<PATH>/movie_db.sqlite';
$dbh  = new PDO($folder);
$sql =  "SELECT * FROM movies WHERE name LIKE '%love%'";

foreach ($dbh->query($sql) as $row)
    echo $row[0];

You can also find out more about SQLite at: https://www.sqlite.org/quickstart.html

avip
  • 1,445
  • 13
  • 14
0

The database is created on the MySQL server.

With PHP, you can connect to and manipulate databases.

MySQL is the most popular database system used with PHP.

What is MySQL? MySQL is a database system used on the web MySQL is a database system that runs on a server MySQL is ideal for both small and large applications MySQL is very fast, reliable, and easy to use MySQL uses standard SQL MySQL compiles on a number of platforms MySQL is free to download and use MySQL is developed, distributed, and supported by Oracle Corporation MySQL is named after co-founder Monty Widenius's daughter: My The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.

Databases are useful for storing information categorically.

Here you can learn more about the PHP and MySQL basics: http://www.w3schools.com/php/php_mysql_intro.asp

Milan
  • 121
  • 5
  • Ok. Thanks for that. Is there any way to materially know the location of the file, or better save it in a certain folder? – Oiproks Mar 05 '16 at 19:45
  • You can use phpMyAdmin to access, browse and backup the database. – Milan Mar 05 '16 at 19:52
  • I'm not gonna interact with the server at all. I'm just put the file up there, call the php page from my computer, the page will generate a db with certain data in a certain position, I will download the db from that position. – Oiproks Mar 05 '16 at 19:55
  • basically you can not download the database from the server, it's not a single file, it's more like a part of the mysql server. but, phpMyAdmin allows you to login into the mysql server and dump a database as a single file to your local folder. – Milan Mar 05 '16 at 20:09