View Full Version : What php mysql functions are usefull

08-18-2007, 07:40 AM
Hy all

I'm just starting MySQL, and wondering, what are the most useful php mysql (http://php.net/mysql)functions for beginner.

I'm using mysql_query for now, and i can see that it's very usefull, couse you can do almost anything with it.

What other functions are useful, and can save you writing lot's of mysql code.

Thanks for the info

08-18-2007, 08:47 AM
Well they are all useful depending on what you plan to do. A very handy tutorial(http://www.tizag.com/mysqlTutorial/).

mysql_error() is handy for debugging error messages
of course... mysql_connect()
mysql_real_escape_string() good for protecting against SQL injections
mysql_num_rows() for loops with the table rows along with mysql_affected_rows()
and of course mysql_close()

for a list off all php-mysql commands look at http://php.net/mysql


08-18-2007, 09:24 AM
I found out that mysql_fetch_array is good for showing results from tables.

Also, is limiting number of rows shown PHP or MySQL thing?
I mean should i use MySQL to limit shown queries, and creating Next and Previous links, or do i use PHP for that?

Thanks for the info :)

08-18-2007, 09:28 AM
You should use MySQL to limit the results. All you need is the total number of results, and the current results based on what page you're on.

08-18-2007, 09:56 AM
Thanks :top:
I gotta a few more questions in repertoár.

OK, now i'm building a simple admin system, where admin can see sent messages. how do i cross reference admin's last login time with new messages time (easiest way).

Do i create two rows for admin, like Last Time, and New Time where Last Time would be time when admin logged off, and New Time would be when admin logged on, and based on the values of last time, messages that are sent after that time, are going to be bolded like "new messages", is that a good way, or do you have any smarter suggestions?

Also i read about databases, a lot, and i shouldn't name table rows from two databases same, or i can do that.

Let's say for some reason i named time from admin database, and time from message database something like newTime, is that something that should be avoided?

Thanks :)

08-18-2007, 02:47 PM
For your first question, is the whole log in log out time just to see whether the message is 'new' or not? If so, why not just add field to the DB table where the message is stored that specifies whether the message has been viewed or not?

The default for the field would be that the message hadn't been viewed, and when the site retrieves a message from the database for the admin to view it, the field's value would be switched to saying he had viewed it.


08-18-2007, 04:05 PM
Ok, now i made a field Status for message, and default i set default value to 0 (or even maybe "unread"). What mysql function can change that status to 1 (or "read").

I've found this code which i need to call on admin message request query.

something like this

$sql = SELECT * FROM messages
//this is the code i found
$update ="UPDATE messages SET status='1' WHERE status='0'"

if (admin requests messages) {
mysql_fetch_array ($sql) or die..
mysql_query($update) or die---

is that the best, and the only way to update status from 0 to 1 or are there better ways?

Thank you all. I'm learning mySQL, and i'm fascinated how smart it is :)

Ok, sorry i didn't mention before, bu also if i had more users than admin, would then be a proper way to set NewTime and LastTime, or some other way, like this update. It looks logical that update can work for one user, but when there would be more users, than it would be hard to make such a system

08-18-2007, 05:50 PM
Use the time. Store the time when the admin logs in or logs out. You can update it on each page or only when the admin logs out, it's up to you. Then just select the messages that were posted since the last time the "time" was updated for that user. For your other question the query would be something like this:

$update ="UPDATE messages SET status='1' WHERE messageId = '123'";

08-18-2007, 07:55 PM
Thanks for the tip. I'll post back here when i test the codes you provided. :thumbsup: