abstract-data-type


Foreach PHP Loop into Array


I am trying to create an array containing results from an SQL table. But my foreach loop does not appear to be function correctly.
Here is my code:
$stmt->bind_param("i", $id);
$stmt->execute();
while ($stmt->fetch()) {
}
$stmt->close();
$i = 0;
foreach ($connected_items as &$value) {
print_r ($connected_items[$i]);
$stmt->bind_param("i", $connected_items[$i]);
$stmt->execute();
while ($stmt->fetch()) {
$result[] = array(,
);
}
$stmt->close();
$i++;
}
unset($value);
)
)
It appears to be running the for loop on my print statement and then MySQL statement, using the last connected_items_id, why is that? And why is it not returning the associated values for that id?
remove this [] at line number 6:
while ($stmt->fetch()) {
$connected_items = array($connected_items_id);
}
instead of this:
while ($stmt->fetch()) {
$connected_items[] = array($connected_items_id);
}
Try this,
"connected_item_id" => $connected_items[0],
instead of
"connected_item_id" => $connected_items_id,
Try
foreach ($connected_items as $value) {
......................
......................
$stmt = $this->db->prepare('SELECT name, serial FROM `glpi_monitors` WHERE id =?');
$stmt->bind_param("i", $value[0]);
................................
................................
}
Also you can use IN keyword instead of looping and querying each time.
$sql = "SELECT name, serial FROM `glpi_monitors` WHERE id IN('".implode("','",$connected_items)."'");
$stmt = $this->db->prepare($sql);
eg :

Related Links

AbstractDataType - Nat
Foreach PHP Loop into Array

Categories

HOME
django
jmeter
dns
alexa-skills-kit
solver
vscode-extensions
tivoli
3d-reconstruction
vsm
spring-social
entitymanager
dimensional-modeling
fileserver
google-data-studio
google-container-registry
production
connector
asciidoctor-pdf
displaytag
jboss6.x
onload
maze
flooding
underscore.js
bioinformatics
realm-mobile-platform
complex-numbers
dumpbin
baidu-map
raft
doc
apriori
dcmtk
plr
xmldocument
chai-as-promised
boxing
java-6
skin
cup
otp
pybrain
branch-prediction
cudd
cubism.js
mapxtreme
envi
wso2developerstudio
tooleap
lua-telegram-bot
lz77
nupic
jaunt-api
phpgrid
jnlua
toran-proxy
dynatree
dllimport
microsoft-reporting
flock
rubaxa-sortable
binomial-theorem
.bash-profile
canalyzer
lasso
eoferror
typeconverter
kubuntu
android-broadcast
dbaccess
big-ip
appserver
raygun
android-framework
android-audiorecord
windows-embedded-standard
cascadingdropdown
bjyauthorize
android-sdk-2.3
jquery-blockui
hibernateexception
jquery-tools
creole
nhprof
squishit
ie-compatibility-mode
maven-1
binomial-heap
dojo-dnd
fault-tolerance
technical-debt
business-model
wspbuilder
netbeans6.1

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App