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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:22:10+00:00 2026-05-28T03:22:10+00:00

I previously designed the website I’m working on so that I’d just query the

  • 0

I previously designed the website I’m working on so that I’d just query the database for the information I needed per-page, but after implementing a feature that required every cell from every table on every page (oh boy), I realized for optimization purposes I should combine it into a single large database query and throw each table into an array, thus cutting down on SQL calls.

The problem comes in where I want this array to include skipped IDs (primary key) in the database. I’ll try and avoid having missing rows/IDs of course, but I won’t be managing this data and I want the system to be smart enough to account for any problems like this.

My method starts off simple enough:

//Run query
$localityResult = mysql_query("SELECT id,name FROM localities");
$localityMax = mysql_fetch_array(mysql_query("SELECT max(id) FROM localities"));
$localityMax = $localityMax[0];

//Assign table to array
for ($i=1;$i<$localityMax+1;$i++)
{
    $row = mysql_fetch_assoc($localityResult);
    $localityData["id"][$i] = $row["id"];
    $localityData["name"][$i] = $row["name"];
}

//Output
for ($i=1;$i<$localityMax+1;$i++)
{
    echo $i.". ";
    echo $localityData["id"][$i]." - ";
    echo $localityData["name"][$i];
    echo "<br />\n";
}

Two notes:

  1. Yes, I should probably move that $localityMax check to a PHP loop.

  2. I’m intentionally skipping the first array key.

The problem here is that any missed key in the database isn’t accounted for, so it ends up outputting like this (sample table):

  1. 1 – Tok
  2. 2 – Juneau
  3. 3 – Anchorage
  4. 4 – Nashville
  5. 7 – Chattanooga
  6. 8 – Memphis
  7. –
  8. –

I want to write “Error” or NULL or something when the row isn’t found, then continue on without interrupting things. I’ve found I can check if $i is less than $row[$i] to see if the row was skipped, but I’m not sure how to correct it at that point.

I can provide more information or a sample database dump if needed. I’ve just been stuck on this problem for hours and hours, nothing I’ve tried is working. I would really appreciate your assistance, and general feedback if I’m making any terrible mistakes. Thank you!


Edit: I’ve solved it! First, iterate through the array to set a NULL value or “Error” message. Then, in the assignations, set $i to $row[“id”] right after the mysql_fetch_assoc() call. The full code looks like this:

//Run query
$localityResult = mysql_query("SELECT id,name FROM localities");
$localityMax = mysql_fetch_array(mysql_query("SELECT max(id) FROM localities"));
$localityMax = $localityMax[0];

//Reset
for ($i=1;$i<$localityMax+1;$i++)
{
    $localityData["id"][$i] = NULL;
    $localityData["name"][$i] = "Error";
}

//Assign table to array
for ($i=1;$i<$localityMax+1;$i++)
{
    $row = mysql_fetch_assoc($localityResult);
    $i = $row["id"];
    $localityData["id"][$i] = $row["id"];
    $localityData["name"][$i] = $row["name"];
}

//Output
for ($i=1;$i<$localityMax+1;$i++)
{
    echo $i.". ";
    echo $localityData["id"][$i]." - ";
    echo $localityData["name"][$i];
    echo "<br />\n";
}

Thanks for the help all!

  • 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-28T03:22:10+00:00Added an answer on May 28, 2026 at 3:22 am

    Primary keys must be unique in MySQL, so you would get a maximum of one possible blank ID since MySQL would not allow duplicate data to be inserted.

    If you were working with a column that is not a primary or unique key, your query would need to be the only thing that would change:

    SELECT id, name FROM localities WHERE id != "";
    

    or

    SELECT id, name FROM localities WHERE NOT ISNULL(id);
    

    EDIT: Created a new answer based on clarification from OP.
    If you have a numeric sequence that you want to keep unbroken, and there may be missing rows from the database table, you can use the following (simple) code to give you what you need. Using the same method, your $i = ... could actually be set to the first ID in the sequence from the DB if you don’t want to start at ID: 1.

    $result = mysql_query('SELECT id, name FROM localities ORDER BY id');
    
    $data = array();
    while ($row = mysql_fetch_assoc($result)) {
        $data[(int) $row['id']] = array(
            'id'   => $row['id'],
            'name' => $row['name'],
        );
    }
    
    // This saves a query to the database and a second for loop.
    end($data); // move the internal pointer to the end of the array
    $max = key($data); // fetch the key of the item the internal pointer is set to
    
    for ($i = 1; $i < $max + 1; $i++) {
        if (!isset($data[$i])) {
            $data[$i] = array(
                'id'   => NULL,
                'name' => 'Erorr: Missing',
            );
        }
    
        echo "$i. {$data[$id]['id']} - {$data[$id]['name']}<br />\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an upgrade for an existing database that was designed without any
I previously created a website using ASP.NET that stores contact information and can produce
Previously we had desktop applications but given the fact that accessing the server (either
I am working on an eCommerce website designed to present a large number of
I've designed a web page in asp.net. And in that page i placed html
I am working with a WinForm application that was designed by the previous, now
Previously, I had a class that wrapped an internal System.Collections.Generic.List<Item> (where Item is a
Previously, I was passing information to a Silverlight control inside of the Page_Load method.
Previously, I used to use MFC collection classes such CArray and CMap . After
We have a web page that was created in Visual Studio 2008 by 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.