Creating database tables within Dreamweaver not working - php

I've used this script to link to my database account (1&1), using Dreamweaver, which proves to be a successful connection yet cannot seem to create tables for my database when this is linked, was just wondering if anyone has any idea in how to go about the right way with this as I can't seem to get it working.
My database connection which works.
<?php
$hostname="db************";
$database="db********";
$username="db********";
$password="*********";
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
This is the tables script i'm using which doesn't work .
<?php
include_once("php_includes/db_conx.php");
// CREATE TABLE USER
$sql= "CREATE TABLE USER (
id_user_pk INT NOT NULL AUTO_INCREMENT,
nick VARCHAR(40),
email VARCHAR(40),
password VARCHAR(20),
user_reg_date DATE,
PRIMARY KEY (id_user_pk)
) TYPE=INNODB";
mysql_query($sql);
?>

You must use "ENGINE=INNODB" instead of "TYPE=INNODB";
CREATE TABLE `user` (
`id_user_pk` int(11) NOT NULL AUTO_INCREMENT,
`nick` varchar(40) DEFAULT NULL,
`email` varchar(40) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`user_reg_date` date DEFAULT NULL,
PRIMARY KEY (`id_user_pk`)
) ENGINE=INNODB;

Related

Creating Database in APACHE2 returning internal server error

I am trying to use my pi as a webserver. When I am attempting to create a Table in my database it is returning an internal server error. Can anyone try help me diagnose whats causing it?
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'anthony';
$dbname = "messages";
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = "CREATE TABLE IF NOT EXISTS message_tbl (
message_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
message_name VARCHAR(50) NOT NULL,
message_subject VARCHAR(40) NOT NULL,
message_txt VARCHAR(500) NOT NULL,
message_amount INT(10) NOT NULL,
message_die VARCHAR(10) NOT NULL,
message_modifier INT(10) NOT NULL,
message_roll INT(10) NOT NULL
)";
mysql_select_db( 'TUTORIALS' );
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
echo "Table created successfully\n";
mysql_close($conn);
?>
I would usually atleast expect it to come back with Could not create table:. I have installed apache2, php, and mysql with a database called messages on my pi.
MySQL's default port is 3306, not 3036. But you really should scrap that obsolete code and start fresh with PDO. Something like this might work. I'm on a device using the app so probably not my best work but should at least get you pointed in the right direction.
<?php
try {
$conn = new PDO("mysql:host=localhost;dbname=TUTORIALS", "root", "anthony");
echo 'Connected successfully<br />';
$sql = "CREATE TABLE IF NOT EXISTS message_tbl (
message_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
message_name VARCHAR(50) NOT NULL,
message_subject VARCHAR(40) NOT NULL,
message_txt VARCHAR(500) NOT NULL,
message_amount INT(10) NOT NULL,
message_die VARCHAR(10) NOT NULL,
message_modifier INT(10) NOT NULL,
message_roll INT(10) NOT NULL
)";
$conn->query($sql);
echo "Table created successfully\n";
} catch (PDOException $e) {
die($e->getMessage());
}
?>

PHP PDO, connecting to database/selecting database + execute

