2013-01-07 8 views
0

에서 성공적인 쿼리 반환에의 printf를 추가하는 방법 : 쿼리뿐만 아니라 성공적으로 실행되지 않으면이 코드가 어떻게 PHP

$query = mysql_query("CALL dodaj_osobe ('$pesel','$imie','$nazwisko','$telefon','$adres','$nr_konta','$zarobek')"); 
print "DONE"; 

을하지만 작동합니다.

"이 쿼리를 성공적으로 수행 한 다음 printf이 텍스트를 다른 방법으로 사용하면 printf을 아무것도"로 변경할 수 있습니까? 는 mysql_query가되지 않기 때문에

+1

나는 이것을 시도 할 수있는 좋은 기회라고 생각합니다. [documentation] (http://php.net/manual/en/book.mysql.php)에서 mysql_query 함수가 리턴한다. 또한 mysql_ *가 더 이상 사용되지 않음을 알 수 있습니다. mysqli_ 또는 PDO로 전환해야합니다. –

+0

@ Nicarus - 아마도 그 사람은 프로그래밍을 배워야합니다. 나는 그것이 어리석은 생각 인 것을 알고 있습니다. 그러나 저는 헤리 티스트입니다. –

+0

저장 프로 시저가 실패하면 어떻게됩니까? 예를 들어 빈 데이터 세트 만 반환합니까? 기본적으로 쿼리는 실제로 실패합니까? 또는 오류로 인식 할 수있는 데이터를 반환합니다 (이는 오류가 있지만 MySQL에 성공적으로 쿼리를 실행 함). –

답변

2
$result = mysql_query('your SQL query'); 
if (!$result) { 
    print('error: ' . mysql_error()); // an error occured 
} 
else 
{ 
    print('Done'); 
} 

시도는 mysqli를 사용 : mysqli manual

간단한 mysqli의 exmaple :

<?php 
// New Connection 
$db = new mysqli('localhost','user','pass','database'); 
$result = $db->query("your SQL query"); 
if($result) 
{ 
    // Cycle through results 
    while ($row = $result->fetch_object()) 
    { 
     $group_arr[] = $row; 
    } 
    // Free result set 
    $result->close(); 
    $db->next_result(); 
} 
else 
{ 
    echo($db->error); // an error occured, print the error 
} 
?> 
1

글쎄, 우선, 이전이 mysql_* 사용되지 않는 사용을 중지합니다.

나는 원하지 않기 때문에 mysql_*으로 원하는 것을하는 방법을 보여주지 않을 것입니다. PDO, 당신은 할 것 :

$db = new PDO('mysql:host=dbHost;dbName=yourDBName;' , 'user' , 'pass'); 

try 
{ 
    $st = $db->prepare("CALL dodaj_osobe (pesel:,imie:,nazwisko:,telefon:,adres:,nr_konta:,zarobek:)"); 
    $st->bindValue('pesel:' , $pesel , PDO::PARAM_dataType); 
    $st->bindValue('imie:' , $imie , PDO::PARAM_dataType); 
    $st->bindValue('nazwisko:' , $nazwisko , PDO::PARAM_dataType); 
    $st->bindValue('telefon:' , $telefon , PDO::PARAM_dataType); 
    $st->bindValue('adres:' , $adres , PDO::PARAM_dataType); 
    $st->bindValue('nr_konta:' , $nr_konta , PDO::PARAM_dataType); 
    $st->bindValue('zarobek:' , $zarobek , PDO::PARAM_dataType); 
    $st->execute(); 
} 
catch(PDOException $qEx) 
{ 
    //the query wasn't successful.. 
    //deal with it 
} 

가 지정된 자리 표시 자에 대한 VAR는 어떤 데이터 유형을 가진 모든 PDO::PARAM_dataType를 교체합니다. 예를 들어 $pesel이 문자열 인 경우 $st->bindValue('pesel:' , $pesel , PDO::PARAM_dataType);$st->bindValue('pesel:' , $pesel , PDO::PARAM_STR);으로 대체하십시오. PARAM_STR?을 확인하십시오.

OOP가 당신을 혼란스럽게하는 경우, 절차 적 접근을 지원하므로 MySQLi를 사용하십시오.

0

PHP 실제 결과 : mysql_query() returns a resource on success 또는 오류시 FALSE에 해당.

이 함수의 정확한 결과를 원하면 다음과 같이 작성할 수 있습니다.

$result = mysql_query("SELECT * FROM table"); 
if (false !== $result) { 
    // it's ok! 
} else { 
    // error! 
} 

또한 mysql_* 함수는 PHP 5.5.0부터 사용되지 않습니다. 자세한 내용보기 : http://php.net/manual/en/function.mysql-query.php