1

Probably a simple overlook, but I cannot seem to create a table with my PHP script. My first question is that I want to add a table to an existing DB. How do I tell the server which DB to create the table in? The code to create a table is pretty simple, but here it is ..

CREATE TABLE Countr(ID INT IDENTITY(1,1) PRIMARY KEY,Page VARCHAR(50), Month INT, Always INT);

Any assistance would be appreciated.

4
  • 1
    How do you connect to the database? You usually tell the system which database to connect to in your connection string. Oh - and I'm guessing your table name is a typo...! Commented Oct 24, 2012 at 13:43
  • 1
    @HelgiHrafnGunnarsson I would imagine because it's a trivial question: When you connect to make SELECT queries, you tell the server which DB you want by using mysql_select_db() (or whatever function to that effect is used in the chosen library). Commented Oct 24, 2012 at 13:53
  • use mysql_connect(host,user,pass) Commented Oct 24, 2012 at 13:53
  • @ArunKillu - did you even read any of the comments before commenting? That function doesn't select the database, it connects to the database daemon. Commented Oct 24, 2012 at 14:10

6 Answers 6

2

In your PHP server you should be connecting to your database. Your code might be like this:

mysqli_connect("sqlserver.mysite.com", "username", "password") or die("SQL Error: Cant connect to database.");

Then you should do:

mysqli_select_db("database_name") or die("SQL Error: Cant select database"); 

to select the database before performing any other sql statements. Such as creating tables.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, i was 13 minutes late!
1

http://dev.mysql.com/doc/refman/5.0/en/use.html

USE my_db1;
CREATE TABLE ...
USE my_db2;
CREATE TABLE ...

2 Comments

if he need to connect to the database to make the operation, why set the database again?
I'm only showing how to change the current database using mysql statement.
1
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('database_name', $link);
CREATE TABLE Countr(ID INT IDENTITY(1,1) PRIMARY KEY,Page VARCHAR(50), Month INT, Always INT);

Comments

0

Try this code to create table in any database.

CREATE TABLE db_name.Countr(ID INT IDENTITY(1,1) PRIMARY KEY,Page VARCHAR(50), Month INT, Always INT);

Note: Current database user has create table privileges in the other database.

Comments

0
$sql = "CREATE TABLE wp_shifts (
    user_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    transact_id int(64),
    hours decimal(10,2), 
    date varchar(64),
    time1 varchar(64),
    PRIMARY KEY (user_id)
)";
$results = $wpdb->query($sql);

Comments

0

Create a Table The CREATE TABLE statement is used to create a table in MySQL.

We must add the CREATE TABLE statement to the mysqli_query() function to execute the command.

The following example creates a table named "Persons", with three columns: "FirstName", "LastName" and "Age":

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Create table
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.