Database class - Table exists? - php

I am using the following awesome, easy & lightweight database class: https://codeshack.io/super-fast-php-mysql-database-class/
My problem is I do not know how I can figure out if a table in the database exists or not. I have the following PHP Code:
function addSts($database, $brow, $vers, $pag, $lang) {
$tablename = "sts" . $pag;
$stsinsert = $database->query('INSERT INTO ' . $tablename . '(id, browser, version, language, date) VALUES (NULL, ?, ?, ?, current_timestamp())', $brow, $vers, $lang);
if ($stsinsert->affectedRows()) {
echo "TABLE EXISTS";
$database->close();
}
else {
echo "TABLE DOES NOT EXISTS -> CREATE TABLE";
$pagecreation = $database->query('CREATE TABLE ' . $tablename . ' (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `browser` VARCHAR(20) NOT NULL, `version` VARCHAR(10) NOT NULL, `language` VARCHAR(5) NOT NULL, `date` TIMESTAMP NOT NULL DEFAULT CURRENT_DATE(), PRIMARY KEY (`id`))');
if ($pagecreation) {
addSts($brow, $vers, $pag, $lang);
}
}
}
It always throws the following error: Unable to prepare MySQL statement (check your syntax) - Table 'testdb.ststest' doesn't exist
So and here we have the salad. It throws the error and does not go further to the if-else part. SO every time the table does not exist the program stops working.
Hope somebody can help me out.
Thanks in advance.

If you can, use the information_schema DB and query TABLES tables
select * from tables where TABLE_SCHEMA like '<database name>'
e.g. select * from tables where TABLE_SCHEMA like 'mydbdev'
the simply iterate through the results OR
select * from tables where TABLE_SCHEMA like '<database name>' AND TABLE_NAME like '<table name>';
and count the rows (should be 0 if not present or 1 if it is).

As #Barmar mentioned in the comments, you can use try/catch statements to do this.
function addSts($database, $brow, $vers, $pag, $lang) {
$tablename = "sts" . $pag;
try {
// try to insert first
$stsinsert = $database->query('INSERT INTO ' . $tablename . '(id, browser, version, language, date) VALUES (NULL, ?, ?, ?, current_timestamp())', $brow, $vers, $lang);
if ($stsinsert->affectedRows()) {
echo "TABLE EXISTS";
$database->close();
}
}
catch (\Exception $e){
echo "TABLE DOES NOT EXISTS -> CREATE TABLE";
$pagecreation = $database->query('CREATE TABLE ' . $tablename . ' (`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `browser` VARCHAR(20) NOT NULL, `version` VARCHAR(10) NOT NULL, `language` VARCHAR(5) NOT NULL, `date` TIMESTAMP NOT NULL DEFAULT CURRENT_DATE(), PRIMARY KEY (`id`))');
if ($pagecreation) {
// call the function to insert data
addSts($brow, $vers, $pag, $lang);
}
}
}

Related

