Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4324352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:07:15+00:00 2026-05-21T09:07:15+00:00

I have the code below which is supposed to check the database for entries

  • 0

I have the code below which is supposed to check the database for entries with a certain username which is working but when I try to add some code to check if the rows returned is greater than 10 and then run some code to limit the number of rows to 10 and then if not run another piece of code which will display all the rows.

Code:

    mysql_select_db("blahblah", $con); //connect to database..
    $username =  basename(dirname(__FILE__));
    $username = mysql_real_escape_string($username);
    $checkres = mysql_query("SELECT COUNT link, notes, titles, color FROM links WHERE username='" . $username . "';");
    if ($checkres>10)
    {
    $resultsmall = mysql_query("SELECT link, notes, titles, color FROM links WHERE username='" . $username . "' LIMIT 10;");
    while ($rowsmall = mysql_fetch_array($resultsmall)) { //loop
      extract($rowsmall);
      $htmlsmall .= "
//code goes here to format results
                ";
    echo $htmlsmall; //display results...
    }
    }
    else {
    $result = mysql_query("SELECT link, notes, titles, color FROM links WHERE username='" . $username . "';");
    while ($row = mysql_fetch_array($result)) { //loop
      extract($row);
      $html .= "
            //code goes here to format results
                ";
    echo $html; //display results...
    }
    }
    mysql_close($con); //close db..

But it just displays two times the number of rows in the database instead of either limiting it or displaying them all. How would I fix this? The //code goes here for formatting isn’t important it just formats the code so that it looks nice and displays it in a box…

Thanks!

EDIT:

I now have this code but it now doesn’t display anything at all? Can someone help:

Code:

mysql_select_db("blahblah", $con); //connect to database..
$username =  basename(dirname(__FILE__));
$username = mysql_real_escape_string($username);
$sql = "SELECT SQL_CALC_FOUND_ROWS link, notes, titles, color FROM links WHERE username='" . $username . "' LIMIT 10";
$result = mysql_query($sql) or die("MySQL error: " . mysql_error());

$subsql = "SELECT found_rows() AS 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 from columns and then formatting them goes here
    ";                                             

    echo "Only 10 is allowed.";
  }
} 
else
{
  while($row = mysql_fetch_array($result))
  {
    extract($row);
    $html .= "
      //getting values from columns and then formatting them goes here
    ";
  }
}
mysql_close($con); //close db..

Thanks!

EDIT 2 (12 April 14:03 2011 GMT):

I now have this code which has been kindly helped by Col. Shrapnel but it doesn’t display anything for me and I don’t know why, please could some help.

Code:

mysql_select_db("blahblah", $con); //connect to database..
$username =  basename(dirname(__FILE__));
$username = mysql_real_escape_string($username);
$sql = "SELECT link, notes, titles, color FROM links WHERE 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 is allowed.”;
echo $htmlsmall;
echo $htmlfree;
}
}
else {
while ($row = mysql_fetch_array($res)) {
extract($rowsmall);
$htmlsmall .= “

  ";
echo $htmlsmall;
}
}

Now if I view the page source I can see these 2 errors:

<b>Warning</b>:  Wrong parameter count for mysql_num_rows() in <b>//url was here</b> on line <b>109</b><br /> 


<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>//url to file goes here</b> on line <b>125</b><br /> 

Line 109 is this: if (mysql_num_rows() == 10) {

Line 125 is this: while ($row = mysql_fetch_array($res)) {

(Those lines are in the code above)

Please could someone help me with this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:07:16+00:00Added an answer on May 21, 2026 at 9:07 am

    I try to add some code to check if the rows returned is greater than 10 and then run some code to limit the number of rows to 10

    It makes no sense.
    Just run your query with LIMIT 10 and you will get your data.

    mysql_select_db("blahblah", $con); //connect to database..
    $username =  basename(dirname(__FILE__));
    $username = mysql_real_escape_string($username);
    $sql = "SELECT * FROM links WHERE 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 to format results
    }
    echo $htmlsmall;
    

    That’s all.
    If you still want to echo “Only 10 is allowed.” (dunno why though) you can add these 3 lines below

    if (mysql_num_rows($res) == 10) {
      echo "Only 10 is allowed.";
    }
    

    However I see not much point in it.

    Also note that your program design is probably wrong, as you’re apparently copying this file for the every user in your system

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far i have got the code below which works lovely when trying an
I have a table defined (see code snippet below). How can I add a
EDIT: See my working code in the answers below. In brief: I have a
I have the below code which is suppose to show a list of contact
Hi I have this code below which basically after 25 seconds of showing the
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I have noticed that my geocoder is inconsistent in the code shown below because
Okay so I have a scenario similar to the below code, I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.