Jump to content

Recommended Posts

I am new at PHP, please help me with the following question:

 

$query = “SELECT * FROM Member WHERE loginName=’gsmith

...

$result = mysqli_query($cxn,$query)

while ($row = mysqli_fetch_assoc($result))

{

extract($row)

}

 

In this case we get variables $password="sdfsd" $email="[email protected]" $loginName="gsmith" $name="Nick"

 

But what happen if the data are as following:

 

$query = “SELECT DISTINCT name FROM Member ORDER BY. Let's say we have 4 persons with the same name.

I guess that multimension arrays will be created because we have 4 names and for each name all data (password, e-mail, log in and name). I don't understand how the multimension arrays will be created in this case. What will be as the key and what as the value and how variables will be created with function extract($row) in case of multimension arrays?

 

Thank you

 

 

Link to comment
https://forums.phpfreaks.com/topic/139919-mysqli_fetch_assocresult/
Share on other sites

Queries never return multi-dim arrays.  At least, not in a form you can directly use.  They will always return a result source, and your _fetch_assoc would pull out the info 1 row at a time (each time you call it).  So when you call _fetch_assoc, you get a single-dim array of the current row.

 

SELECT DISTINCT name FROM Member

 

would return just 1 column 'name' and each row would only contain a single name.  So for instance, if your table looks like this:

 

name

John

Mary

Joe

Mary

John

John

Joe

 

You would get

 

Joe

John

Mary

 

So you would do something like this:

 

$query = "SELECT DISTINCT name FROM Member";
$result = mysqli_query($cxn,$query);
while ($row = mysqli_fetch_assoc($result)) {
   echo $row['name'] . "<br/>";
}

 

And it would echo out

 

Joe

John

Mary

 

Perhaps you should clarify your situation.  I interpreted your original post to say that you thought that doing a select distinct would somehow return a multi-dimensional array.  Well, it doesn't.  Nor does any query.  At least, not in this context.  _fetch_whatever pulls one row at a time.  That's why you put it into a loop.

 

You mentioned something about having 4 of the same name.  Well, what do you want to happen? Do you only want a list of names returned, no duplicates? Use the select distinct. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.