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?

10
  • Normally MySQL stores its files at a predefined location that depends on your operating system (Windows, Linux, ...). This question 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. Commented Mar 5, 2016 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. Commented Mar 5, 2016 at 19:34
  • I see. But if you want to backup your DB then mysqldump is your friend. Google for it to see numerous examples how to use it. It's really simple. Commented Mar 5, 2016 at 19:48
  • 1
    Use a tool like MySQL Workbench, HeidiSQL or phpMyAdmin to export and import a MySQL-database Commented Mar 5, 2016 at 19:49
  • Thanks @PerlDog I'm gonna take a look at that after dinner. Commented Mar 5, 2016 at 19:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

This may be a good solution. Do you have any knowledge about if Android has support for SQLite?
I was able to find this: SQLiteDatabase
Thanks a thousand. I'm gonna go with this.
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

4 Comments

Ok. Thanks for that. Is there any way to materially know the location of the file, or better save it in a certain folder?
You can use phpMyAdmin to access, browse and backup the database.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.