Oiproks
Oiproks

Reputation: 794

Create DB in PHP and save it in localhost

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?

Upvotes: 0

Views: 1996

Answers (2)

avip
avip

Reputation: 1465

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

Upvotes: 2

Milan
Milan

Reputation: 121

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

Upvotes: 0

Related Questions