如何在PHP中将printf添加到成功的查询返回中

如何在PHP中将printf添加到成功的查询返回中,php,mysql,sql,phpmyadmin,Php,Mysql,Sql,Phpmyadmin,我有以下代码: $query = mysql_query("CALL dodaj_osobe ('$pesel','$imie','$nazwisko','$telefon','$adres','$nr_konta','$zarobek')"); print "DONE"; 但当查询不成功时,它也能工作 如何将其更改为“如果此查询成功,则printf此文本,换句话说printfnothing” 尝试使用mysqli,因为mysql\u查询已被弃用: 一个简单的mysqli-exmaple:

我有以下代码:

$query = mysql_query("CALL dodaj_osobe ('$pesel','$imie','$nazwisko','$telefon','$adres','$nr_konta','$zarobek')"); 
print "DONE";
但当查询不成功时,它也能工作

如何将其更改为“如果此查询成功,则
printf
此文本,换句话说
printf
nothing”

尝试使用mysqli,因为mysql\u查询已被弃用:

一个简单的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
}
?> 

尝试使用mysqli,因为mysql\u查询已被弃用:

一个简单的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
}
?> 

首先,停止使用旧的不推荐的
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
}
将所有
PDO::PARAM_数据类型
替换为指定占位符的var的任何数据类型。例如,如果
$pesel
是一个字符串,则替换
$st->bindValue('pesel:',$pesel,PDO::PARAM_数据类型)带有
$st->bindValue('pesel:',$pesel,PDO::PARAM_STR)。注意这个参数


如果OOP方法让您感到困惑,请使用MySQLi,因为它支持过程性方法。

首先,停止使用旧的不推荐的
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
}
将所有
PDO::PARAM_数据类型
替换为指定占位符的var的任何数据类型。例如,如果
$pesel
是一个字符串,则替换
$st->bindValue('pesel:',$pesel,PDO::PARAM_数据类型)带有
$st->bindValue('pesel:',$pesel,PDO::PARAM_STR)。注意这个参数


如果OOP方法让您感到困惑,请使用MySQLi,因为它支持过程方法。

根据PHP maual的说法:
mysql\u query()在成功时返回一个资源
,或者在出错时返回
FALSE

所以,如果你想要这个函数的精确结果,可以这样写

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

此外,从PHP5.5.0开始,mysql的函数就不推荐使用了。查看更多详细信息:

根据PHP maual:
mysql\u query()在成功时返回资源
,或在出错时返回资源

所以,如果你想要这个函数的精确结果,可以这样写

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

此外,从PHP5.5.0开始,mysql的函数就不推荐使用了。查看更多详细信息:

我认为这是一个尝试的好机会:转到并了解
mysql\u查询
函数返回的内容。另外,您会注意到,mysql_*正在被弃用-您应该切换到mysqli_*或PDO。@Nicarus-也许那个人需要学习编程。我知道这是一个愚蠢的想法,但我是一个疱疹患者。当存储过程失败时会发生什么?例如,它是否只返回空数据集?基本上,查询是否真的失败了,或者它是否返回了您可以识别为错误的数据(这对您来说是一个错误,但对MySQL来说是一个成功运行的查询)。@EdHeal-我认为您的评论与我想说的不太“PC”版本,但我同意。:-)直言不讳-@NicarusI认为这是一个尝试的好机会:转到并了解
mysql\u查询
函数返回的内容。另外,您会注意到,mysql_*正在被弃用-您应该切换到mysqli_*或PDO。@Nicarus-也许那个人需要学习编程。我知道这是一个愚蠢的想法,但我是一个疱疹患者。当存储过程失败时会发生什么?例如,它是否只返回空数据集?基本上,查询是否真的失败了,或者它是否返回了您可以识别为错误的数据(这对您来说是一个错误,但对MySQL来说是一个成功运行的查询)。@EdHeal-我认为您的评论与我想说的不太“PC”版本,但我同意。:-)直言不讳-@Nicarus