Jump to content

Output to CREATE TABLE?


Hangwire

Recommended Posts

Hi, I need a quick tip. I want this script to produce an output of "Table created" if it succesfully creates a table.

 

<?php
$con = mysql_connect("localhost","****","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE People
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?> 

 

I tried to do it on my own, but I guess I'm still completely new to php  :-[

I tried:

<?php
$con = mysql_connect("localhost","****","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create table
if (mysql_select_db("my_db", $con))
  {
  echo "Table created";
  }
else
  {
  echo "Error creating Table: " . mysql_error();
  }
$sql = "CREATE TABLE People
(
FirstName varchar(15),
LastName varchar(15),
Age int
)"


// Execute query
mysql_query($sql,$con);

mysql_close($con);
?> 

 

It gave me an error with the last two lines

mysql_query($sql,$con);

mysql_close($con);

 

I removed them and it really did give the output "Table created" yet it didn't create the table in the database. I'm having a feeling that I need to add an "if" statement, something of the sort.

 

if (sql(CREATE TABLE Persons))

 

But I'm also getting the feeling something there is wrong, I still haven't tried it.  :D

 

Help? :-\

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.