Syntax error in MySQL INSERT INTO statement [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 3 years ago.
there is some problem with my code. Everytime I try to insert something into the database, I get the syntax error.
Here is my database structure:
CREATE TABLE `notes` (
`id` int(12) NOT NULL,
`type` varchar(15) NOT NULL,
`title` varchar(43) NOT NULL,
`text` varchar(43) NOT NULL,
`group` varchar(32) NOT NULL,
`uid` int(64) NOT NULL,
`creator` int(64) NOT NULL,
`insert_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `notes`
ADD PRIMARY KEY (`id`);
ALTER TABLE `notes`
MODIFY `id` int(12) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=79;
And thats my code
<?php
session_start();
require '../config.php';
$notetype = $_POST['type'];
$notetitle = $_POST['title'];
$notetext = $_POST['text'];
$notegroup = $_POST['group'];
$noteuid = $_POST['uid'];
$notecreator = $_POST['creator'];
$notetbname = $note['tbname'];
$conn = new mysqli($databaseconfig['ip'], $databaseconfig['user'], $databaseconfig['pass'], $databaseconfig['dbname']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO $notetbname (type, title, text, group, uid, creator)
VALUES ('$notetype', '$notetitle', '$notetext', '$notegroup', $noteuid, $notecreator);";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This is what I get as error message:
Error: INSERT INTO notes (type, title, text, group, uid, creator) VALUES ('player', 'Hello there', 'Good morning everybody', 'Cop', 3325, 103);
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group, uid, creator) VALUES ('player', 'Hello there', 'Good morning everybody'' at line 1
This is because group is a mysql's reserved word.
change the fieldname or try this (notice the backtick " ` " before and after the word group:
$sql = "INSERT INTO $notetbname (type, title, text, `group`, uid, creator)
VALUES ('$notetype', '$notetitle', '$notetext', '$notegroup', $noteuid, $notecreator);";
Here you can find a list of all reserved word (mysql 5.5)
https://dev.mysql.com/doc/refman/5.5/en/keywords.html#keywords-5-5-detailed-G

How to create multiple MySQL tables via PHP using a single query?

I am trying to create a "setup script" for my website. I would like to create the database, adding tables and some content at the same time. So far this is how I did it, but it seems kind off messy using multiple queries:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE MYDB";
if ($conn->query($sql) === TRUE) {
echo "1. Database created successfully <br/>";
$conn->select_db("MYDB");
$sql_members = "CREATE TABLE MEMBERS (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
USERNAME VARCHAR(30) NOT NULL,
EMAIL VARCHAR(40) NOT NULL,
DISCOUNT VARCHAR(5),
PASSW CHAR(128),
ROLE VARCHAR(9)
)";
if ($conn->query($sql_members) === TRUE) {
echo "2. Table MEMBERS created successfully <br/>";
} else {
echo "Error creating table: " . $conn->error;
}
$sql_content = "CREATE TABLE CONTENT (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
TITLE VARCHAR(30) NOT NULL,
TEXT VARCHAR(30) NOT NULL
)";
if ($conn->query($sql_content) === TRUE) {
echo "3. Table CONTENT created successfully <br/>";
} else {
echo "Error creating table: " . $conn->error;
}
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Is there a better way?
Thanks!
== UPDATE ==
I have tried to export the database and use the resulted .sql file as my setup query, but something is wrong, I get:
Error creating tables: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'INSERT INTO CONTACTS (ID, NAME, PHONE,
EMAIL, ADDRESS, CITY, `COUN' at line 12
CREATE TABLE IF NOT EXISTS `CONTACTS` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(25) COLLATE utf8_romanian_ci NOT NULL,
`PHONE` varchar(16) COLLATE utf8_romanian_ci NOT NULL,
`EMAIL` varchar(35) COLLATE utf8_romanian_ci NOT NULL,
`ADDRESS` text COLLATE utf8_romanian_ci NOT NULL,
`CITY` varchar(16) COLLATE utf8_romanian_ci NOT NULL,
`COUNTRY` varchar(16) COLLATE utf8_romanian_ci NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_romanian_ci AUTO_INCREMENT=2 ;
INSERT INTO `CONTACTS` (`ID`, `NAME`, `PHONE`, `EMAIL`, `ADDRESS`, `CITY`, `COUNTRY`) VALUES
(1, 'Peter Brown', '0742062307', 'office#shop.com', 'Avenue 13.', 'Santaclaus', 'Austria');
== SOLUTUION ==
I needed "multi_query()" for executing my multiple queries.
You can try this too :p
$errors = [];
$table1 = "CREATE TABLE MEMBERS (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
USERNAME VARCHAR(30) NOT NULL,
EMAIL VARCHAR(40) NOT NULL,
DISCOUNT VARCHAR(5),
PASSW CHAR(128),
ROLE VARCHAR(9)
)";
$table2 = "CREATE TABLE CONTENT (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
TITLE VARCHAR(30) NOT NULL,
TEXT VARCHAR(30) NOT NULL
)";
$tables = [$table1, $table2];
foreach($tables as $k => $sql){
$query = #$conn->query($sql);
if(!$query)
$errors[] = "Table $k : Creation failed ($conn->error)";
else
$errors[] = "Table $k : Creation done";
}
foreach($errors as $msg) {
echo "$msg <br>";
}
You could export the whole database including all tables using the command line or using PhPMyAdmin. Then query the content of the file in php to create the database.
you can create a file and put all your sql queries in it..
CREATE TABLE MEMBERS (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
USERNAME VARCHAR(30) NOT NULL,
EMAIL VARCHAR(40) NOT NULL,
DISCOUNT VARCHAR(5),
PASSW CHAR(128),
ROLE VARCHAR(9)
);
CREATE TABLE CONTENT (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
TITLE VARCHAR(30) NOT NULL,
TEXT VARCHAR(30) NOT NULL
);
then in your php code:
$query = file_get_contents ('queries.sql');
if ($conn->query($query) === TRUE) {
echo "all tables created successfully <br/>";
} else {
echo "Error creating tables: " . $conn->error;
}

How to check if column 1 has two different values for column 2

I am trying to make a INSERT by UNIQUE and I am getting stuck, What I want too know is how I would do is make it so Column A is ALWAYS UNIQUE unless Column C has two different values and then it will insert into the database a second row for Column A where Column C is different.
a|b|c
-----
1|2|3
2|4|5
1|6|7
So I am currently using this sql table
CREATE TABLE `Player` (
`id` int(11) NOT NULL auto_increment,
`playername` varchar(255) NOT NULL,
`SteamID` varchar(255) NOT NULL UNIQUE,
`position` varchar(255) NOT NULL,
`lastlogin` varchar(255) NOT NULL,
`approved` varchar(255) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
With this query
$sql1= "INSERT IGNORE INTO Player (playername, SteamID, position, lastlogin, approved) VALUES ('$name', '$id', '$position', '$lastlogin', '$approve')";
It inserts the way I want so there is only 1 of each SteamID but it also will not insert if the same SteamID has two values for position.
EDIT:
What I have is a xml file I load up and insert data into the database based on the data in the xml file, What I need is below.
$xml = simplexml_load_file("players.xml");
foreach($xml->children() as $player)
{
$id = $player->attributes()->id;
$profile = new SteamProfile($id);
$name = mysql_real_escape_string($profile->getUsername());
$lastlogin = $player->attributes()->lastlogin;
$position = $player->lpblock->attributes()->pos;
$sql1= "INSERT IGNORE INTO Player (playername, SteamID, position, lastlogin, approved) VALUES ('$name', '$id', '$position', '$lastlogin', '$approve')";
if (!mysql_query($sql1,$connection))
{
die('Insert Error: ' . mysql_error());
}
}
so the xml file gets read and then the data in the file gets inserted into the database. sometimes the xml file would contain
<player id="76561197961716203" lastlogin="8/22/2014 10:49:28 PM">
<acl id="76561197961543041"/>
<acl id="76561197988417990"/>
<lpblock pos="273,93,-102"/>
<lpblock pos="1322,62,-1711"/>
</player>
at which point I would need a second row created for the second <lpblock pos=
So its currently inserting
|60|SwordFish |76561197961716203|273,93,-102|8/22/2014 10:49:28 PM|Yes|2014-08-24 15:28:16|
and it should be inserting
|60|SwordFish |76561197961716203|273,93,-102|8/22/2014 10:49:28 PM|Yes|2014-08-24 15:28:16|
|61|SwordFish |76561197961716203|1322,62,-1711|8/22/2014 10:49:28 PM|Yes|2014-08-24 15:28:16|
I'm not sure what you are trying to achieve, but you can set a UNIQUE restraint on a combination of columns, in your case SteamID and position:
CREATE TABLE `Player` (
`id` int(11) NOT NULL auto_increment,
`playername` varchar(255) NOT NULL,
`SteamID` varchar(255) NOT NULL,
`position` varchar(255) NOT NULL,
`lastlogin` varchar(255) NOT NULL,
`approved` varchar(255) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_combination_of_two_fields` (`SteamID`,`position`)
) ENGINE=InnoDB;
Thanks for you help Jeroen I did this and its working.
foreach($player->children() as $lpblock)
{
$position = $lpblock->attributes()->pos;
if(!empty($position))
{
$sql1= "INSERT IGNORE INTO Player (playername, SteamID, position, lastlogin, approved) VALUES ('$name', '$id', '$position', '$lastlogin', '$approve')";
if (!mysql_query($sql1,$connection))
{
die('Insert Error: ' . mysql_error());
}
}
}
However I would like all of the SteamID's inserted and not just the ones with a $position set. When I take this out and try it adds the correct rows but also adds another row with a blank $position as if there is a SteamID that's blank when there is none.
if(!empty($position)){
}

