MySQL table not creating when using php - php

So, I'm trying to create a table via php.Below given is the part of code:
<?
# db configurations
define('DB_HOST', '127.0.0.1:3306');
define('DB_USER', 'root');
define('DB_PASS', 'PWREMOVED');
define('DB_NAME', 'poll2');
# db connect
function dbConnect($close=true){
global $link;
if (!$close) {
mysql_close($link);
return true;
}
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to MySQL DB ') . mysql_error();
if (!mysql_select_db(DB_NAME, $link))
return false;
}
$sql=CREATE TABLE `voting` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`fyrir` varchar(45) NOT NULL,
`more` text NOT NULL,
`vote` int(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
?>
When running this nothing happens to my database.

You are not executing the mysql query using mysql_query(). The query will get executed/runned only if you use this function. Also you have to enclose the query within quotes.
$sql="CREATE TABLE `voting` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`fyrir` varchar(45) NOT NULL,
`more` text NOT NULL,
`vote` int(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)";
mysql_query($sql);

After adding the $sql variable, you have to call dbConnection inside the file to open the database connection and then you add: mysql_query($sql). That means:
<?php
# db configurations
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'PWREMOVED');
define('DB_NAME', 'poll2');
# db connect
function dbConnect(){
global $link;
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to MySQL DB ') . mysql_error();
if (!mysql_select_db(DB_NAME, $link))
return false;
}
$sql="CREATE TABLE `voting` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`fyrir` varchar(45) NOT NULL,
`more` text NOT NULL,
`vote` int(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)";
/* call dbConnect to open db connectioin */
dbConnect();
/* Execute the query */
if(!mysql_query($sql))
die(mysql_error());
else
echo 'table creation successful';
?>
But, why don't you use OOP? It will make things easier.

You are not executing query and query string should be wrapped in " Try this code " added:
$sql="CREATE TABLE `voting` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`fyrir` varchar(45) NOT NULL,
`more` text NOT NULL,
`vote` int(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)";
Executing mysql query: mysql_query($sql) or die(mysql_error());
You forgot to Call dbConnect function to connect to mysql: dbConnect();

Related

Error Creating a database using 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

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();
?>

Simple MySQL Query Showing Error

I am trying to execute the following query
DROP TABLE IF EXISTS `developer_messenger`;
CREATE TABLE `developer_messenger` (
`id` int(10) NOT NULL,
`title` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`message` varchar(45) NOT NULL,
`type` varchar(45) NOT NULL,
`date_time` varchar(45) NOT NULL,
`status` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
But this simple query is showing me Error in PHP
Could not get data: 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 developer_messenger ( id int(10)
NOT NULL, title varchar(' at line 1
I am a newbie, Sorry if its silly,
Help Appreciated!
In Response to your second question about magic_quotes:
if (!get_magic_quotes_gpc()) { //checks php ini if magic_quotes is not on
$title = addslashes($_POST['title']);
}
else
{
$title = stripslashes($_POST['title']);
}
addslashes(); http://php.net/manual/en/function.addslashes.php
stripslashes(); http://php.net/manual/en/function.stripslashes.php
not sure if this helps but it might that way you dont have to change php.ini
just the $data that is to be entered into sql
Your query is correct, Try exciting DROP and CREATE one by one.
This code may help you.
<?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 `developer_messenger` (
`id` int(10) NOT NULL,
`title` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`message` varchar(45) NOT NULL,
`type` varchar(45) NOT NULL,
`date_time` varchar(45) NOT NULL,
`status` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>

Mysql query create table does not works by php

I'm trying to copy 2 tables structures into a new database.
I use a function that makes by herself the SQL command: When i execute the code on phpmyadmin the code gets executed but when I execute this by PHP, it doesn't execute.
How is possible?
The sql command is this:
CREATE TABLE `tabella_1` (
`campo1` int(11) NOT NULL AUTO_INCREMENT,
`campo2` varchar(100) COLLATE latin1_general_ci NOT NULL,
`campo_3` int(11) NOT NULL,
PRIMARY KEY (`campo1`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE `tabella_2` (
`campo1` int(11) NOT NULL DEFAULT '0',
`campo2` varchar(100) COLLATE latin1_general_ci NOT NULL,
`campo_3` int(11) NOT NULL,
PRIMARY KEY (`campo1`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Thanks
Update this code with your database connection details:
<?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 `tabella_1` ( `campo1` int(11) NOT NULL AUTO_INCREMENT, `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL, `campo_3` int(11) NOT NULL, PRIMARY KEY (`campo1`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci";
if ($conn->query($sql) === TRUE) {
echo "Table 1 created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// sql to create table
$sql2 = "CREATE TABLE `tabella_2` ( `campo1` int(11) NOT NULL DEFAULT '0', `campo2` varchar(100) COLLATE latin1_general_ci NOT NULL, `campo_3` int(11) NOT NULL, PRIMARY KEY (`campo1`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci";
if ($conn->query($sql2) === TRUE) {
echo "Table 2 created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
I got it!
I putted in a array() the functions and it works!
Tkanks to everybody!

Create new SQL table on form submit IF TABLE NOT EXIST

I am new to php and SQL so this is probably an easy question but I could not find any good sources online.
I am trying to create a SQL table when someone submits a form and this is what I have so far
include("dbstufflive.php");
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
or die("Couldn't connect to server");
$sql = "CREATE TABLE IF NOT EXISTS `$company` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`company_name` varchar(80) NOT NULL,
`contact` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`phone` varchar(13) NOT NULL,
... (long list of table data)
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ";
mysqli_query($cxn,$sql);
I will of course be doing other stuff with this table but I think I have most of that under control.
The problem is that this statement does not actually create my table :( The SQL statement works in phpadmin when I enter it as is and also there are no errors when the script runs. So it goes through all of this, and more, and seems to work but the table simply doesn't appear.
I can supply more code if needed but I don't want to paste more code here than is necessary.
Thanks in advance for any help from the community.
EDIT:
I was using wrong DBinfo...wow, I am not very bright.
Your SQL Statement looks fine - from the looks of it, you are missing your login credentials. An efficient way to do so:
// Add this line
require_once('config.php');
// Then change the variables below to pull your credentials from that file.
$cxn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD)
or die("Couldn't connect to server");
$sql = "CREATE TABLE IF NOT EXISTS `$company` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`company_name` varchar(80) NOT NULL,
`contact` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`phone` varchar(13) NOT NULL,
... (long list of table data)
`description` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ";
mysqli_query($cxn,$sql);
Then create a new file called config.php in same directory. Put your credentials:
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASSWORD', 'your_password');
define('DB_DATABASE', 'database_name');
?>
You should check the result of mysqli_query (as far as i know it returns false on failure). See mysqli documentation for details, where you can read, that "Create table doesn't return a resultset" but True/false on sucess/failure.
example:
if (!mysqli->query($cxn,$sql)) {
printf("Error: %s\n", mysqli_error($cxn));
}

Categories