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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:00:48+00:00 2026-05-31T17:00:48+00:00

My code is as follows: class Database { private $db_host; private $db_user; private $db_pass;

  • 0

My code is as follows:

class Database  
{  
    private $db_host;  
    private $db_user;  
    private $db_pass;  
    private $db_name;  
    private $con;

    public function __construct() {
        $this->db_host = "localhost";  
        $this->db_user = "admin";  
        $this->db_pass = 'password';  
        $this->db_name = 'test';    
        $this->con = '';
    }

    public function connect() {
        $db_name = "test";    
        $this->con = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
    }  

    public function select(){
        $q = "SELECT name, city FROM customers;";
        mysql_select_db($this->db_name, $this->con);
        $result = mysql_query($q);
        return mysql_fetch_assoc($result);
    }  
} 


$db = new Database();
$db->connect();
$tempArray = Array();
$rs = $db->select('customers', 'name, suburb');
foreach ($rs as $row)
{
    echo $rs['name'] . "<br>";
}

And my table’s data is

name | city
--------------
Anne | Sydney
Jane | London

The actual output is:

Anne 
Anne

The desired output is:

Anne
Jane

Can someone tell me what I am doing wrong. It seems like I have missed something basic. I have read over 50 articles and nothing seems to explain what I am doing wrong.

Note: This is a scaled down version of my code. I intend to use this to make a more general object that pulls information from my database.

Thanks,

Brett

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

    You need to call mysql_fetch_assoc for each row. It only returns one row of data, not the full set. For example, you could move it out into the loop:

    class Database
    {  
        /* ... */
    
        public function select(){
            $q = "SELECT name, city FROM customers;";
            mysql_select_db($this->db_name, $this->con);
            return mysql_query($q);
            /* Remove your line here, returning the query result, not the first row */
        }  
    } 
    
    $db = new Database();
    $db->connect();
    $tempArray = Array();
    $result = $db->select('customers', 'name, suburb');
    /* Note that I'm now using mysql_fetch_assoc to get each row from the result */
    while ($row = mysql_fetch_assoc($result));
        echo $row['name'] . "<br>";
    }
    

    You can use a while loop there because after the last row has been retrieved, mysql_fetch_assoc will return FALSE and exit the loop.

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

Sidebar

Related Questions

All the code examples always use base() as follows class A { public A()
Ok, here is the code and then the discussion follows: public class FlatArrayList {
I have a model like this defined... public class Product{ private long timestamp; //
Code for SQL class as follows: import java.text.SimpleDateFormat; import java.util.Date; import android.content.ContentValues; import android.content.Context;
Why can't I do something like this in c# (pseudo code follows) Interface1 {
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
I have code as follows: $(#item_select).change(function() { var params = $(#item_select option:selected).val(); $.post('/account/ar_form.php', {idata:
I have an entity class: Cabbage.java package womble; public class Cabbage { private int
I'm trying to figure out the best way to unit test this class: public
I have a custom initializer setup as follows: public class PromptIfChangesNeededDBInitializer : IDatabaseInitializer<MeyerREContext> {

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.