J'ai un tableau, $ scans. Je veux interroger MySQL avec toutes les valeurs de ce tableau et récupérer mes résultats dans un tableau. Par exemple, les données d'échantillons dans les analyses seraient:PHP/mysql: interroger MySQL avec un tableau et obtenir des résultats dans un tableau
E1234
E2244
E3654
Le tableau MYSQL PARTS
a des champs part, size, length, plate, side, type
. Je veux finir avec $output["E1234"][0]
pour être le résultat de taille pour cette partie, 1 pour être longueur, etc. et je veux que le tableau soit triable par la requête MYSQL. (commande par SIDE desc, PLATE asc
).
En ce moment, je ne fais que passer par le tableau $SCANS
et faire une requête après la requête, mais je ne suis pas capable de trier tous les résultats correctement.
Est-ce possible? C'est ce que je fais maintenant, mais évidemment, puisque chaque requête renvoie une ligne qui est ensuite sortie, il n'y a pas de triabilité. Je veux être en mesure d'effectuer une requête, trier les résultats dans le tableau, puis sortir ces données après le tri.
foreach ($scans as $currentscan) {
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
$query = "SELECT french, length, plate, side, type FROM parts WHERE part = '$partselect' ORDER BY side DESC, plate ASC LIMIT 1";
$result = mysql_query($query);
#echo 'query is '.$query.' <br>';
$error = mysql_error();
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
echo 'BAD PART: '.$currentscan.' '.$partselect.' error is '.$error.'<br \>
';
} else {
$row = mysql_fetch_array($result);
print $partselect.' is a '.$row['french'].'/'.$row['length'].' - '.$row['type'].' - '.$row['plate'].'('.$row['side'].')<br \>';
}
};
EDIT: Ceci est le code tel qu'il suit quelques suggestions ici:
$scans = explode("\n",$_POST['scans']);
foreach ($scans as $currentscan) {
if ($currentscan[0] == "E") { //this is a cheap trick to ignore a bad scan inherent in the scanning mechanism
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
$tempQuery .= 'part = "'.$partselect.'" OR ';
};
};//end foreach
$tempQuery = substr($tempQuery, 0, -3); //remove last OR (fixed substr to 0,-3 to scrip final OR - stephen)
$tempQuery .= ") ORDER BY side DESC, plate ASC LIMIT 1"; //add on end of query
$query = "SELECT french, length, plate, side, type FROM parts WHERE ".$tempQuery;
$result = mysql_query($query);
echo $result;
while($row = mysql_fetch_array($result)){
print $row['french']."/".$row['length']; //just doing something pointless to verify data was pulled.
}
résultat
est:
Warning: mysql_fetch_array(): argument fourni est pas un résultat MySQL ressource dans /home/foo/bar/sort.php sur la ligne 35
La ligne 35 est while ($ row = mysql_fetch_array ($ result)) {
FINAL EDIT:
Cela fonctionne.
//DECLARED CONSTANTS//
if (!$_POST) { // Have scans been entered? If not, display the form.
print '
<html>
<body>
Scans:
<form action="index.php" method="post">
<textarea rows="20" cols="6" name="scans" id="scans">
</textarea>
<br />
<input type="submit" value="Submit" />
</body>
</html>
';
} else { //Scans have been entered. Start scan processing logic
//==openDB==//
mysql_connect(SQLSERVER, SQLUSER, SQLPASSWORD) or die("Can not connect to DB server.");
mysql_select_db(DATABASE) or die("Can not connect to Database.");
//==openDB==//
$scans = explode("\n",$_POST['scans']); // Explode posted scans into scans array
foreach ($scans as $currentscan) { // step through each scan
if ($currentscan[0] == "E") { //Cheap check for real part numbers.
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5); // Strip off the extraneous data from the scan
$count{$partselect}++; //Count instances of particular parts. ideally this would be in an array in the form of $count[$partnumber] so I can easily display that data at the end. each part needs to be displayed every time it's scanned.
$tempQuery .= 'part = "'.$partselect.'" OR '; //Starts building query
};
};//end foreach
$tempQuery = substr($tempQuery, 0, -3); //remove last OR
$tempQuery .= ") ORDER BY side DESC, plate ASC"; //add on end of query
$query = "SELECT part, french, length, plate, side, type FROM parts WHERE (".$tempQuery; //Form beginning of query
$result = mysql_query($query);// execute query
while($row = mysql_fetch_array($result)){ // step through results
for ($i = 0; $i < $count{$row['part']}; $i++) { //if a part was scanned $count{$row['part']} times, display it that many times
print $row['part']." ".$row['french']."/".$row['length']." ".$row['plate']."(".$row['side'].")<br>"; //data parsing goes here. this will be expanded.
};// close for loop
};//close while loop
};//close else
?>
Voyons voir votre code! – BoltClock
Stephen que vous voulez regarder la requête construite. Ce n'est probablement pas totalement correct. Avant d'itérer, vérifiez num_rows pour voir si vous avez des résultats. – Chris