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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:09:42+00:00 2026-06-01T10:09:42+00:00

i have a table with the following fields: row_id first_field second_field row_id is of

  • 0

i have a table with the following fields:

row_id    
first_field
second_field

row_id is of type integer, is set to auto increment and is the primary key. the other two fields are of type text.

the table is populated up to five rows. as such, the values for row_id are 1, 2, 3, 4 and 5.

i also have another table of a similar structure that has a one-to-many correspondence with my first table.

something weird happens though when i do a select query and feed the result to mysql_fetch_array.

when i run this:

$query = "select a.*, b.* from table1 as a
         left join table2 as b on a.row_id = b.row_id";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    echo '<pre>'; print_r($row); echo '</pre>';
}

i get this:

Array
(
    [0] => 1
    [row_id] => 1
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}


Array
(
    [0] => 2
    [row_id] => 2
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}


Array
(
    [0] => 3
    [row_id] => 
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}


Array
(
    [0] => 4
    [row_id] => 
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}


Array
(
    [0] => 5
    [row_id] => 
    [1] => some text
    [first_field] => some text
    [2] => some text
    [second_field] => some text 
}

on each array result, i’d like to direct your attention to the first field, row_id. in the first two arrays, index 0 and row_id have the same values, whereas in the subsequent three arrays, only index 0 has a value. row_id appears to be null.

this is the first time i’ve ever encountered something like this. what is causing this? how can this be fixed?

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-06-01T10:09:43+00:00Added an answer on June 1, 2026 at 10:09 am

    This is related to the fact that you have LEFT JOINed the other table in the query.

    With a LEFT JOIN, the rows in the first table that do not have matching row in the second table will have the JOINed fields populated with NULL values.

    Because you have a column called row_id in both tables and you have selected * from both tables, the later value for row_id is overwriting the earlier in the results. It would seem that there are no matching rows in table2 for rows 3, 4 and 5 in table1. This is resulting in a NULL row_id.

    The solution is to choose which row_id you want, and there are a number of ways to do this. But with a relatively simple set of results like this, I suggest you explicitly state all the columns you want:

    SELECT a.*, b.col_1, b.col_2
    FROM table1 a
    LEFT JOIN table2 b ON a.row_id = b.row_id
    

    Alternatively, you could drop the LEFT from the join, and the rows 3, 4 and 5 will be omitted from the results.

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

Sidebar

Related Questions

I have a table (session_comments) with the following fields structure: student_id (foreign key to
I have a table with the following structure, FIELD TYPE EXTRA faciltiy_id int auto_increment(Primary
I have sqlite table CREATE TABLE IF NOT EXISTS [app_status]( [id] INTEGER PRIMARY KEY
I have a table called Employee with the following fields: EmpID Salary Name I
I have a table in db2 which has the following fields int xyz; string
I have a table that has multiple rows with the following fields: PersonName SongName
If I have the following fields in a table called DailyLeaveLedger LeaveDate LeaveType AmountPaid
I have the following mysql table called pics, with the following fields and sample
I have a table called link_track with the following fields: id, uid, ip, hitdate
Lets say I have the following query. SELECT stringdata FROM table ORDER BY FIELDS(

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.