Help - Search - Members - Calendar
Full Version: Tried to write simple phpscript for creating table
Movable Type Community Forum > Additional Resources > Tips and Tricks
nikolaus
I wanted to add a very simple news-script to my MT-weblog

Now I tried to write a php-script to create a MySQL-table called "my_news".

I keep getting the "failed"-message
I can't see what's wrong...



CODE
<?php
   /* Connecting, selecting database */
   $link = mysql_connect("localhost", "username", "password")
       or die("Could not connect");
   print "Connected successfully";
   mysql_select_db("database") or die("Could not select database");

$query = "CREATE TABLE my_news {
     id int (10 )  NOT NULL auto_increment ,
     title varchar (100 )  NOT NULL ,
     text text NOT NULL ,
     PRIMARY KEY  (id )
}";

echo "
creating table [b]my_news[/b]... ";
$q = mysql_query($query) or die ("<font face=ff0000>failed</font>");
echo "<font color=00ff00>succeeded</font>
";

  /* Closing connection */
   mysql_close($link);

?>
stepan
You shouldn't be using braces ({}) in your table specification. Also, you may want to use mysql_error() to display the error returned from mySQL:
CODE
$q = mysql_query($query) or die (mysql_error());
nikolaus
Thanx a lot!

It's working now!
nikolaus
Ok, I'm in trouble again...

I'm trying to make a script that gets the data from the table I just created (above)

I keep getting a parse error on line 10

which is this line:

CODE
while($news = mysql_fetch_array($query))


CODE
<?php
   /* Connecting, selecting database */
   $link = mysql_connect("localhost", "base", "password")
       or die("Could not connect");
   print "Connected successfully";
   mysql_select_db("base") or die("Could not select database");
    
$query = mysql_query('SELECT * FROM my_news');
    // get all the news in the 'news' table
while($news = mysql_fetch_array($query)) {
    echo "Title: " . $news['title'] . "n";
    echo $news['text'] . "n";
    echo "---------------------n";
}
?>
gramcracker
Do you have any data in the table?
girlie
Answered over here.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.