Forum Moderators: coopster

Message Too Old, No Replies

Mysql beginner problem

make table, insert data , and read table.

         

ikbenhet1

10:45 am on Jun 7, 2003 (gmt 0)

10+ Year Member



Why does this not return the contents of table mydatabase?
It returns: "0 records."

I've read a lot of manuals but can't see what's wrong.

//make database
mysql_query("CREATE TABLE mydatabase( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), url VARCHAR(30), description VARCHAR(30))")or die("Create table Error: ".mysql_error());


//insert something
mysql_query("INSERT INTO TABLE mydatabase (url,description) VALUES ('hi','database')");


//output table
$result = mysql_query( "SELECT * FROM mydatabase" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";

addded: If anybody knows function library's that handle Mysql queries specially for beginners and idiots please let me know.

Paul in South Africa

11:26 am on Jun 7, 2003 (gmt 0)

10+ Year Member



From what I can see on a quick look your insert query should be:

"INSERT INTO mydatabase (url,description) VALUES ('hi','database')"

Leave the TABLE out.

ikbenhet1

11:36 am on Jun 7, 2003 (gmt 0)

10+ Year Member




I added the "TABLE" later myself to play and see if i could make it work.
Without the "TABLE" i still get 0 results.

Birdman

11:41 am on Jun 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing you INSERT statement to:

mysql_query("INSERT INTO TABLE mydatabase (id,url,description) VALUES ('','hi','database')");

Added: phpMyAdmin is very helpful for finding problems. It this case you could have simply done an insert via phpMyAdmin and then they give you the exact query used. You then copy that to your script and alter where needed.

ikbenhet1

11:48 am on Jun 7, 2003 (gmt 0)

10+ Year Member



I've tried it. still 0 results.

Added: I'll search for phpMyAdmin then.

Birdman

12:13 pm on Jun 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is straight from phpMyAdmin:

CREATE TABLE `mytable` (
`id` INT NOT NULL AUTO_INCREMENT,
`url` VARCHAR( 30 ) NOT NULL ,
`description` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `id` )
);

INSERT INTO `mytable` ( `id` , `url` , `description` )
VALUES (
'', 'hi', 'database'
);

The noticable thing are the use of backquotes and PRIMARY KEY is at the end.

ikbenhet1

12:20 pm on Jun 7, 2003 (gmt 0)

10+ Year Member




Birdman, your great!

I have installed phpmyadmin, and had just found the query to use(before i saw your's), and it works fine.

This could have saved me 2 days if i known it sooner. 2 days of work and i had only achieved to connect to mysql.

THANKS

Birdman

12:27 pm on Jun 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So what was the actual problem, the backquotes? I don't normally use them and my queries work fine.

Added: You will definately like phpMyAdmin! You can test more complex queries there before adding them to scripts and it also gives you export/import features.

ikbenhet1

3:41 pm on Jun 7, 2003 (gmt 0)

10+ Year Member



Birdman,

Must be because i did not have backquotes around the fields.

Thank you again for the phpmyadmin tip, i'm really getting the hang of this now.

jatar_k

3:48 pm on Jun 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The back quotes wouldn't be it by themselves though, oh well.

phpmyadmin will help you out quite a bit, its quite the powerful little tool.