我需要检查我的数组是否来自数据库(SQLSERVER,所以odbc)我有一些值。
我有这段代码:
$dbhandle = odbc_connect("Driver={SQL Server Native Client 11.0};Server=$myServer;Database=$myDB;", $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
$query = "SELECT NumNation, NumPlayers FROM table1";
$result = odbc_exec($dbhandle, $query);
while($rows = odbc_fetch_array($result)){
$myArray[] = $rows['NumNation']."=>".$rows['NumPlayers']; //this
或者我也可以这样:
$myArray[] = $rows; //or this
} // close while
echo ("<br>".implode(",<br>",$myArray).".<br>");
然后我有一些其他代码从另一个数据库表中选择然后我想检查我的数组(从第一个表)是否有这些数据。
我写了这个例子:
$NumNation = '123'
$Players = '456'
if (in_array($NumNation."=>".$NumPlayers, $myArray)) continue;
继续我的代码保存和其他东西。
但检查不起作用。
为什么?
如果我var_dump myArray我有这个:
array(363) { [0]=> string(31) " =>2000-12-31 00:00:00" [1]=> string(31) " =>2000-12-31 00:00:00" [2]=> string(31) " =>2000-12-31 00:00:00" [3]=> string(36) " 15=>2001-02-28 00:00:00" [4]=> string(36) " 18=>2001-02-28 00:00:00" [5]=> string(36) " 18=>2001-02-28 00:00:00" [6]=> string(36) " 14=>2001-02-15 00:00:00" [7]=> string(36) " 12=>2001-02-10 00:00:00" ....so on until......[347]=> string(31) " 4 =>2013-07-23 00:00:00" [348]=> string(31) " 5 =>2013-07-23 00:00:00" [349]=> string(2) "=>" [350]=> string(36) " 011013=>2013-10-01 00:00:00" [351]=> string(36) " 011013=>2013-10-01 00:00:00" [352]=> string(36) " 011213=>2013-12-01 00:00:00" [353]=> string(36) " 011213=>2013-12-01 00:00:00" [354]=> string(36) " 0112131=>2013-12-01 00:00:00" [355]=> string(36) " 0112131=>2013-12-01 00:00:00" [356]=> string(36) " 100=>2014-11-30 00:00:00" [357]=> string(31) " 1 =>2014-12-02 00:00:00" [358]=> string(31) " 1 =>2014-12-02 00:00:00" [359]=> string(31) " 1 =>2014-12-02 00:00:00" [360]=> string(26) "00032=>2014-02-12 00:00:00" [361]=> string(26) "00032=>2014-02-12 00:00:00" [362]=> string(26) "00017=>2014-02-12 00:00:00" }
答案 0 :(得分:0)
试试这个:
$dbhandle = odbc_connect("Driver={SQL Server Native Client 11.0};Server=$myServer;Database=$myDB;", $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
$query = "SELECT NumNation, NumPlayers FROM table1";
$result = odbc_exec($dbhandle, $query);
$myArray = array();
while($rows = odbc_fetch_assoc($result)){
$myArray[$rows['NumNation']] = $rows['NumPlayers'];
}
$NumNation = 123;
$NumPlayers = 456;
ksort($myArray);
foreach($myArray as $key=>$val) {
if($key == $NumNation && $val == $NumPlayers) {
continue;
}
}