Error Creating a database using PHP - php

I'm trying to create a database with a php file but I don't know why when i run it, it displays this error: "Error: CREATE TABLE contaclick ( id int(20) NOT NULL auto_increment, link varchar(255) NOT NULL default '', count int(20) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM;"
Someone knows how to fix it?
my code in php is this:
<?
$login = "xxx";
$password = "xxx";
$database = "xxx";
$db = mysql_connect("xxx", $login, $password) or die ("Errore!");
mysql_select_db($database, $db);
$sql = "CREATE TABLE contaclick (
id int(20) NOT NULL auto_increment,
link varchar(255) NOT NULL default '',
count int(20) NOT NULL default '0',
PRIMARY KEY (id)
) TYPE=MyISAM;";
#mysql_query($sql) or die("Errore: $sql");
$sql = "INSERT INTO contaclick VALUES('', 'https://www.mywebsite.com/', '0')";
#mysql_query($sql) or die("Errore: $sql");
?>

TYPE is deprecated, use ENGINE instead
CREATE TABLE contaclick ( id int(20) NOT NULL auto_increment,
link varchar(255) NOT NULL default '',
count int(20) NOT NULL default '0', PRIMARY KEY (id) ) ENGINE = MyISAM

Related

Insert an PHP array into MySQL with table as variable or dynamic table

I have an php array:
$cont_array = Array("613:m-ent:id=one","930:m-lk:id=one;x=180;y=79;which=1","1080:m-lev:id=one;");
I want to insert it into a MySQL table with table-name as variable, like $table_name = "user1".
In addition, I want to break each array-element by the first ":" .After executing my php code I get just an empty table.
I need help please. This is my code:
<?php
$connection = mysqli_connect('localhost','root','','prueba');
$cont_array = Array("613:m-ent:id=one","930:m-clk:id=one;x=180;y=79;which=1","1080:m-lev:id=one;");
$table_name = "user1";
$sql_1 = "DROP TABLE IF EXISTS `".$table_name."`;";
$sql_1 .= "CREATE TABLE `".$table_name."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`usercod` varchar(20) NOT NULL DEFAULT '',
`pid` varchar(1000) NOT NULL DEFAULT '',
`name` varchar(1000) NOT NULL DEFAULT '',
`time` varchar(1000) NOT NULL DEFAULT '',
`all_act` varchar(1000) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;";
mysqli_multi_query($connection,$sql_1);
foreach ($cont_array as $row){
$break_e = explode(':', $row, 2);
$sql="INSERT INTO`".$table_name."` (usercode,pid,name,time,all_act) VALUES ("user","pid_value","user_name",'$break_e [0]','$break_e [1]');";
mysqli_query($connection,$sql);
}
?>
This is what I get:
This is what I would like to get:
Your SQL seem to have issue check below SQL statement.
$sql = "INSERT INTO $table_name (usercod,pid,name,time,all_act) VALUES ('user','pid_value','user_name','".$break_e [0]."','".$break_e [1]."')";

PHP Fatal error: Call to undefined function pdo_query()

