I am using PHP to put automatically generate a Database with its tables, but for some reason I am getting an error when calling the prepare statement for table3. I have tried combing it over and re-writing it a bunch of times, but I am just not seeing what I am missing. Can you help me out?
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DB);
$table1 = 'CREATE TABLE ArtecAdmins
(
id INT NOT NULL AUTO_INCREMENT,
Username VARCHAR(160) NOT NULL,
Password VARCHAR(160) NOT NULL,
PRIMARY KEY(id)
)';
$table2 = 'CREATE TABLE ArtecRacers
(
id INT NOT NULL AUTO_INCREMENT,
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL,
Banner VARCHAR(150),
Bio TEXT(1000),
PRIMARY KEY(id)
)';
$table3 = 'CREATE TABLE Parts
(
id INT NOT NULL AUTO_INCREMENT,
sku VARCHAR(20) NOT NULL,
PRIMARY KEY(id)
)';
$table4 = 'CREATE TABLE PartsUsed
(
id INT NOT NULL AUTO_INCREMENT,
ItemID INT NOT NULL,
RacerID INT NOT NULL,
Used INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(ItemID) REFERENCES Parts(id),
FOREIGN KEY(RacerID) REFERENCES ArtecRacers(id)
)';
$makeTables = $mysqli
->prepare($table1)
->prepare($table2)
->prepare($table3) //Error happens here...
->prepare($table4)
->execute();
The Error I am receiving says that I am calling the prepare function on a non-object. I am sure it is something really simple, but I am stumped. Any help would be greatly appreciated!
you cannot use prepare like this. It is execute can be called multiple times, not prepare.
you don't need prepare here at all. just run $mysqli->query() four times
Related
I am following a codeiniter tutorial, which tells me to make a database with the following code (using mysql):
$sql = "create database login;
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ";
I try to run this, using my own php code, but i keep getting this error, and have no idea how to solve it:
Error creating table: 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 'CREATE TABLE IF NOT EXISTS `user_login` ( `id` int(11) NOT NULL AUTO_INCREM' at line 2
No idea what it could be.
create database login;
and
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
are two commands which are to be executed separately. Have the database created before you try to create the table - either via a SQL client or by executing something like:
$sql1 = 'create database login;';
$sql1->execute();
$sql2 = 'CREATE TABLE IF NOT EXISTS `user_login`...';
$sql2->execute();
I'm trying to make some SQL commands in XAMPP. My query returns following error:
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 'CREATE TABLE IF NOT EXISTS kayttajat ( id INT(10) NOT NULL
AUTO_INCREMENT, tunn' at line 2
I can't find the syntax error. And when I run the exact same command by copy-paste in command line, it works. So do I need some different syntax in PHP code?
Also if I remove the first command, the error message moves to [...] right syntax to use near 'CREATE TABLE IF NOT EXISTS rivit [...]. If I remove second command the error comes from third command and so on. I really don't understand where the error is.
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS rivit
(
id INT(10) NOT NULL AUTO_INCREMENT,
sivu INT(10) NOT NULL,
kayttaja INT(10) NOT NULL,
sana varchar(500),
kommentti varchar(1000),
aika TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
muutos TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(id)
);
CREATE TABLE IF NOT EXISTS sivut
(
id INT(10) NOT NULL AUTO_INCREMENT,
nimi varchar(32) NOT NULL,
ohje varchar(2000) NOT NULL,
salaisuus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY(id)
);
';
$mysqli->query($query) or die($mysqli->error);
The query() method is designed to execute a single query not multiple ones.
What you are looking for is multi_query() to execute multiple queries separated by a semicolon.
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
...
}
You should use multi_query if you want execute many queries with one command.
Also you don't select database which will used for insert statements.
You should add asdgfhj prefix for all tables or use USE asdgfhj after create table statement.
example with prefix:
<?php
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS asdgfhj.kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
';
$mysqli->multi_query($query) or die($mysqli->error);
example with use statement:
<?php
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
USE asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
';
$mysqli->multi_query($query) or die($mysqli->error);
I am trying to create a table in a php script and I am getting an SQL error. I can't find it or figure out how to fix it. I do have $table defined earlier in the script. Thanks in advance!
$newtable = "CREATE TABLE $table (id INT(11) NOT NULL AUTO_INCREMENT, time datetime NOT NULL,punchtype VARCHAR(255) NOT NULL,groupname VARCHAR(255) NOT NULL, dept VARCHAR(255) NOT NULL, notes VARCHAR(255))";
Error Message: Incorrect table definition; there can be only one auto column and it must be defined as a key
try this, it works
$newtable = "CREATE TABLE $table
(id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
time datetime NOT NULL,
punchtype VARCHAR(255) NOT NULL,
groupname VARCHAR(255) NOT NULL,
dept VARCHAR(255) NOT NULL,
notes VARCHAR(255))";
I'm trying to create multiple tables with a query, but it doesn't seem to work. I'm using PHP code to execute the query. Below is my PHP code, my query and my error.
My PHP code:
<?php
$conn=mysql_connect("localhost","secret","secret") or die("Kan geen verbinding maken met de DB server");
mysql_select_db("secret",$conn) or die("Kan database niet selecteren");
$query_file = 'tables.txt';
$fp = fopen($query_file, 'r');
$sql = fread($fp, filesize($query_file));
fclose($fp);
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
mysql_close($conn);
?>
My query:
CREATE TABLE pages(
id INT NOT NULL AUTO_INCREMENT,
pagid VARCHAR(32) NOT NULL,
title VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
image VARCHAR(65) NOT NULL,
youtube VARCHAR(32) NOT NULL,
primary key ( id ));
CREATE TABLE members(
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(65) NOT NULL,
password VARCHAR(65) NOT NULL,
primary key ( id ));
CREATE TABLE news(
id INT NOT NULL AUTO_INCREMENT,
pagid VARCHAR(32) NOT NULL,
title VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
image VARCHAR(150) NOT NULL,
youtube VARCHAR(32) NOT NULL,
date VARCHAR(32) NOT NULL,
primary key ( id ));
CREATE TABLE gallery(
id INT NOT NULL AUTO_INCREMENT,
image VARCHAR(65) NOT NULL,
title VARCHAR(65) NOT NULL,
description TEXT NOT NULL,
url VARCHAR(200) NOT NULL,
category VARCHAR(65) NOT NULL,
primary key ( id ));
My error:
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 'CREATE TABLE members( id INT NOT NULL AUTO_INCREMENT, username VARCHA' at line 10
Does anyone have any idea what I'm doing wrong here?
Try this:
$q1 = "CREATE TABLE pages(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
pagid VARCHAR(32) NOT NULL,
title VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
image VARCHAR(65) NOT NULL,
youtube VARCHAR(32) NOT NULL)";
$q2 = "CREATE TABLE members(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(65) NOT NULL,
password VARCHAR(65) NOT NULL)";
mysql_query($q1);
mysql_query($q2);
If you're using mysql_query, it only supports one query at a time.
However, I would recommend you avoid using mysql_* functionality as its depreciated. Try mysqli or PDO
You're propably using mysql_query. The documentation explicitly states that multiple queries are not supported. As each of your create statements is an own query, you'll have to split up your query string.
I'm having trouble inserting image data into my database. I have a table called images. When dumped with PHPMyAdmin it looks like this:
CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL,
`orig_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`hash` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
`filename` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
`uploaded` datetime NOT NULL,
`views` int(11) NOT NULL DEFAULT '0',
`album_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`server_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
KEY `server_id` (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This is the code I'm using to insert rows:
// Database connection
$db = new PDO('mysql:host=localhost;dbname=testdb', 'root', '');
// some code...
$st = $db->prepare('INSERT INTO `images` (orig_name, hash, filename, uploaded, server_id)
VALUES (?, ?, ?, ?, (SELECT `id` FROM `servers` WHERE `name` = ?))');
$st->execute(array($origName, $fileHash, $filename, date('c'), $server));
// more code...
// Database cleanup
$st = null;
$db = null;
The script returns no errors, and works flawlessly for the first row inserted. If the script runs again, it fails to insert any more rows in the images table. I see no reason why it'd behave like this, the data going into each field is unique each time (except for the server_id field).
Your id field isn't set to auto_increment.
The first record that you post will be added, with a NULL as id; the second record won't be added because there's already a record with NULL as the primary key, so it'll fail - you don't have any error checking in the code, so it won't be printing out the errors it's getting back.