Web Hosting Talk







View Full Version : mysql: how do I limit the # of entries?


bfrog
05-14-2004, 02:52 PM
Let's say I want a table that stores each all the friends for each user... so the table structure would simply be two fields: one for userID, and another for the friendID.

The problem is, let's say I want to limit the number of friends per user to 50. How can I accomplish this?

azizny
05-14-2004, 03:09 PM
before entering the friend into the database, check how many friends are already there. If more than or equal to 50 give the appropraite message, else enter him. This can be accomplished using the mysql_num_rows of the mysql_query:


$query = "SELECT * FROM tablename WHERE userid=aziz";
$result = @mysql_query_result;
$howmany = @mysql_num_rows($result);

if($howmany >50){
$message = 'You already got 50 firneds, enough already';
} else {
$message = 'another firned added, loser';
//Also do the adding into the database
}



Peace,

bfrog
05-14-2004, 04:00 PM
thanks, I figured that's what I'd do at first, but I was hoping there would be a more efficient solution.

however, I'm a bit unfamiliar with the syntax... what's the @ for?

is it equivalent to this?
$result = mysql_query("SELECT * FROM ....");
$howmany = mysql_num_rows($result);also, a bit of a related question...

let's say I add the field 'visible' to this table, which sets if a user wants to be visible to a specific friend.

if I run the query
SELECT * FROM table WHERE userid=$id AND visible='yes';

would 'visible' benefit from having an index?

thanks

azizny
05-14-2004, 07:08 PM
The @ is for error reporting in PHP, You add it before you do any opeartion that might cause an error (openfile, read, delete, include, etc..)..

This means if an error occurs, the user WILL NOT SEE THE ERROR, BUT WILL CONTINUE OR STOP the page.

E.G. You Do This:

@require("header.php"); and it is not there the page will stop without printing a warning "missing file". On the other hand, if u do this:

@include("header.php"); and its not there the page will continue to load without the header and without a an error printin on the screen..

To tell u the truth, i dont even know whats index is for (i only use primary and regular :p )

Peace,

evilek
05-14-2004, 07:18 PM
$query = mysql_query("SELECT * FROM tablename WHERE userid=aziz LIMIT 50");
while($result = @mysql_fetch_array($query)) {
print "record ".++$i."<br>";
print_r($result);
print "<hr>";
}
$howmany = @mysql_num_rows($result);
print "you have just only $howmany friends";


ok, if you wanna limit records in db per user, you must in formpage check current number of friends like..

if ( current no. of friends > = 50 ) then blabla
else blabla ...

something like that...