我面临一个奇怪的问题,我不知道这个问题来自哪里。我有一个 mySQL 数据库,该数据库weather-app使用该表cities上的一个表调用我有一个所有美国城镇的列表(列city和population)。让我直接在我的 SQL 数据库上运行一个查询:SELECT * FROM cities WHERE city LIKE 'boston' ORDER BY population DESC结果我得到1840000455 Boston MA 46375371840013902 Boston GA 13151840024721 Boston VA 6741840026471 Boston PA 2361840009492 Boston IN 1301840026800 Boston KY 89直接通过我的 PHP 进行的相同查询给了我:1840013902 Boston GA 13151840024721 Boston VA 6741840026471 Boston PA 2361840009492 Boston IN 1301840026800 Boston KY 89它对每个查询都执行此操作,并且仅针对第一行。我的代码:索引.html<body> <?php require_once("config/db.php")?> <h2>Weather</h2> <input type="text" name="city-search" class="city-search" id="city-search"> <ul class="result"> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="scripts/app.js"></script></body>应用程序.jssearch.addEventListener("keyup", e =>{ let city = search.value; if(city != ""){ loadCities(city); }});const loadCities = (city) => { $.ajax({ url: "config/fetch-cities.php", method: "GET", data: { query: city }, success: function(data){ results.innerHTML = data; } });}获取-cities.php<?php require("db.php");if(isset($_GET["query"])){ $search = $_GET["query"]; $results = $conn->prepare("SELECT * FROM cities WHERE city LIKE '{$search}%' ORDER BY population DESC");} $results->execute();$row_count =$results->fetchColumn();foreach ($results as $row) { echo $row["city"] . "-" . $row["population"] ."<br/>";}?>结果http://localhost:8080/weather-app/config/fetch-cities.php?query=Boston:
1 回答
互换的青春
TA贡献1797条经验 获得超6个赞
这一行:
$row_count =$results->fetchColumn();
不返回行数。它从结果集的第一行返回第一列并将指针移动到下一行,这就是为什么您foreach以后没有得到第一行的原因。
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报
0/150
提交
取消
