473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

why is the php result an array of arrays for a mySQL query

Claus Mygind
571 512MB
I'm trying to figure out how to return just an array from a mySQL query with this php code.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $query = 'Select myField from myTable where id = "'.$thisValue.'"';
  3. $link = new mysqli("localhost", "my_user", "my_password", "world");
  4. $result = mysqli_query( $link , $query  );
  5. $c = mysqli_fetch_all( $result , $resulttype = MYSQLI_NUM );
  6. ?>
The result from the query looks like this.
array(
[0] =>array([0] =>4337)
[1] =>array([0] =>4910)
[2] =>array([0] =>5129)
[3] =>array([0] =>5672)
[4] =>array([0] =>5761)
)
which means I have to reference the value like this $a[$i][0] instead of just $a[$i]

How do I return a single array for a multiple rowset?

I would like the response to look something like this:

array(
[0] =>4337
[1] =>4910
[2] =>5129
[3] =>5672
[4] =>5761
)
Apr 14 '14 #1

✓ answered by Dormilich

in PDO you’d use the FETCH_COLUMN fetch mode. MySQLi has AFAIK no way to do something similar (except for using the given fetch_*() methods) so you probably have to run array_map() on your result array.

4 1501
Rabbit
12,516 Expert Mod 8TB
Is there a reason why it has to be returned as a 1 dimensional array?

What happens if you need to return 2 fields?
Apr 14 '14 #2
Claus Mygind
571 512MB
For this specific purpose, I am returning just one field. So to use the data, I must first loop through it to eliminate the inner array. I would like to eliminate that processing step.
Apr 14 '14 #3
Dormilich
8,658 Expert Mod 8TB
in PDO you’d use the FETCH_COLUMN fetch mode. MySQLi has AFAIK no way to do something similar (except for using the given fetch_*() methods) so you probably have to run array_map() on your result array.
Apr 15 '14 #4
Claus Mygind
571 512MB
Thanks dormilich, I'll take a look at that.
Apr 18 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
0
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
5
tolkienarda
by: tolkienarda | last post by:
Hi all how would i go about reseting the internal pointer in a mysql_query result I use a loop to display the results of a colom in a select box here is the loop $i=1; while ($i<=$row)...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
10
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
18
by: wizardry | last post by:
hello - i'm trying to pull this query from the ids that are displayed on the page from another query. it runs but it limits the result sets only returning one record instead of 2 that are for...
3
by: bigitup | last post by:
Hi, I have created a database on a website using phpmyadmin and I would like to display the result of a mySQL query on a webpage using PHP. I used the following code and nothing appears on my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.