php - Checking If MySQL Rows Returned>10 and if so running some code, if not running a different piece of code -
i have code below supposed check database entries username working when try add code check if rows returned greater 10 , run code limit number of rows 10 , if not run piece of code display rows.
code:
mysql_select_db("blahblah", $con); //connect database.. $username = basename(dirname(__file__)); $username = mysql_real_escape_string($username); $checkres = mysql_query("select count link, notes, titles, color links username='" . $username . "';"); if ($checkres>10) { $resultsmall = mysql_query("select link, notes, titles, color links username='" . $username . "' limit 10;"); while ($rowsmall = mysql_fetch_array($resultsmall)) { //loop extract($rowsmall); $htmlsmall .= " //code goes here format results "; echo $htmlsmall; //display results... } } else { $result = mysql_query("select link, notes, titles, color links username='" . $username . "';"); while ($row = mysql_fetch_array($result)) { //loop extract($row); $html .= " //code goes here format results "; echo $html; //display results... } } mysql_close($con); //close db.. but displays 2 times number of rows in database instead of either limiting or displaying them all. how fix this? //code goes here formatting isn't important formats code looks nice , displays in box...
thanks!
edit:
i have code doesn't display @ all? can help:
code:
mysql_select_db("blahblah", $con); //connect database.. $username = basename(dirname(__file__)); $username = mysql_real_escape_string($username); $sql = "select sql_calc_found_rows link, notes, titles, color links username='" . $username . "' limit 10"; $result = mysql_query($sql) or die("mysql error: " . mysql_error()); $subsql = "select found_rows() foundrows;"; $subresult = mysql_query($subsql) or die("mysql error: " . mysql_error()); $found_rows = $subresult['foundrows']; if($found_rows > 10) { while($row = mysql_fetch_array($result)) { extract ($row); $html .= " //getting values columns , formatting them goes here "; echo "only 10 allowed."; } } else { while($row = mysql_fetch_array($result)) { extract($row); $html .= " //getting values columns , formatting them goes here "; } } mysql_close($con); //close db.. thanks!
edit 2 (12 april 14:03 2011 gmt):
i have code has been kindly helped col. shrapnel doesn't display me , don't know why, please help.
code:
mysql_select_db("blahblah", $con); //connect database.. $username = basename(dirname(__file__)); $username = mysql_real_escape_string($username); $sql = "select link, notes, titles, color links username='$username' limit 10"; $res = mysql_query($sql); if (mysql_num_rows() == 10) { while ($row = mysql_fetch_array($res)) { extract($row); $htmlsmall .= " = "; $htmlfree .= "only 10 allowed."; echo $htmlsmall; echo $htmlfree; } } else { while ($row = mysql_fetch_array($res)) { extract($rowsmall); $htmlsmall .= "
"; echo $htmlsmall; } } now if view page source can see these 2 errors:
<b>warning</b>: wrong parameter count mysql_num_rows() in <b>//url here</b> on line <b>109</b><br /> <b>warning</b>: mysql_fetch_array(): supplied argument not valid mysql result resource in <b>//url file goes here</b> on line <b>125</b><br /> line 109 this: if (mysql_num_rows() == 10) {
line 125 this: while ($row = mysql_fetch_array($res)) {
(those lines in code above)
please me this?
thanks!
i try add code check if rows returned greater 10 , run code limit number of rows 10
it makes no sense.
run query limit 10 , data.
mysql_select_db("blahblah", $con); //connect database.. $username = basename(dirname(__file__)); $username = mysql_real_escape_string($username); $sql = "select * links username='$username' limit 10"; $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql); while ($row = mysql_fetch_array($res)) { extract($rowsmall); $htmlsmall .= ""; //code goes here format results } echo $htmlsmall; that's all. if still want echo "only 10 allowed." (dunno why though) can add these 3 lines below
if (mysql_num_rows($res) == 10) { echo "only 10 allowed."; } however see not point in it.
also note program design wrong, you're apparently copying file every user in system
Comments
Post a Comment