...

View Full Version : count()



mivec
04-15-2004, 12:48 PM
hi all,

ok the scenario is like this.






let's say i have a few records in my db. what i want to do is i want to count the number of records in the and then +1 to the number and then insert the new value(with the added 1) in to the DB. i have done something like this. but it's not working and i am getting this notice:



Notice: Undefined variable: num in c:\program files\apache group\apache\htdocs\ewedding\uploadagain.php on line 235







this is m code:






$query = "SELECT * FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1";
$rs = mysql_query($query);
$numRows = mysql_num_rows($rs);

while ($row = mysql_fetch_object($rs))
{
$num = count($row = mysql_fetch_object($rs));
if ($num = 0)
{
$num = 1;
}
else
{
$num = $num + 1;
}
}

$query = "INSERT INTO tblgallery (pic_Num) VALUES ('" . $num . "')";
mysql_query($query) or
die (mysql_error());





and i checked the DB that it didn't insert the value. pls help.thank you

zigo86
04-15-2004, 01:25 PM
Don't do this:

$row = mysql_fetch_object($rs)

inside the while loop - It's already the condition for the while loop, and if you **** around with the $row var inside the loop, it's bound to bugger everything up.

zigo

raf
04-15-2004, 02:38 PM
$query = "SELECT * FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1";
$rs = mysql_query($query);
$numRows = mysql_num_rows($rs);

while ($row = mysql_fetch_object($rs))
{
$num = count($row = mysql_fetch_object($rs));
if ($num = 0)
{
$num = 1;
}
else
{
$num = $num + 1;
}
}

should be replaced by


$query = "SELECT count(*) as numrec FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1";
$rs = mysql_query($query) or die ('Queryproblem');
$row = mysql_fetch_assoc($rs))
$num = $row['numrec'];

mivec
04-16-2004, 07:47 AM
thanks for replying..but raf..i dun really understand ur code. especially at



die ('Queryproblem');


what do u mean queryproblem? and y do u use mysql_fetch assoc and not mysql_fetch_object? so sorry...i am pretty new to php that's y. i hope u can explain ur code...thank you thank you...



$query = "SELECT count(*) as numrec FROM tblgallery WHERE username = '" .$_POST["txtUsername"]. "' AND pic_Num = 1";
$rs = mysql_query($query) or die ('Queryproblem');
$row = mysql_fetch_assoc($rs))
$num = $row['numrec'];

raf
04-16-2004, 07:57 AM
i dun really understand ur code. especially at
Quote:

die ('Queryproblem');

what do u mean queryproblem?

or die ('Queryproblem') will print 'Queryproblem' if the mysql_query() function returns an error, and will then immedeately stop processing the script. So this means, if you have an invalid sql. Like if you would have a statement like 'select var from table testing asc' . If the sql is valid, then nothing is printed.

and y do u use mysql_fetch assoc and not mysql_fetch_object?
Because i handle it as an associative array instead of as an object. No point in treating a 1 value recordset as an object. But it's basically just a matter of preference.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum