V-Man Posted February 12, 2007 Share Posted February 12, 2007 Hey guys, It's been ages! Alright, Im just now getting back into PHPing and have found my self in a slump. Here's what I want to do. Select all the rows out of a table (this will be five, as all others will be deleted) and display them with the text and the user that posted it. Im coding a shout box for my guild website. Heres what I have so far. And yes, my session based login works already and the variables pass correctly. the SQL table that i set up CREATE TABLE `shout` ( `id` mediumint( unsigned NOT NULL default '0', `text` varchar(40) NOT NULL default '', `user` varchar(32) NOT NULL default '', `shown` int(1) NOT NULL default '1', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Now, I know what I need to do: select the rows, display them like so: "blah blah blah blah", --{the username} "blah blah blah blah", --{the username} That's what I need to do. But I can't for the life of my remember how to do it. Any suggestions? Quote Link to comment Share on other sites More sharing options...
fert Posted February 12, 2007 Share Posted February 12, 2007 do you mean the $_SESSION array? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Are you asking how to do a SELECT from a database? There are some tutorials here on how to do that. Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 Not just a select, I can do the basic part. SELECT * FROM shout I can do that. What I cant remember how to do is select the rows, then display them as seen above, but only doing the five newest. Thats what i cant remember Quote Link to comment Share on other sites More sharing options...
fert Posted February 12, 2007 Share Posted February 12, 2007 ORDER BY `posted_on` LIMIT 5 Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 I cant remember the PHP to take the SQL data and order it to be shown row by row, like in my example above. Thats what i cant remember how to do. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 So look at the tutorials, or the php manual. mysql_query() Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 >.< never mind, i clearly cant explain this. Ill let you know if i get it. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Look at the examples here: http://us2.php.net/mysql_query You're saying you want to loop through the results and print them - the code in "Example 1356. Valid Query" shows you how to do that. Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 Alright, got what i need, but now my insert query is bugging... $query = "INSERT INTO shout (id, text, user, shown) VALUES (NULL, $post, $user, CURRENT_TIMESTAMP)"; Why does that bug Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Strings must be surrounded in ''. Also, you don't need the id and NULL parts. Quote Link to comment Share on other sites More sharing options...
fert Posted February 12, 2007 Share Posted February 12, 2007 test is a reserved sql keyword Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 $query = 'INSERT INTO shout (shouttext, user) VALUES ('.$post.', '.$user.')'; Why does that not work? What am i missing... Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 yes, I updated my DB. The error im getting is ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'query' at line 1 NOT the unidentifyed row error. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 $query = 'INSERT INTO shout (shouttext, user) VALUES ('.$post.', '.$user.')'; They're still not in the quote marks. $query = "INSERT INTO shout (shouttext, user) VALUES ('$post', '$user')"; Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 Still getting same error. Nada. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Can you post your code please. I suspect it looks like this: mysql_query(query); and not mysql_query($query); Quote Link to comment Share on other sites More sharing options...
V-Man Posted February 12, 2007 Author Share Posted February 12, 2007 Youre good, thank you. Problem solved. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted February 12, 2007 Share Posted February 12, 2007 $query = "INSERT INTO shout (`text`,`user`,`shown`) VALUES ('".$post."','".$user."',CURRENT_TIMESTAMP)"; Only other problem I can see is that you're trying to insert a TIMESTAMP variable into an INT container sized 1 - meaning it can only store a single number (0-9). Try changing the datatype of "shown" to DATETIME. You could also use INT size 10 UNSIGNED and use this instead: $query = "INSERT INTO shout (`text`,`user`,`shown`) VALUES ('".$post."','".$user."','".time()."')"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.