2017-11-05 7 views
-1

데이터베이스에 데이터를 가져오고 싶습니다. 몇 가지 데이터를 삽입했지만 지금은 검색하고 싶지만 NULL이 표시됩니다.데이터베이스에서 행 가져 오기

나는 이유를 모른다. 이 var_dump($result);가 그렇지 않으면 당신은 가능한 모든 행이 있는지 알고 $result->num_rows() 먼저 확인해야해야처럼

<?php 
require "connect.php";  
// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
$sql = "SELECT * FROM tbltemperature ORDER BY time DESC LIMIT 1"; 
$result = $conn->query($sql); 
var_dump($results); 
$t = 0; 
while($row = $result->fetch_assoc()) { 
    $weather[] = array(
        $row["time"], 
        $row["inside_temperature"] 
       ); 
    echo $row["time"]; 
    echo $row["inside_temperature"]; 
} 
$conn->close(); 
?> 
+3

이 수행 후 $의 result'과'$의 results'이 __different__ 변수'것을 __understand__ 일 경우? –

+0

@u_mulder 우리 모두는 간단한 실수를 저질렀습니다. 그것에 대해 무례 할 필요는 없습니다. '$ result'의'var_dump'는 어쨌든 유용한 정보를 제공하지 않습니다. –

+0

@MattS는 이제 답을 찾아 봅니다. 유용할까요? 아니? –

답변

0

체크 var_dump($results);, 그것은 보인다.

+0

객체 (mysqli_result) # 2 (5) {[ "current_field"] => int '0'} –

+0

'LIMIT'를 제거하십시오. ("0") [ "field_count"] => int (6) [ "길이"] => NULL [ "num_rows" 1 'SQL 문에서, 당신은 PHP MYSQL 프로그래밍 튜토리얼을 통해 갈 필요가 있다고 생각합니다. –

+0

LIMIT 1은 테스트입니다. 결국 나는 마지막 15 행을 원합니다. –

0

먼저 phpmyadmin에서 쿼리를 확인해야합니다. 쿼리가 작동하는지 여부. 당신이 var_dump($result);에 시도해야하고

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "<br> time: ". $row["time"]. " - inside_temperature: ". $row["inside_temperature"]."<br>"; 
    } 
} else { 
    echo "0 results"; 
}