This is my code http://prntscr.com/a2d8qq currently, I am learning things but I am really wondering why it will say that there is no database selected, tho I have selected it in line 5, also if I remove the "dbname = users_details" and then execute a query that creates a databse then it is fine. But whenever I create a table in that database (I selected it) it will not make me, I searched across google and it really is the same to my code but mine will not work.
<?php
try {
$connect = new PDO("mysql: host = 'localhost'; dbname = users_details", 'root', '');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "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
)";
$connect->exec($sqlQuery);
echo 'Successfully created table.';
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
So this fixed my problem: I have to execute a query to use a specified database into where I want my tables to be in. Then in the line 5 I have just removed the "dbname =
<?php
try {
$connect = new PDO("mysql: host = 'localhost';", 'root', '');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "CREATE TABLE details (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
$connect->exec("use users_details");
$connect->exec($sqlQuery);
echo 'Successfully created table.';
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>

mysqli_query not working in php despite correctly built query

I am trying to get what should otherwise be a simple but of php to insert sample data into a table, but something just isn't having any of it.
Table Definition:
CREATE TABLE IF NOT EXISTS teams (
token varchar(12) COLLATE utf8_unicode_ci NOT NULL,
tname varchar(48) COLLATE utf8_unicode_ci NOT NULL,
captain varchar(64) COLLATE utf8_unicode_ci NOT NULL,
email varchar(64) COLLATE utf8_unicode_ci NOT NULL,
phone varchar(14) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (token),
UNIQUE KEY name (tname),
KEY id (token)
)
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Code:
$con = mysqli_connect("localhost","username","password","database");
$tname = "Big Bang";
$cname = "Mike";
$cemail = "test#gmail.com";
$cphone = "123-456-7898";
$teamToken = strtoupper(bin2hex(mcrypt_create_iv(6, MCRYPT_DEV_URANDOM)));
$query = "INSERT INTO teams (token, tname, captain, email, phone) VALUES ('" . $teamToken . "', '" . $tname . "', '" . $cname . "', '" . $cemail . "', '" . $cphone . "')";
if (mysqli_query($con, $query))
{
echo "Pass!";
}
else
{
echo $query;
}
mysqli_close($con);
What's odd is the php echos the query, because the mysqli_query result is false, yet the echoed query, when copied and pasted right into phpMyAdmin's terminal, works fine.
I am at my qit's end.
Your Code:
$con = mysqli_connect("localhost","username","password","database");
Edited code:
$con = mysqli_connect("localhost","root","","database");
if(!$con):
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
endif;
Its worked fine on my localhost
Maybe the datatypes of our columns make any problems.
I have once a similar "error" where my token field was to short. phpMyAdmin simply cut the long string to fit (or some thing this) and so it worked inside phpMyAdmin but not with my program.
Please post the CREATE statement of your table.
Try this
mysqli_query($con, $query) or die(mysqli_error($con));
I will suggest go step by step
step 1 : $con = mysqli_connect("localhost","username","password","database");
comment everything other than this statement.
then below write
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
verify your username password and database name (by default:username=="root" password=="" )
step 2 : add the query statements and print it. also check that all values are within their bounds as specified id DB.
step 3 :if $con and $query are valid then you will get your o/p as you req.
If still you have an error please paste your error statement.
using following to connect:
username="root" password=""
Your code works fine in my system.
I used varchar as datatype. Maybe the length of the varchar which you have taken as 12 is less to store the $teamToken variable. Try increasing the size.

PHP and MYSQL database connection and table creation only once

I'm developing a small application for my academic. i have a registration form and a log_in form. Here is what i want, i have a database configuration file which contains "connection to server, creating database and creating table queries" included in it. I've included this database file at the top of my registration form so that when the page is loaded the "query should execute and create database and table ONLY ONCE". But the problem is the query is successfully executing for the first time but when the registration page is loaded again, it prompt's an error "DATABASE ALREADY EXISTS". Please me.
I've attached the db.php code..
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$connect = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$connect) {
die('SERVER CONNECTION FAILED...\n: ' . mysql_error());
}
;
$sql = 'CREATE DATABASE USERS';
$retval = mysql_query($sql, $connect);
if (!$retval) {
die('DATABASE CREATION FAILED\n: ' . mysql_error());
}
;
$sql = "CREATE TABLE USERS( " . "Memberid int(10) NOT NULL AUTO_INCREMENT,
" . "Name varchar(100) NOT NULL,
" . "Username varchar(20) NOT NULL,
" . "Password varchar(10) NOT NULL,
" . "Email varchar(20) NOT NULL,
" . "Activation varchar(40) DEFAULT NULL,
" . "Status int(1) NOT NULL DEFAULT '1',
" . "PRIMARY KEY (`Memberid`)); ";
mysql_select_db('USERS');
$retval = mysql_query($sql, $connect);
if (!$retval) {
die('COULD NOT CREATE TABLE\n: ' . mysql_error());
}
;
mysql_close($connect);
?>
<html>
<body>
//registration form code
</body>
</html>
query to create database only once if it doesn't exists
CREATE DATABASE IF NOT EXISTS DBName;
query to create table only once if it doesn't exists
CREATE TABLE IF NOT EXISTS tablename;
You are able to create database/table once only.
Change
CREATE DATABASE
To:
CREATE DATABASE IF NOT EXISTS
Same changes should be applied for the table creation statement.
Try this change
CREATE DATABASE IF NOT EXISTS USERS
And for table
CREATE TABLE IF NOT EXISTS tablename;

Attempting to create a database and table and insert some data

This is what I have so far.... I do not understand why it is not working? Any ideas? This is just a simple script to connect to a database, create a table and insert some data. I also want to retrieve the data but I think I may be jumping a little a head.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (mysql_query("CREATE_DATABASE nogjhghkgst98", $link))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
if ($link="CREATE TABLE contactsZ8 (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))") {
echo "ineserted";
}
else
{
echo "not inserted" . mysql_error();
}
$link = "INSERT INTO contactsZ VALUES ('','John','Smith','01234 567890','00112 334455','01234 567891','johnsmith#gowansnet.com','http://www.gowansnet.com')";
$link="SELECT * FROM contactsZ";
$link=mysql_query($link);
mysql_close($link);
?>
There is definitely something wrong:
if ($link="CREATE TABLE contactsZ8 (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))") {
an assignment in an if
query not executed (that is not so bad: once the table is created, executing again the query when reloading the page will fail)
assigning to $link ! this is confusing (but should not generate any error)...
Then :
$link = "INSERT INTO contactsZ VALUES ('','John','Smith','01234 567890','00112 334455','01234 567891','johnsmith#gowansnet.com','http://www.gowansnet.com')";
The query is not executed.
Edit: the INSERT is done in contactsZ, whereas the CREATE TABLE creates contactsZ8.
Edit2: And finally:
mysql_close($link);
After re-assigning 3 times $link, $link is not the (optional, by the way) link identifier any more...

Categories