2010-08-17 114 views
3

我有一個數組$ scans。我想用該數組中的所有值查詢MySQL,並將結果返回到數組中。例如,在掃描樣本數據將是:PHP/mysql:使用數組查詢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():提供的參數不是一個有效的MySQL結果第35行的/home/foo/bar/sort.php中的資源

第35行是 while($ row = mysql_fetch_array($ result)){

FINAL編輯:

它的工作原理。

//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 

?> 
+2

讓我們看看你的代碼! – BoltClock 2010-08-17 18:11:36

+0

Stephen想要查看構建的查詢。這可能不完全正確。在迭代之前,請檢查num_rows以查看是否有結果。 – Chris 2010-08-17 20:45:54

回答

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

您還可以,如果你希望多結果IN子句做:

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

這不是我正在尋找的。更新代碼示例更加清晰。 – Screevo 2010-08-17 18:19:20

+0

@stephen我更新了你的代碼,首先爲$ scans數組中的所有項目構建查詢,然後遍歷結果集...... – Chris 2010-08-17 18:28:10

+0

這似乎可以工作,但substr語法看起來不對,因爲之後$ tempQuery =「R」(R和一個空格) – Screevo 2010-08-17 19:38:47

-1

MySQL Fetch Array Function

看你的更新/編輯後,您可以使用

// 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 \>'; 
} 
0

Whay你應該做的是構建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; 
} 
+0

ooh。這會創建一個giganto查詢。我喜歡這個。嘗試它。 – Screevo 2010-08-17 18:21:05

+0

如果這是用戶爲$ whereExpr生成的內容,那麼你會打開你的數據庫到SQL注入:http://en.wikipedia.org/wiki/SQL_injection – 2010-08-17 18:37:06

+0

是的,當然你應該使用mysql_real_escape_string等等。這只是一些快速彙總的示例代碼。 – eliego 2010-08-17 18:38:04