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 7182917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:52:21+00:00 2026-05-28T17:52:21+00:00

To get half of MySQL table records here is what I’m using: $numRows=mysql_num_rows(mysql_query(SELECT *

  • 0

To get half of MySQL table records here is what I’m using:

$numRows=mysql_num_rows(mysql_query("SELECT * FROM cultures"));
$half = $numRows/2;

Then on each side of the page, I limit the number of records by the half of it, and on the other side I order by DESC instead to get the other half:

$result = mysql_query("SELECT * FROM cultures ORDER BY name ASC LIMIT ".$half);
while($row = mysql_fetch_array($result)) {
$url = "artists.php?culture=".ucfirst($row['name']);
echo '<div class="homepage_culture_item">
<a href="'.$url.'"></a>
<img src="/images/flags/'.$row['name'].'.png" style="width: 30px; vertical-align: middle; margin-right: 10px;" />'.$row['name'].'
</div>
<br />';
}

Which works great, as you can see in this screenshot:

enter image description here

However, if there is an odd number of records in the table, I get an error:

enter image description here

Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in C:\wamp\www\index.php on line 50

It works great when there’s an even number of records but if another one is added, instead of just putting it on one side, it messes up because it is probably returning an error because when you divide 15 by 2, it will be a decimal and MySQL LIMIT can’t accept decimals (obviously). Is there a better way to do what I’m trying to do?

Thanks in advance!

  • 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-28T17:52:22+00:00Added an answer on May 28, 2026 at 5:52 pm

    You can just round it down so you always get an integer. That way, a decimal won’t break the query.

    $half = floor($numRows/2);
    

    You’ll want to use a new $half value using ceil() on the other side to round up, so you don’t skip a row in the middle.

    As an aside, it might be better to do just one query returning every row, then output the rows up to $half on one side, then continue from that point to the end for the other side. Then you’re only hitting the database once and it’s easier to see what’s going on.

    e.g

    $result = mysql_query("SELECT * FROM cultures ORDER BY name ASC");
    $half   = floor(mysql_num_rows($result)/2);
    $count  = 0;
    
    // First side.
    while($count <= $half
          && $row = mysql_fetch_array($result))
    {
        // ...
        $count++;
    }
    
    // ...
    
    // Second side.
    while($row = mysql_fetch_array($result))
    {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am on half of URL shortening system. I get the URL from the
I created a MySQL view using UNION ALL from two tables so that I
My problem is when using C# and MySQL database to save some records by
MySQL CMD Client will run this file but get half way through and complain
We get a large amount of data from our clients in pdf files in
I get a URL from a user. I need to know: a) is the
I've read the papers linked to in this question . I half get it.
I have a gloss method and I'm trying to get a inverted half moon
I've spent half an hour trying to get this, maybe someone can come up
I'm trying to return MySQL records where they don't end in a specific way

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.