No answers were found at google and stackoverflow
The code is as follows
<?php
pdo_query("CREATE TABLE IF NOT EXISTS `ims_cyl_vip_video` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uniacid` int(5) NOT NULL,
`title` varchar(255) NOT NULL,
`uid` varchar(25) NOT NULL,
`openid` varchar(255) NOT NULL,
`time` varchar(15) NOT NULL,
`video_url` text NOT NULL,
`share` int(3) NOT NULL,
`yvideo_url` text NOT NULL,
`type` VARCHAR(25) NOT NULL,
`index` int(2) NOT NULL,
`video_id` int(11) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `ims_cyl_vip_video_member` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uniacid` int(10) NOT NULL,
`openid` varchar(255) NOT NULL,
`uid` varchar(25) NOT NULL,
`nickname` varchar(255) NOT NULL,
`avatar` varchar(1000) NOT NULL,
`end_time` varchar(15) NOT NULL,
`is_pay` int(2) NOT NULL,
I think you are trying to create a table with PHP. Here is the simple code example from which you can create the tables: Replace the sample query with your SQL query.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>

Mysqli not finding row

i am trying to let a user login here is my login script atm im just checking if the username exists but for some reason its not finding any records even though the user name is right
<?php if(isset($_POST)){
print_r($_POST);
//Variables from the table
$usernamelogin = $_POST['usernamelogin'];
$passwordlogin = $_POST['passwordlogin'];
//Prevent MySQL Injections
$usernamelogin = stripslashes($usernamelogin);
$passwordlogin = stripslashes($passwordlogin);
$usernamelogin = mysqli_real_escape_string($con, $usernamelogin);
$passwordlogin = mysqli_real_escape_string($con, $passwordlogin);
$loginquery = mysqli_query($con,"SELECT * FROM reg_users WHERE user ='$usernamelogin' AND authorised ='1'") or die("Can not query DB.");
$logincount = mysqli_num_rows($loginquery);
if($logincount == 1){
echo "user exists";
} else {
echo "User doesnt exist";
}
}
?>
my table is called reg_users and user is the column the username goes into. i am doing the same thing on register and that works
Any ideas guys?
table is
`reg_users` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`firstname` varchar(30) DEFAULT NULL,
`surname` varchar(30) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`password` varchar(512) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`banned` int(1) DEFAULT '0',
`authorised` int(1) DEFAULT '0',
`activationcode` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;
And the sqlfiddle is here
Here is the config.php which is called in the header of the page
<?php //Information to connect to your MySQL Server AND DB
$hostdb = "localhost";
$userdb = "test_nathan";
$passworddb = "xxxxx";
$db = "test_nathan";
//Connect to MySQL Server
$con = mysqli_connect($hostdb,$userdb,$passworddb,$db) or die ("could not connect");
session_save_path('../login/sessions');
require_once('functions.php');
?>
Figured out the problem guys for some reason when i was storing the variables on escaping the string there was spaces attached to the variable. thanks for the help guys.

SQL Database trouble

I'm trying to create a database with 4 tables. When I run my code, it creates the first table but not the other 3. It's because of the second table not changing the storage engine to myISAM, but I can't figure out why it's not doing this. Here is my code:
<?php
require_once 'conn.php';
$sql= <<<EOS
CREATE TABLE IF NOT EXISTS cms_access_levels (
access_lvl tinyint(4) NOT NULL auto_increment,
access_name varchar(50) NOT NULL default'',
PRIMARY KEY(access_lvl)
)
EOS;
$result= mysql_query($sql) or
die(mysql_error());
$sql= "INSERT IGNORE INTO cms_access_levels " .
"VALUES(1, 'Users'), " .
"(2, 'Moderator'), " .
"(3, 'Administrator')";
$result= mysql_query($sql) or
die(mysql_error());
$sql= <<<EOS
CREATE TABLE IF NOT EXISTS cms_articles ENGINE = MYISAM (
article_id int(11) NOT NULL auto_increment,
author_id int(11) NOT NULL default '0',
is_published tinyint(11) NOT NULL default '0',
date_submitted datetime NOT NULL default '0000-00-00 00:00:00',
date_published datetime NOT NULL default '0000-00-00 00:00:00',
title varchar(255) NOT NULL default '',
body mediumtext NOT NULL,
PRIMARY KEY(article_id),
KEY IdxArticle(author_id, date_submitted),
FULLTEXT KEY IdxText(title, body)
)
EOS;
$result= mysql_query($sql) or
die(mysql_error());
$sql= <<<EOS
CREATE TABLE IF NOT EXISTS cms_comments (
comment_id int(11) NOT NULL auto_increment,
article_id int(11) NOT NULL default '0',
comment_date datetime NOT NULL default '0000-00-00 00:00:00',
comment_user int(11) NOT NULL default '0',
comment text NOT NULL,
PRIMARY KEY(comment_id),
KEY idxComment(article_id)
)
EOS;
$result= mysql_query($sql) or
die(mysql_error());
$sql= <<<EOS
CREATE TABLE IF NOT EXISTS cms_users (
user_id int(11) NOT NULL auto_increment,
email varchar(255) NOT NULL default '',
password varchar(50) NOT NULL default '',
name varchar(100) NOT NULL default '',
access_lvl tinyint(4) NOT NULL default '1',
PRIMARY KEY(user_id),
UNIQUE KEY uniq_email(email)
)
EOS;
$result= mysql_query($sql) or
die(mysql_error());
$adminemail= "aolle570#uwsp.edu";
$adminpass= "graysen7";
$adminname= "olle";
$sql= "INSERT INTO cms_users " .
"VALUES (NULL, '$adminemail', '$adminpass', '$adminname', 3)";
$result= mysql_query($sql) or
die(mysql_error());
echo "<html><head><title>CMS Tables Created</title></head><body>";
echo "CMS Tables created. Here is the initial login information: <br />";
echo "<ul><li><strong>Login:</strong> " . $adminemail . "</li><br />";
echo "<li><strong>Password:</strong> " . $adminpass . "</li><br />";
echo "<a href='login.php'>Login</a> to the site now.";
echo "</ul></body></html>";
?>
Any help is greatly appreciated!
As far as I know, and my database tells me I am right, engine should be mentioned after create definition. Try this:
CREATE TABLE IF NOT EXISTS cms_articles (
article_id int(11) NOT NULL auto_increment,
author_id int(11) NOT NULL default '0',
is_published tinyint(11) NOT NULL default '0',
date_submitted datetime NOT NULL default '0000-00-00 00:00:00',
date_published datetime NOT NULL default '0000-00-00 00:00:00',
title varchar(255) NOT NULL default '',
body mediumtext NOT NULL,
PRIMARY KEY(article_id),
KEY IdxArticle(author_id, date_submitted),
FULLTEXT KEY IdxText(title, body)
) ENGINE = MYISAM
You should put the ENGINE = MYISAM after the field definition's closing parenthesis.
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
shows table_options are after create definition.

SQL error when creating multiple tables

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.

Categories