Jump to content

cannot get the rows from the db


jimfog

Recommended Posts

I am having difficulty getting rows out of the db. The code is in a function and the while loop is correct(you will see in a while), the problem what is returned from the function.

 

First, the code:

function get_services($connection,$email) {       $connection->set_charset("utf8");             $result = $connection->query('                                select servicename                 from business_users,buser_services,services,users                 where users.email="'.$email.'"                 and users.user_ID=business_users.crID                 and business_users.crID=buser_services.buserID                 and buser_services.serviceID=services.serviceID;                ');        if(!$result)        {return false;}        elseif($result ->num_rows>0){    while ($services=$result->fetch_object()) {//άραγε πότε χρησιμοποιώ το        $serviceslist=$services->servicename;          var_dump($serviceslist);        }}        return $serviceslist;        }

var_dump gives me 2 rows, stored in the servicelist variable...so this is OK.

The problem is that when I call the function, only one row gets returned.

 

Here is the code that calls the function:

$services=get_services($conn,$_SESSION['valid_user']);                               echo $services;

in the above code...only one row gets echoed in the browser.

What am I doing wrong?

Link to comment
Share on other sites

I cannot make it work. I did what you said but obviously there is something I am doing wrong:

function get_services($connection,$email) {     $serviceslist=array();     $connection->set_charset("utf8");             $result = $connection->query('                                select servicename                 from business_users,buser_services,services,users                 where users.email="'.$email.'"                 and users.user_ID=business_users.crID                 and business_users.crID=buser_services.buserID                 and buser_services.serviceID=services.serviceID;                ');        if(!$result)        {return false;}        elseif($result ->num_rows>0)            {                while ($services=$result->fetch_object()) {//άραγε πότε χρησιμοποιώ το                    $serviceslist=$services->servicename;                                    }                       }                    return $serviceslist;         }

As you above, in the beginning of the function I declare servicelist as an array but the results are the same as before.One row gets returned.

Link to comment
Share on other sites

You overwrote the array with something else.

 

Typing $a = array() and then $a = 5 won't give you an array with "5" in it. It will just be "5". The assignment operator "=" overwrites anything that was previously in the variable.

Link to comment
Share on other sites

You overwrote the array with something else.

 

Typing $a = array() and then $a = 5 won't give you an array with "5" in it. It will just be "5". The assignment operator "=" overwrites anything that was previously in the variable.

Αnd what is the solution to it?

Link to comment
Share on other sites

$arr[]='foo';

I still cannot make it work...overwriting takes place still(despite trying the above). Here is a cleaner version of the code where I have stripped out unnecessary things like the query itself so that we can focus better on the while loop:

The function:

 function get_services($connection,$email) {     $serviceslist=array();    .....            {                while ($services=$result->fetch_object()) {//άραγε πότε χρησιμοποιώ το                    $serviceslist['services']=$services->servicename;                       var_dump($serviceslist);                       }        } return $serviceslist;         } 

And here is the code which calls the function:

$services=get_services($conn,$_SESSION['valid_user']);                               echo $services['services']; 

Instead the browser echoing 2 rows I get 1...the overwriting problem takes place.

Link to comment
Share on other sites

You created an element in the array called "services" and then you keep on overwriting it in each iteration of the loop.

You need to learn more about PHP arrays.

 

The syntax provided by birbal works, but you didn't use it.

 

$a = array();$a[] = "zero";$a[] = "one";$a[] = "two";

The output would be:

Array (    [0] => "zero",    [1] => "one",    [2] => "two")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...