I finally got the database connected but it won't create the table for me. Can we only create tables at the command line?

$sth = $dbh->prepare (CREATE TABLE test
    (first_field CHAR(10),
     second_field INT)) or print "Can't create table: " . DBI->errstr;

$sth -> execute($test) or print "Can't execute create table" . DBI->errstr;

$dbh -> commit or print "Couldn't commit: " . DBI->errstr;

$dbh -> disconnect or print "Couldn't disconnect: " . DBI->errstr;

$dbh = DBI->connect("DBI:mysql:host=localhost;database=$database","$username","$password") or print "Couldn't connect to database: " . DBI->errstr;
print "Database connected<br>";


print "Table created <br>";

$sth = $dbh->prepare ( INSERT INTO test VALUES ('1new line',1),
INSERT INTO test VALUES ('2NEW LINE',2),
INSERT INTO test VALUES ('3NEW LINE',3)) or print "Can't insert rows: " . DBI->errstr;


$sth -> execute($first_field, $second_field) or print "Can't insert rows: " . DBI->errstr;

$dbh -> commit or print "Couldn't commit: " . DBI->errstr;

print "Rows inserted<br>";

#$dbh = shift;
$dbh -> disconnect;
$dbh = DBI->connect("DBI:mysql:host=localhost;database=$database","$username","$password") or print "Couldn't connect to database: " . DBI->errstr;
print "Database connected<br>";
my $count;


print "Right before prepare to select<br>";

$sth = $dbh->prepare ("SELECT $first_field, $second_field FROM test") or print "Can't prepare: " . DBI->errstr;
$sth -> execute($first_field, $second_field) or print "Can't execute: " . DBI->errstr;


$count = 0;

$sth = $dbh->prepare( q{
          SELECT first_field, second_field
          FROM test
  }) or die "Can't prepare statement: " . DBI->errstr;

  my $rc = $sth->execute()
      or die "Can't execute statement: " . DBI->errstr;

  print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
  print "Field names: @{ $sth->{NAME} }\n";

  while (($first_field, $second_field) = $sth->fetchrow_array) {
      print "$first_field, $second_field\n";
  }
  # check for problems which may have terminated the fetch early
  print $sth->errstr if $sth->err;

print "</p>";
$sth -> finish();
$dbh -> disconnect;

print "<p>No items are found.</p>" if $count == 0;
print "</body></html>";

exit(0);

Reply via email to