2014-07-06 6 views
1

특정 타임 스탬프보다 크고 특정 타임 스탬프보다 작은 열 (name-time 및 type timestamp)이있는 행만 가져 오려고합니다. 아래 코드를 사용했지만 제공하고 있습니다. 나 오류 .. 나는 그것이 바로 SQL 구문이나하지mysql의 열에서 데이터를 가져 오는 중

<?php 
include_once'header.php'; 
$time=$_GET['t']; 
$prev=$_GET['prev']; 
$q="SELECT * FROM comment WHERE time<'$time' AND time >'$prev' "; 
$r=mysql_query($q); 
while($row=mysql_fetch_array($r)) 
{ 
echo "<p>".$row[4]."</p>"; 
} 

?> 

답변

0

$firstname = 'fred'; $lastname = 'fox'; 

    // Formulate Query 
    // This is the best way to perform a SQL query 
    // For more examples, see mysql_real_escape_string() 
    $query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'", 
     mysql_real_escape_string($firstname), 
     mysql_real_escape_string($lastname)); 

    // Perform Query $result = mysql_query($query); 

    // Use result 
    // Attempting to print $result won't allow access to information in the resource 
    // One of the mysql result functions must be used 
    // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. 
    while ($row = mysql_fetch_assoc($result)) { 
     echo $row['firstname']; 
     echo $row['lastname']; 
     echo $row['address']; 
     echo $row['age']; 
    } 
( http://www.php.net/manual에서 촬영)

요점은 쿼리에 이스케이프 처리되지 않은 표현식을 사용하면 SQL 주입 방법을 제공 할 수 있기 때문입니다.

또한, 특정 쿼리에, 당신이 사이의 SQL 키워드 사용할 수 있습니다 또한 그는 mysql_query가되지 않습니다주의

$q=sprintf("SELECT * FROM comment WHERE time BETWEEN '%s' AND '%s'", 
    mysql_real_escape_string($prev), 
    mysql_real_escape_string($time)); 

을 대신 mysqli_query를 사용하려고합니다.

0

당신은 MySQL의와 연결을 못했다는 것을 알고 싶어

$conn = New mysqli(SERVER, USER, PASS, NAME); 
if (!$conn) 
{ 
die("Database connection failed: " . mysqli_error()); 
} 
$result = $conn->query("SELECT * FROM comment WHERE time<'".$time."' AND time >'".$prev."' "); 
while($row = mysqli_fetch_array($result)) 
{ 
echo "<p>".$row[4]."</p>"; 
echo "<br>"; 
} 
관련 문제