close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Creating & accessing arrays from MySQLi Recordsets

Thread began 3/18/2016 4:46 am by Dave Buchholz | Last modified 3/21/2016 3:49 pm by Ray Borduin | 1155 views | 4 replies |

Dave BuchholzBeta Tester

Creating & accessing arrays from MySQLi Recordsets

so my recordset looks like this

$rsDataSet = new WA_MySQLi_RS("rsDataSet",$connMySQLi,5);
$rsDataSet->setQuery("SELECT tbl_products.pkSKU, tbl_products.CommonBrandName, tbl_products.ModelDescription, tbl_products.SKU, tbl_products.ColorDescription, tbl_products.MSRP FROM tbl_products");
$rsDataSet->execute();

Doing a print_r on $rsDataSet shows me that the recordset results are held in an array called Results:

print '<pre>';
print_r($rsDataSet);
print '</pre>';
WA_MySQLi_RS Object
(
[Connection] => mysqli Object
(
[affected_rows] => -1
[client_info] => 5.5.42
[client_version] => 50542
[connect_errno] => 0
[connect_error] =>
[errno] => 0
[error] =>
[field_count] => 6
[host_info] => Localhost via UNIX socket
[info] =>
[insert_id] => 0
[server_info] => 5.5.42
[server_version] => 50542
[stat] => Uptime: 251213 Threads: 7 Questions: 12860 Slow queries: 0 Opens: 216 Flush tables: 1 Open tables: 209 Queries per second avg: 0.051
[sqlstate] => 00000
[protocol_version] => 10
[thread_id] => 138
[warning_count] => 0
)

[CurrentPage] => /test-files/mysqli/index.php
[Debug] => 1
[FilterValues] => Array
(
)

[Index] => 0
[LastRow] => 15
[MaxRows] => 5
[Name] => rsDataSet
[NextPage] => 3
[PageNum] => 2
[ParamTypes] => Array
(
)

[ParamValues] => Array
(
)

[ParamDefaults] => Array
(
[0] => -1
[1] => -1
[2] => -1
[3] => -1
)

[PrevPage] => 1
[QueryString] => &totalRows_rsDataSet=207
[Results] => Array
(
[0] => Array
(
[pkSKU] => 128879
[CommonBrandName] => Weber
[ModelDescription] => Performer® Silver Charcoal Grill
[SKU] => 1401001
[ColorDescription] => Black
[MSRP] => 0.00
)

[1] => Array
(
[pkSKU] => 127609
[CommonBrandName] => Weber
[ModelDescription] => Performer® Charcoal Grill
[SKU] => 1411001BK
[ColorDescription] => Black
[MSRP] => 0.00
)

[2] => Array
(
[pkSKU] => 105603
[CommonBrandName] => Weber
[ModelDescription] => Performer® Charcoal Grill
[SKU] => 1421001PERF
[ColorDescription] => Black
[MSRP] => 0.00
)

[3] => Array
(
[pkSKU] => 112433
[CommonBrandName] => Weber
[ModelDescription] => Performer® Charcoal Grill
[SKU] => 1424001
[ColorDescription] => Brick Red
[MSRP] => 0.00
)

[4] => Array
(
[pkSKU] => 112434
[CommonBrandName] => Weber
[ModelDescription] => Performer® Charcoal Grill
[SKU] => 1427001
[ColorDescription] => Green
[MSRP] => 0.00
)

)

[StartLimit] => 10
[StartRow] => 11
[Statement] => SELECT tbl_products.pkSKU, tbl_products.CommonBrandName, tbl_products.ModelDescription, tbl_products.SKU, tbl_products.ColorDescription, tbl_products.MSRP FROM tbl_products
[Table] =>
[TotalPages] => 41
[TotalRows] => 207
)


And I have found I can create an array like so:
$DataSet = $rsDataSet->Results;

But what I would like to do if possible is create an array using only one of the fields, pkSKU in this case but I can't seem to get the code correct, is this possible and if so how do I access it ?

Thanks

Sign in to reply to this post

Christopher WestCommunity Expert

Hey Dave long time since we last spoke. I wondered if you could use something like what I have done (obviously my code was for a different purpose but its taking a single field from a recordset as an array and outputting it. I wondered if taking a look at my code and seeing how it works can help you solve your question?

php:
<?php require_once('../Connections/ecartdb.php'); ?>

<?php 
require_once('../webassist/mysqli/rsobj.php'); ?>
<?php
header
('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=emails.csv');

$output fopen('php://output''w');

$rows = new WA_MySQLi_RS("rows",$ecartdb,0);
$rows->setQuery("SELECT NewsLetterEmail FROM newsletter");
$rows->execute();

while(!
$rows->atEnd()) {
    
    
$row = array($rows->getColumnVal("NewsLetterEmail"));


fputcsv($output$row);

$rows->moveNext();
}
$rows->moveFirst(); //return RS to first record
?>



Regards
Chris

Sign in to reply to this post

Dave BuchholzBeta Tester

Hi Chris, hope you are well.

I am looking to see if I can use the "shorthand" method rather than having to loop through the recordset to create the array but thanks for your input.

Sign in to reply to this post

Ray BorduinWebAssist

You could loop through the Results array directly like:

php:
<?php

$pkSKUArray 
= array(); 
for (
$x=0$x<sizeof($rsDataSet->Results); $x++) {
  
$pkSKUArray[] = $rsDataSet->Results[$x]['pkSKU'];
}
?>
Sign in to reply to this post
Did this help? Tips are appreciated...

Dave BuchholzBeta Tester

now why didn't I think of that, thank you Ray that is exactly what I am looking for.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...