PHP PDO mySQL query create table if not exist

Using PHP PDO query to execute a mySQL query. The query is made up of a multitude of information inputted from foreach(); so I have echo'd out the sql query. The problem lies here but I cannot see it.
This is the output of $sql
CREATE TABLE IF NOT EXISTS `page` (
`page_ID` INT AUTO_INCREMENT NOT NULL,
`url` varchar(200) NOT NULL,
`title` varchar(200),
`subtitle` TEXT,
`content` TEXT,
`parent` varchar(10),
`privacy` varchar(1),
`status` varchar(1),
`creation` varchar(30)
) CHARACTER SET utf8 COLLATE utf8_general_ci;
FYI the query is executed like this:
function createdbtable($table,$fields){
global $fsdbh;
$sql = "CREATE TABLE IF NOT EXISTS `$table` (";
foreach($fields as $field => $type){ $sql.= "`$field` $type,"; }
$sql = rtrim($sql,',');
$sql .= ") CHARACTER SET utf8 COLLATE utf8_general_ci"; return $sql;
if($fsdbh->exec($sql) !== false) { return 1; }
}
This is the error:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
You forgot the primary key:
CREATE TABLE IF NOT EXISTS `page` (
`page_ID` INT AUTO_INCREMENT NOT NULL,
`url` varchar(200) NOT NULL,
`title` varchar(200),
`subtitle` TEXT,
`content` TEXT,
`parent` varchar(10),
`privacy` varchar(1),
`status` varchar(1),
`creation` varchar(30),
PRIMARY KEY (`page_ID`))
CHARACTER SET utf8 COLLATE utf8_general_ci
Error was quite explicit:
Schema Creation Failed: Incorrect table definition; there can be only one auto column and it must be defined as a key
You must specify the auto increment key as key.
edit:
And for the PHP code, I will go to something like that:
function createdbtable($table,$fields)
{
global $fsdbh;
$sql = "CREATE TABLE IF NOT EXISTS `$table` (";
$pk = '';
foreach($fields as $field => $type)
{
$sql.= "`$field` $type,";
if (preg_match('/AUTO_INCREMENT/i', $type))
{
$pk = $field;
}
}
$sql = rtrim($sql,',') . ', PRIMARY KEY (`'.$pk.'`)';
$sql .= ") CHARACTER SET utf8 COLLATE utf8_general_ci";
if($fsdbh->exec($sql) !== false) { return 1; }
}

mysql_insert_id() 0, but insert is done successfully

this is table schema:
CREATE TABLE `USERS` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(30) DEFAULT '',
`first_name` varchar(15) DEFAULT '',
`last_name` varchar(15) DEFAULT '',
`password` varchar(15) DEFAULT '',
`gender` int(1) DEFAULT '-1',
PRIMARY KEY (`id`)
)
in php:
$sql = 'INSERT INTO Users (email, first_Name, last_Name, password, gender ) VALUES ("'.$email.'", "'.$first_name.'", "'.$last_name.'", "'.$password.'", '.$gender.')';
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->execute();
//$user = $stmt->fetchObject();
echo 'last id was '.mysql_insert_id();
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
I can't figure out why mysql_insert_id() returns 0. there are no other processes running. id is set to auto increment. Insert is done and seen in db.
You are using PDO to interface with the database. Use PDO::lastInsertId() to get the last inserted id.
$stmt->execute();
echo 'last id was ' . $db->lastInsertId();
mysql_insert_id() is part of the ext/mysql extension. You cannot mix-match functions from both extensions to interface with the same connection.

Categories