PHP fetch MySQL array with an index - php

When fetching an array from MySQL the rows are typically returned with a key from 0 to the size of your recordset:
row[0][key][value]
Is it possible to have one of the fields from the select statement returned as the key in the array?
For example. Assuming my data set has StudentID, Name, City, etc.
How can I select into an array where I could refer to the StudentID as the index like this:
rows[StudentID][Name]
rows[StudentID][City]
etc.
Thanks!

PDOStatement::fetchAll
To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.
// Other PDO stuff to get a statement - abstract below
$result = PDOStatement::fetchAll( PDO::FETCH_COLUMN | PDO::FETCH_GROUP, 0 );
See example 3 on this page

Depending on which library you are using:
mysql_fetch_assoc()
mysqli_fetch_assoc()
PDO fetches both by default.

Related

How do I make execute with PDO output a string rather than a single-element array?

I want to select a field in my mysql database containing values separated by commas (let´s say it´s "dd,bb,ee"), so that these can be exploded and turned into an array.
However, if trying to do this:
$sql = $conn->prepare("SELECT contacts FROM Users WHERE username = ?");
$sql->execute($usernametmp);
$oldcontacts = $sql->fetch(PDO::FETCH_COLUMN);
I get this error:
Warning: PDOStatement::execute() expects parameter 1 to be array, string
given in /.../.../.../.../.../....php
on the execute line, whereas if I do the following:
$sql = $conn->prepare("SELECT contacts FROM Users WHERE username = ?");
$sql->execute(array($usernametmp));
$oldcontacts = $sql->fetch(PDO::FETCH_COLUMN);
it works, but with the db entry coming out as one array element containing "dd,bb,ee", where it´ll need to be a string in order for me to use explode on it with the comma as a delimiter.
Any idea how to fix this?
I believe the PDO fetch function returns an array, not a scalar, even if the row contains a single column.
(I'm not at all familiar with the PDO::FETCH_COLUMN style with the fetch function. Is that documented somewhere? I think that style can be used with the fetchAll function. But that will still return an array.)
The PDO fetchColumn function will return a scalar, rather than an array.
Reference: http://php.net/manual/en/pdostatement.fetchcolumn.php
(And passing bind parameters into the execute is separate unrelated issue.)

MySQLi associative array of results in PHP 5.2.6

Is there a way to fetch an associative array of results in MySQLi using PHP 5.2.6?
I know is PHP 5.3+ you can use get_result(). I know there is bind_result() in PHP 5.2.6 although there can be any number of columns in the query (not a set number.)
Been pulling my hair out over this one, ANY help will be appreciated!
Procedural :
mysqli_fetch_assoc ( mysqli_result $result )
Object Oriented :
mysqli_result::fetch_assoc ( void )
More info : http://php.net/manual/en/mysqli-result.fetch-assoc.php
Your friend is mysqli_fetch_assoc.It Fetches a result row as an associative array.
From the doumentation..
Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset

Getting a single value from an indexed array

good morning,
i need some help. am using mysql_fetch_row(), which gets a single record from a result set as an indexed array (one that refers to elements by numbers). but i cant get any value passed into my variable
//get the total number of images
$getTotal = "SELECT COUNT(*) FROM images";
$total = mysql_query($getTotal);
$rows = mysql_fetch_assoc($total);
$totalPix = $rows[0];
am trying to get an element or single recored passed into my variable totalPix, is there any other way of achieving this.
thanks you
Change your query to
$getTotal = "SELECT COUNT(*) AS Total FROM images";
and
$totalPix = $rows['Total'];
since you are using mysql_fetch_assoc that returns an associative array that corresponds to the fetched row.
Also you should move to mysqli or PDO
mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide
Your $rows is going to contain an array of fields returned, even if it's just COUNT(*).
Try:
$totalPix = $rows['COUNT(*)'];
... or name your COUNT in your query:
SELECT COUNT(*) as totalcount FROM images
...
$totalPix = $rows['totalcount'];
--edit-- removed 0 from assoc array return
Well, an associative array isn't numeric. So using the 0 index won't work.
You'll want to use $rows['COUNT(*)'] since you're using that query.

mysql In clause to avoid loop

I have to retrieve the history of a user and I have 4 tables whose data depend on each other.I can retrieve the data using loops,but I instead used the "where IN ()" clause and I implode the output of the previous query.However,if the list I provide to "where IN()" is empty it return an error.Is it that IN() cannot be empty?
When imploding an array for the IN clause, i do one of two things
1: Check if you even need to run the query at all
if(!empty($some_array)) {
//run mysql query
}
else {
// if you need to do something if the array is empty, such as error or set some defaults, do it here
}
2: A value in the array initiliser which is not ever in the database (for example, if im selecting based on a auto incrememnt id, i use zero as a default array value to stop any issues with empty data sets, as zero will never be in my id column).
$some_array = array(0);
You can add an empty value to the start, such as IN (0,your values here)

PHP: calling Oracle stored proc that returns a table

I have an Oracle stored proc that takes 2 parameters. userid as an input parameter and an Oracle table with 2 columns as second out parameter. How can I invoke the procedure from PHP? I think that the problem stands in the oci_bind_* for the second parameter. I've tried oci_bind_array_by_name but I always get PLS-00306: wrong number or types of arguments in call to GET_VALUES.
Can anyone help me, please?
Here is my code:
$tab=array();
$query = "begin GET_VALUES(:P_CUSTOMERCODE,:P_TAB); end;";
$stmt = oci_parse($ora_conn, $query) or die(oci_error());
oci_bind_by_name($stmt,":P_CUSTOMERCODE",$codUtente,255);
oci_bind_array_by_name($stmt,":P_TAB",$tab,100,100,SQLT_CHR);
oci_execute($stmt) or die(oci_error());
This might answer your problem: How to call package from php having procedure in oracle using oci drivers?
Not sure a multi-column table will work with oci_bind_array_by_name. Looking at the php manual, you can use this to bind a simple varray, assoc array or nested table, basically as simply 1 column list of values. You'd specify the type of array in the "type" param, using SQLT_CHR for varchar2 for example (if you defined an array like : type t_array is table of varchar2(100) index by pls_integer).
Seems you created a custom table of a custom record type(?), something like:
type t_rec is record (
col1 number,
col2 varchar2(100)
);
type t_tab is table of t_rec;
I don't see where you can bind to t_tab as an out param using php's oci8 calls, but I may be mistaken.

Categories