gpt4 book ai didi

PHP/MySQL : querying MySQL with an array and getting results in an array

转载 作者:行者123 更新时间:2023-11-29 01:05:37 25 4
gpt4 key购买 nike

我有一个数组 $scans。我想用该数组中的所有值查询 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

?>

最佳答案

<?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...
}

关于PHP/MySQL : querying MySQL with an array and getting results in an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3505548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com