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
https://forums.phpfreaks.com/topic/213331-output-to-create-table/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.