-1
데이터베이스를 검색하기 위해 텍스트 상자와 버튼을 사용하여 테이블에 표시되도록하려고합니다. 이것에 동요.PHP 검색 쿼리 오류 : sqlsrv_fetch_array()는 매개 변수 1이 리소스가 될 것으로 예상하고, 부울
경고 : sqlsrv_fetch_array() :이 오류가 계속 2 C에 유효한 ss_sqlsrv_stmt 자원되지 않습니다 :에 관해서는 \ RDEUsers 라인 NET \ 531,545 \ 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>