2014-12-19 2 views
0

특정 시간에 sql에 추가 된 모든 행을 수신하고 각 새 레코드 (행)의 특정 열에서 값을 가져 오는 변수를 설정하는 스크립트를 만들었습니다. 이것은 내가 한 것입니다 :특정 시간에 추가 된 MySQL 정보 얻기

<?php 
header('Content-type: text/html; charset=utf-8'); 

$host='localhost'; 
$user='username'; 
$password='password'; 
$dbname='racheaqui'; 

$connection=mysql_connect('localhost:/var/run/mysqld/mysqld.sock',$user,$password) or die ("connection failed"); 

mysql_select_db($dbname, $connection); 

$query = "SELECT * FROM store WHERE date >= NOW() - INTERVAL 20 MINUTE;"; 

$result=mysql_query($query, $conexao); 

if ($result){ 

while($row = mysql_fetch_assoc($result)){ 
$email=$row['email_contact']; 
$name=$row['name']; 
$phone=$row['phone_contact']; 
} 
} 

#if (!empty($name)) { 
print_r("The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n"); 
mysql_close($connection); 
#} 
?> 

그러나이 코드를 사용하면 마지막 행의 값으로 만 변수를 설정합니다. 내가 계획하는 것은 $ variable1, $ variable2, $ variable3, $ variableX와 같이 설정하는 것입니다. $ variableX는 (배열 형식으로받은) 추가 된 행의 수입니다.

제발 좀 도와 주시겠습니까? Im 초보 PHP 코딩에 있지만, 믿어, 나는 열심히 노력하고 또한 구글과 stackoverflow의 데이터베이스에서 여기에 물어 전에 검색했습니다.

미리 감사드립니다. 이와

+0

pdo를 사용해보십시오. mysql은 쉽게 상각됩니다. –

답변

0

: 각 레코드

while($row = mysql_fetch_assoc($result)){ 
    $email=$row['email_contact']; 
    $name=$row['name']; 
    $phone=$row['phone_contact']; 
} 

당신입니다 덮어$email, $name$phone. 이 같은 루프 내부에 print_r()를 넣을 수 있습니다, 또는

$data = array(); 
while($row = mysql_fetch_assoc($result)){ 
    $data[] = array(
     'email' => $row['email_contact'], 
     'name' => $row['name'], 
     'phone' => $row['phone_contact'] 
    ); 
} 

:

$emails = array(); 
$names = array(); 
$phones = array(); 
while($row = mysql_fetch_assoc($result)){ 
    $emails[]=$row['email_contact']; 
    $names[]=$row['name']; 
    $phones[]=$row['phone_contact']; 
} 

또는 각 점포의 데이터를 하나 개의 큰 배열을 만들기 : 당신은 하나 같이 별도의 배열에서 그들을 수집 할 수 있습니다 :

while($row = mysql_fetch_assoc($result)){ 
    $email=$row['email_contact']; 
    $name=$row['name']; 
    $phone=$row['phone_contact']; 
    print_r("The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n"); 
} 
+0

위대한 @ mopo922. 첫 번째 경우, 어떻게 각 배열에서 값을 추출 할 수 있습니까? – Marcelo

+0

'$ emails [0]','$ emails [1]'등 http://php.net/manual/en/language.types.array.php – mopo922

+0

@Marcelo 중지 하시겠습니까? – mopo922

0

while 루프를 실행할 때마다 변수를 덮어 쓰게됩니다. print 문을 print $ email과 같이 while 루프 안에 넣으십시오. "
"; $ name을 인쇄하십시오. "
"; $ phone을 출력하십시오. "
"; 대신 print_r을 사용하십시오.

루프에 카운터를 놓고 인쇄 할 수도 있습니다.

0

선택한 행 수를 알고 싶으면 mysql_num_rows을 사용하십시오.

if ($result) 
{ 
    $nr_rows = mysql_num_rows($result); 
    while($row = mysql_fetch_assoc($result)) 
    { 
     $email=$row['email_contact']; 
     $name=$row['name']; 
     $phone=$row['phone_contact']; 
     print "The user ".$name." with phone ".$phone." and e-mail: ".$email." has created an account in the system.\n"; 
    } 
    print "Accounts created the last 20 minutes: ".$nr_rows; 
}