-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.php
51 lines (40 loc) · 1.47 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class createConnection {
// if used localhost on cli, this error is thrown. PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /opt/lampp/htdocs/Song Downloader/connect.php
var $dbhost = "127.0.0.1";
var $dbuser = "root";
var $dbpass = "";
var $db = "songdata";
var $my_conn;
// function to connect to db named "songdata"
function connectToDatabase() {
// sudo apt-get install mysqli
$conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->db);
if (!$conn) {
die("Connect failed: %s\n" . $conn->error);
} else {
$this->my_conn = $conn;
echo "\n\nConnection established \n";
}
return $this->my_conn;
echo "returned";
}
function closeConnection() {
mysqli_close($this->my_conn);
echo "Connection closed";
}
}
// saving data to database into table "song_info"
function savingToDb($video_title, $video_duration, $video_url, $songs_path) {
// connection to database using class
$connection = new createConnection();
$my_conn = $connection->connectToDatabase();
$sql = "INSERT INTO songs_info (Name, Duration, URL, Path )
VALUES ('$video_title','$video_duration' ,'$video_url', '$songs_path')";
if ($my_conn->query($sql) === TRUE) {
echo "Data has been successfully stored in Database.";
} else {
echo "Error: " . $sql . "<br>" . $my_conn->error;
}
}
?>