2017-12-04 8 views
-1

データベースを検索するためにテキストボックスとボタンを使用していますが、まだ開始していますこれを振る。私はこのエラーを取得しておいてください。PHP検索クエリエラー:sqlsrv_fetch_array()は、パラメータ1がリソースであることを期待しています。ブール型

警告:sqlsrv_fetch_array():2はCで有効なss_sqlsrv_stmtリソースではありません:\ RDEUsers \ NET \ 531545 \ LocationPOST.phpライン55

に何のような非常に混乱しを正確に間違っています。

 <?php 

if(isset($_POST['Search'])) 
{ 
    $SearchValue = $_POST['SearchValue']; 
    // search in all table columns 
    // using concat mysql function 
    $LocationQuery = "SELECT * FROM Location WHERE (Username, First_Name, Surname, Current_Location) LIKE '%".$SearchValue."%'"; 
    $search_result = FilterTable($LocationQuery); 

} 
else { 
    $LocationQuery = "SELECT * FROM Location"; 
    $search_result = FilterTable($LocationQuery); 
} 

// function to connect and execute the query 
function FilterTable($LocationQuery) 
{ 
$server = 'SQL2008.net.dcs.hull.ac.uk'; 
$connectionInfo = array("Database"=>"rde_531545"); 
$conn = sqlsrv_connect($server,$connectionInfo); 

$Filter_Result = sqlsrv_query($conn, $LocationQuery); 
return $Filter_Result; 
} 

?> 

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title> Location Tracker </title> 
</head> 

<center><h1> ACW Location Tracker </h1></center> 

<body> 

<form action="LocationPOST.php" method="POST"> 
<input type="text" name="SearchValue" placeholder="Username"><br><br> 
<input type="submit" name="Search" value="Search"><br><br> 


<table> 
<tr> 
<th> Username </th> 
<th> First_Name </th> 
<th> Surname </th> 
<th> Current_Location </th> 

</tr> 

<?php while($row = sqlsrv_fetch_array($search_result)):?> 

     <tr> 
        <td><?php echo $row['Username'];?></td> 
        <td><?php echo $row['First_Name'];?></td> 
        <td><?phpecho $row['Surname'];?></td> 
        <td><?php echo $row['Current_Location'];?></td>     
     </tr> 

<?php endwhile;?> 

<?php 



//set timezone to current timezone GMT 
date_default_timezone_set('Europe/London'); 

$server = 'SQL2008.net.dcs.hull.ac.uk'; 
//sets up connection to database 
$connectionInfo = array("Database"=>"rde_531545"); 
$conn = sqlsrv_connect($server,$connectionInfo); 
//creates a table called location in the database 
$LocationQuery='create table Location '; 
$LocationQuery .= '(Username int NOT NULL IDENTITY(500, 23), First_Name varchar(50) NOT NULL, Surname varchar(50) NOT NULL, Current_Location varchar(50) NOT NULL, Date date NOT NULL, PRIMARY KEY (Username))'; 
$result = sqlsrv_query($conn, $LocationQuery); 

if (!$result) 
{ 
if(($errors = sqlsrv_errors()) != null) 
{ 
foreach($errors as $error) 
{ 
echo "<p>Error: ".$error[ 'message']."</p>"; 
} 
} 
} 
else 
{ 
echo "<p>DB successfully created</p>"; 
} 
//close server connection 
sqlsrv_close($conn); 

$server = 'SQL2008.net.dcs.hull.ac.uk'; 
$connectionInfo = array("Database"=>"rde_531545"); 
$conn = sqlsrv_connect($server,$connectionInfo); 
date_default_timezone_set('Europe/London'); 
$date = new DateTime(''); 
$dateStr = $date->format("Y-m-d H:i:s"); 
$d=strtotime(""); 
$insert_query = "INSERT INTO Location (First_Name, Surname, Current_Location) VALUES (?, ?, ?, ?)"; 
$params = array("John","Doe","Hull"); 
$result = sqlsrv_query($conn,$insert_query,$params); 
$params = array("Jane","Doe","London"); 
$result = sqlsrv_query($conn,$insert_query,$params); 
$params = array("Jon","Doe","Sheffield"); 
$result = sqlsrv_query($conn,$insert_query,$params); 

//select all data from table Location in the database 
$LocationQuery='SELECT * FROM Location'; 
$results = sqlsrv_query($conn, $LocationQuery); 
// while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) 
// { 
// echo '<p>'.$row['Username'].' '.$row['First_Name'].' '.$row['Surname'].' '.$row['Current_Location']. '</p>'; 
// } 
// sqlsrv_close($conn); 

//display the associative array results in a table 
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) 
{ 
    echo "<tr><td>".$row['Username']."</td><td>".$row['First_Name']."</td><td>".$row['Surname']."</td><td>".$row['Current_Location']."</td><td>".$dateStr = $date->format("Y-m-d H:i:s")."</td></tr>"; 
} 

sqlsrv_close($conn); 
?> 

</table> 

</form> 

</body> 
</html> 

答えて

1

質問のうちの1つが失敗しています(私は"SELECT * FROM Location WHERE (Username, First_Name, Surname, Current_Location) LIKE '%".$SearchValue."%'"と思います)。

sqlsrv_queryに失敗した場合はfalseを返します(チェックしていない)sqlsrv_fetch_arrayに渡します。

関連する問題