我遇到以下代码的问题,并且无法弄清楚我在为MySqli查询准备变量时做错了什么,有人可以帮忙吗?
我认为它可能在bind_param中的第一个参数中,但实际上我不确定。
提前致谢
<!-- Insert Bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<?
include('db.php');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_GET['id'];
// Build sql command
$sqlCommand = "SELECT * FROM queries WHERE id='{$id}'";
// Execute the query here now
$query = mysqli_query($conn, $sqlCommand) or die (mysqli_error());
//Build Table
// Output the data using a while loop
while ($row = mysqli_fetch_assoc($query)) {
echo '<h1>Query Details From Database For Query ID '.$id.'</h1>';
if ($_GET['key'] <> $row['key']) {echo '<div class="alert alert-danger" role="alert">The Key Does Not Match!</div>';}
else {echo '<div class="alert alert-success" role="alert">The Provided Key Matches For This ID</div>';}
echo '<div class="table-responsive"><table class=table table-bordered>';
echo '<tr class=striped><td>Field</td><td>Value</td></tr>';
// Gather all $row values into local variables
foreach ($row as $key => $value) {
echo '<tr><td>'.$key.'</td><td>'.$value.'</td></tr>';
}
echo '</table></div>';
}
// Free the results
mysqli_free_result($query);
// close mysql connection
mysqli_close($conn);
// DECIDE WHICH FEEDS TO QUERY
//SEND CURL REQUEST TO RELEVANT FEEDS
//GET THE RESULTS AND ADD TO LOCAL DATABASE
?>
<table>
<tr><td>Item Key<td>Brand</td><td>Resort</td><td>Price</td></tr>
<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.
if (file_exists('result.xml')) {
$xml = simplexml_load_file('result.xml');
foreach ($xml->PackageHoliday as $PackageHoliday){
echo '<tr><td>'.$PackageHoliday->ItemKey.'</td><td>'.$PackageHoliday->Brand.'</td><td>'.$PackageHoliday->HolidayResort.'</td><td>'.$PackageHoliday->PriceDetails->PricePerAdult.'</td></tr>';
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$stmt = $mysqli->prepare("INSERT INTO `search_queries`.`search_results`
(
`itemkey`,
`brand`,
`holidayresort`,
`duration`,
`boardbasis`,
`roomtypedescription`,
`deeplinkurl`,
`offerreference`,
`currency`,
`priceperadult`,
`totalpricebeforediscount`,
`totalprice`,
`adjustedprice`,
`mealsincluded`,
`transfersincluded`,
`luggageincluded`,
`hotelidentifiercode`,
`hotelname`,
`starrating`,
`hotelurl`,
`outbounddeparture`,
`outbounddepartureairport`,
`outboundarrival`,
`outboundarrivalairport`,
`outboundflightnumber`,
`outboundairline`,
`inbounddeparture`,
`inbounddepartureairport`,
`inboundarrival`,
`inboundarrivalairport`,
`inboundflightnumber`,
`inboundairline`)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssssssssssssssssssssssss", $itemkey,$brand,$holidayresort,$duration,$boardbasis,$roomtypedescription,$deeplinkurl,$offerreference,$currency,$priceperadult,$totalpricebeforediscount,$totalprice,$adjustedprice,$mealsincluded,$transfersincluded,$luggageincluded,$hotelidentifiercode,$hotelname,$starrating,$hotelurl,$outbounddeparture,$outbounddepartureairport,$outboundarrival,$outboundarrivalairport,$outboundflightnumber,$outboundairline,$inbounddeparture,$inbounddepartureairport,$inboundarrival,$inboundarrivalairport,$inboundflightnumber,$inboundairline);
$itemkey = $PackageHoliday->ItemKey;
$brand = $PackageHoliday->Brand;
$holidayresort = $PackageHoliday->HolidayResort;
$duration = $PackageHoliday->Duration;
$boardbasis = $PackageHoliday->BoardBasis;
$roomtypedescription = $PackageHoliday->RoomTypeDescription;
$deeplinkurl = $PackageHoliday->DeeplinkUrl;
$offerreference = $PackageHoliday->OfferReference;
$currency = $PackageHoliday->PriceDetails->Currency;
$priceperadult = $PricePerAdult;
$totalpricebeforediscount = $PriceDeforeDiscount;
$totalprice = $TotalPrice;
$adjustedprice = $AdjustedPrice;
$mealsincluded = $PackageHoliday->PriceDetails->Extras->MealsIncluded;
$transfersincluded = $PackageHoliday->PriceDetails->Extras->TransfersIncluded;
$luggageincluded = $PackageHoliday->PriceDetails->Extras->LuggageIncluded;
$hotelidentifiercode = $PackageHoliday->HotelDetails->HotelIdentifierCode;
$hotelname = $PackageHoliday->HotelDetails->HotelName;
$starrating = $PackageHoliday->HotelDetails->StarRating;
$hotelurl = $PackageHoliday->HotelDetails->HotelURL;
$outbounddeparture = $PackageHoliday->FightDetails->Outbound->DepartureDateTime;
$outbounddepartureairport = $PackageHoliday->FightDetails->Outbound->DepartureAirportCode;
$outboundarrival = $PackageHoliday->FightDetails->Outbound->ArrivalDateTime;
$outboundarrivalairport = $PackageHoliday->FightDetails->Outbound->ArrivalAirportCode;
$outboundflightnumber = $PackageHoliday->FightDetails->Outbound->FlightNumber;
$outboundairline = $PackageHoliday->FightDetails->Outbound->Airline;
$inbounddeparture = $PackageHoliday->FightDetails->Inbound->DepartureDateTime;
$inbounddepartureairport = $PackageHoliday->FightDetails->Inbound->DepartureAirportCode;
$inboundarrival = $PackageHoliday->FightDetails->Inbound->ArrivalDateTime;
$inboundarrivalairport = $PackageHoliday->FightDetails->Inbound->ArrivalAirportCode;
$inboundflightnumber = $PackageHoliday->FightDetails->Inbound->FlightNumber;
$inboundairline = $PackageHoliday->FightDetails->Inbound->Airline;
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$mysqli->close();
}
// print_r($xml);
} else {
exit('Failed to open test.xml.');
}
?>
</table>
//DISPLAY RESULTS
?>
错误是:
致命错误:在非对象中调用成员函数bind_param() 第104行/home/search/public_html/get-results.php
答案 0 :(得分:0)
我不使用mysqli,所以我可能会在这方面做得很好,但你可以试试这个:
$stmt = $mysqli->stmt_init();
if( $mysqli->prepare("INSERT INTO `search_queries`.`search_results`
(
`itemkey`,
`brand`,
`holidayresort`,
`duration`,
`boardbasis`,
`roomtypedescription`,
`deeplinkurl`,
`offerreference`,
`currency`,
`priceperadult`,
`totalpricebeforediscount`,
`totalprice`,
`adjustedprice`,
`mealsincluded`,
`transfersincluded`,
`luggageincluded`,
`hotelidentifiercode`,
`hotelname`,
`starrating`,
`hotelurl`,
`outbounddeparture`,
`outbounddepartureairport`,
`outboundarrival`,
`outboundarrivalairport`,
`outboundflightnumber`,
`outboundairline`,
`inbounddeparture`,
`inbounddepartureairport`,
`inboundarrival`,
`inboundarrivalairport`,
`inboundflightnumber`,
`inboundairline`
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)") ){
$stmt->bind_param("sssssssssssssssssssssssssssssssss", $itemkey,$brand,$holidayresort,$duration,$boardbasis,$roomtypedescription,$deeplinkurl,$offerreference,$currency,$priceperadult,$totalpricebeforediscount,$totalprice,$adjustedprice,$mealsincluded,$transfersincluded,$luggageincluded,$hotelidentifiercode,$hotelname,$starrating,$hotelurl,$outbounddeparture,$outbounddepartureairport,$outboundarrival,$outboundarrivalairport,$outboundflightnumber,$outboundairline,$inbounddeparture,$inbounddepartureairport,$inboundarrival,$inboundarrivalairport,$inboundflightnumber,$inboundairline);
$itemkey = $PackageHoliday->ItemKey;
$brand = $PackageHoliday->Brand;
$holidayresort = $PackageHoliday->HolidayResort;
$duration = $PackageHoliday->Duration;
$boardbasis = $PackageHoliday->BoardBasis;
$roomtypedescription = $PackageHoliday->RoomTypeDescription;
$deeplinkurl = $PackageHoliday->DeeplinkUrl;
$offerreference = $PackageHoliday->OfferReference;
$currency = $PackageHoliday->PriceDetails->Currency;
$priceperadult = $PricePerAdult;
$totalpricebeforediscount = $PriceDeforeDiscount;
$totalprice = $TotalPrice;
$adjustedprice = $AdjustedPrice;
$mealsincluded = $PackageHoliday->PriceDetails->Extras->MealsIncluded;
$transfersincluded = $PackageHoliday->PriceDetails->Extras->TransfersIncluded;
$luggageincluded = $PackageHoliday->PriceDetails->Extras->LuggageIncluded;
$hotelidentifiercode = $PackageHoliday->HotelDetails->HotelIdentifierCode;
$hotelname = $PackageHoliday->HotelDetails->HotelName;
$starrating = $PackageHoliday->HotelDetails->StarRating;
$hotelurl = $PackageHoliday->HotelDetails->HotelURL;
$outbounddeparture = $PackageHoliday->FightDetails->Outbound->DepartureDateTime;
$outbounddepartureairport = $PackageHoliday->FightDetails->Outbound->DepartureAirportCode;
$outboundarrival = $PackageHoliday->FightDetails->Outbound->ArrivalDateTime;
$outboundarrivalairport = $PackageHoliday->FightDetails->Outbound->ArrivalAirportCode;
$outboundflightnumber = $PackageHoliday->FightDetails->Outbound->FlightNumber;
$outboundairline = $PackageHoliday->FightDetails->Outbound->Airline;
$inbounddeparture = $PackageHoliday->FightDetails->Inbound->DepartureDateTime;
$inbounddepartureairport = $PackageHoliday->FightDetails->Inbound->DepartureAirportCode;
$inboundarrival = $PackageHoliday->FightDetails->Inbound->ArrivalDateTime;
$inboundarrivalairport = $PackageHoliday->FightDetails->Inbound->ArrivalAirportCode;
$inboundflightnumber = $PackageHoliday->FightDetails->Inbound->FlightNumber;
$inboundairline = $PackageHoliday->FightDetails->Inbound->Airline;
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$mysqli->close();
}