mybluehair Posted November 10, 2007 Share Posted November 10, 2007 ok, so im making a chat room using only php and mysql. here is my full code (this is one file called allinone.php): $database = "*******"; $username = "********"; $password = "*****"; $message=$_POST['message']; echo ' <center><a href="logintoap.html">Admin panel</a></center><br> <form action="allinone.php" method="post"> <br> chat message:<br> <textarea rows="12" cols="65" name="message" id="message"></textarea> <br><br> <input type="submit"><br>'; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die ( "unable to connect"); if ($message == '') { $message = false; } else { $query = "INSERT INTO contacts VALUES ('', '$message')"; } mysql_query($query); mysql_close(); echo '<hr>'; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die ( "unable to connect"); $query="SELECT * FROM contacts"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>chats:</center></b><br><br><br>"; $i=0; while ($i < $num) { $message=mysql_result($result,$i,"message"); echo "<b>$message</b><br><hr><br>"; $i++; } so hopfully u can imagnine what that looks like, and what it does. but for some reason, half the time, the code that displays the messages from my DB will order it wong. so lets yes you type in "hello" and click enter. the "hello" will appear on the bottom of the list of everything else sayed, intead of appearing of the top. can some one help me? how do i correct this? Quote Link to comment Share on other sites More sharing options...
Daukan Posted November 10, 2007 Share Posted November 10, 2007 This needs to use the ORDER BY function $query="SELECT * FROM contacts"; To $query="SELECT * FROM `contacts` ORDER BY created_date DESC"; I'm don't know the schema of your database so I can tell you exactly what to order by. But you have the timestamp of when the message was created, that would be a good field to use. Quote Link to comment Share on other sites More sharing options...
mybluehair Posted November 10, 2007 Author Share Posted November 10, 2007 This needs to use the ORDER BY function $query="SELECT * FROM contacts"; To $query="SELECT * FROM `contacts` ORDER BY created_date DESC"; I'm don't know the schema of your database so I can tell you exactly what to order by. But you have the timestamp of when the message was created, that would be a good field to use. ok. um...how would i creat a field like that? (timestamp) how exactly would i get it to know what time a message was created? also when i enter your code i get error: Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/gotdrake/public_html/tutorial/allinone.php on line 40 line 40 is: $num=mysql_numrows($result); Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 10, 2007 Share Posted November 10, 2007 do you have mysql_select_db("db",$connect); ?? Quote Link to comment Share on other sites More sharing options...
mybluehair Posted November 10, 2007 Author Share Posted November 10, 2007 do you have mysql_select_db("db",$connect); ?? um...what? in my php code you mean? i have: @mysql_select_db($database) or die ( "unable to connect"); y? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 10, 2007 Share Posted November 10, 2007 why would you supress an error and then put an or die on it makes no sense to me Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 10, 2007 Share Posted November 10, 2007 nonono the @ will supress it and not make it work $connect= mysql_connect(localhost,$username,$password); mysql_select_db("db",$connect); Quote Link to comment Share on other sites More sharing options...
mybluehair Posted November 10, 2007 Author Share Posted November 10, 2007 nonono the @ will supress it and not make it work $connect= mysql_connect(localhost,$username,$password); mysql_select_db("db",$connect); ok, but what would $connect equal. and how does this help my problem? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted November 10, 2007 Share Posted November 10, 2007 connect would makeit connect to the actual database and the select would select it. Quote Link to comment Share on other sites More sharing options...
gtal3x Posted November 10, 2007 Share Posted November 10, 2007 ok. um...how would i creat a field like that? (timestamp) how exactly would i get it to know what time a message was created? ill post how i did it... MySQL: somerow DATETIME NOT NULL and from your script you need something like that: $query = "INSERT INTO sometable (somerow) VALUES (NOW())"; Quote Link to comment Share on other sites More sharing options...
mybluehair Posted November 10, 2007 Author Share Posted November 10, 2007 ok. um...how would i creat a field like that? (timestamp) how exactly would i get it to know what time a message was created? ill post how i did it... MySQL: somerow DATETIME NOT NULL and from your script you need something like that: $query = "INSERT INTO sometable (somerow) VALUES (NOW())"; ok. thank you. im almost there. here is my little code: $query = "INSERT INTO contacts VALUES ('', '$message', NOW())"; as you can imagine, i have created a new field in contacts called "datetime" and i used the little "now" code thing you gave me. so i tested it out. when i enter in a message, and submit it, i go to myphpadmin and in browse i see my message i entered, and along with it, in the datetime feild, i see the time it was created. but only problem now, is when i change: $query="SELECT * FROM `contacts` "; to: $query="SELECT * FROM `contacts` ORDER BY created_date DESC"; all i get is a mysql error. maybe you could help me out. here is the bit in my file that works as showing entered messges: $query="SELECT * FROM `contacts` "; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>chats:</center></b><br><br><br>"; $i=0; while ($i < $num) { $message=mysql_result($result,$i,"message"); echo "<b>$message</b><br><hr><br>"; $i++; } how would i change this, so it could use the datetime to order them correctly? 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.