-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbconnect.php
120 lines (97 loc) · 3.11 KB
/
dbconnect.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
# ------------------------------------------------------
# eBayLister - An eBay Listing Creator
# Copyright © 2021 David Rodgers
# Released under the terms of the MIT License
# ------------------------------------------------------
*/
// Debug
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
// Default Time Zone
date_default_timezone_set("America/New_York");
// SQL Connection
$dbhost = "localhost"; // Database server IP
$dbuser = "root"; // Database user name
$dbpass = "root"; // Database user password
$dbname = "apps_ebaylister"; // Database for this app
$dsn = "mysql:host=$dbhost;dbname=$dbname";
$options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
$pdo_conn = new PDO($dsn, $dbuser, $dbpass, $options);
// Connect to the database
function connect($dbuser, $dbpass) {
global $pdo_conn;
$pdo_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo_conn;
}
// Test the database connection
function testConnect()
{
global $dbuser, $dbpass;
if ( connect($dbuser, $dbpass) ) {
return true;
} else {
return false;
}
}
// Get the last ID in the listings table
function getLastID() {
global $pdo_conn;
try {
$statement = "SELECT id FROM listings ORDER BY id DESC LIMIT 1";
$pdo_statement = $pdo_conn->prepare($statement);
$pdo_statement->execute();
$last_id = $pdo_statement->fetch();
return $last_id[0];
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
// Get the next unused ID in the listings table
function getNextID() {
global $pdo_conn;
try {
$statement = "SELECT id FROM listings ORDER BY id DESC LIMIT 1";
$pdo_statement = $pdo_conn->prepare($statement);
$pdo_statement->execute();
$last_id = $pdo_statement->fetch();
return $last_id[0] + 1;
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
// Get the Auto Increment index in the listings table
function getAutoID() {
global $pdo_conn;
try {
$statement = "SELECT `AUTO_INCREMENT`FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'apps_ebaylister' AND TABLE_NAME = 'listings'";
$pdo_statement = $pdo_conn->prepare($statement);
$pdo_statement->execute();
$auto_id = $pdo_statement->fetch();
return $auto_id[0];
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
// Check if ID exists in listings
function idExists($id) {
global $pdo_conn;
try {
$stmt = $pdo_conn->prepare('SELECT * FROM listings WHERE ID=?');
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count == 0)
{
//return $count;
return 0;
} else {
return 1;
}
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>