我有一个数组,$扫描。我想用该数组中的所有值查询MySQL,并将我的结果返回到数组中。例如,扫描中的样本数据为:
E1234
E2244
E3654
MYSQL表PARTS
包含字段part, size, length, plate, side, type
。
我希望$output["E1234"][0]
最终成为该部分的大小结果,1是长度等等。我希望数组可以通过MYSQL查询进行排序。 (按SIDE desc, PLATE asc
排序)。
现在,我只是单步执行$SCANS
数组并在查询后进行查询,但我无法对所有结果进行正确排序。
这可能吗?这就是我现在正在做的事情,但很明显,因为每个查询返回一行然后输出,所以没有可排序性。我希望能够执行一个查询,在数组中对结果进行排序,然后在排序后输出该数据。
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 \>';
}
};
编辑:这是代码,因为它现在遵循一些建议:
$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.
}
结果是:
警告:mysql_fetch_array():提供的参数不是第35行/home/foo/bar/sort.php中的有效MySQL结果资源
第35行是 while($ row = mysql_fetch_array($ result)){
最终编辑:
有效。
//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
?>
答案 0 :(得分:0)
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['name']. " - ". $row['age'];
?>
由http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php提供
如果您希望获得多个结果,也可以这样做:
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['age'];
}
?>
为评论添加了代码:
foreach ($scans as $currentscan) {
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
$tempQuery .= "(part = ".$partselect." OR";
}//end foreach
$tempQuery = substr($tempQuery,-2); //remove last OR
$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);
while($row = mysql_fetch_array($result)){
//do something with row in result set...
}
答案 1 :(得分:0)
您应该在PHP中构造一个WHERE子句,它允许您选择具有任何部件号的所有行。像这样:
$whereExpr = array();
for ($i = 0; $i < count($scans); $i++) {
$whereExpr[] = "part = \"{$scans[$i]}\"";
}
$whereExpr = implode(" OR ", $whereExpr);
$query = "SELECT * FROM `parts` WHERE $whereExpr ORDER BY <whatev>";
$result = array();
while ($row = mysql_fetch_array) {
$result[ $row['part'] ] = $row;
}
答案 2 :(得分:-1)
查看更新/编辑后,您可以使用IN子句
// Represents your $scans array
$arr = array('Hello','World!','Beautiful','Day!');
foreach ($arr as $currentscan) {
$partselect[] = substr(mysql_real_escape_string($currentscan), 0, 5);
}
$inExpr = implode("','",$partselect);
$query = "SELECT french, length, plate, side, type FROM parts WHERE part IN('$inExpr') ORDER BY side DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
print $partselect.' is a '.$row['french'].'/'.$row['length'].' - '.$row['type'].' - '.$row['plate'].'('.$row['side'].')<br \>';
}