【问题标题】:Mysql return valuemysql返回值
【发布时间】:2013-06-16 19:52:00
【问题描述】:

我有一个像下面这样的小 php 页面,我想从这个插入返回“auto-incremented_id”。

唯一的要求是我可以从 android 应用程序中读取数字。我确定我可以查一下,但是否有一个代码 SQL 我可以检查成功后返回它的代码?

这是 php:

<?php

//Make connection
$con = mysqli_connect('xxxxx', 'xxxxxxx', 'xxxxx');


//check connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//change db to andriodnfp db
mysqli_select_db($con, 'andriodnfp');

$table = 'USER';

$fbid   = htmlspecialchars($_GET["fbid"]);
$social = htmlspecialchars($_GET["social"]);


$name = htmlspecialchars($_GET["name"]);
$name = !empty($name) ? "'$name'" : "NULL";

$fname = htmlspecialchars($_GET["fname"]);
$fname = !empty($fname) ? "'$fname'" : "NULL";

$username = htmlspecialchars($_GET["username"]);
$username = !empty($username) ? "'$username'" : "NULL";

$email = htmlspecialchars($_GET["email"]);
$email = !empty($email) ? "'$email'" : "NULL";

$picture = htmlspecialchars($_GET["picture"]);
$picture = !empty($picture) ? "'$picture'" : "NULL";

$other = htmlspecialchars($_GET["other"]);
$other = !empty($other) ? "'$other'" : "NULL";

if (!$fbid == '') {

    if (!mysqli_query($con, 'INSERT INTO ' . $table . ' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("' . $fbid . '","' . $social . '","' . $name . '","' . $fname . '","' . $username . '","' . $email . '","' . $picture . '","' . $other . '")')) {
        printf("Errormessage: %s\n", mysqli_error($con));
    };
}

mysqli_close($con);

//$posts = array($json);
$posts = array(
    1
);
header('Content-type: application/json');
echo json_encode(array(
    'posts' => $posts
));

?>

【问题讨论】:

  • 反正它不属于android
  • 这与如何在Anroid应用程序中读取json有关..
  • 如果有问题的代码在 PHP 和 MySQL 之间,那么即使使用其他语言,它们也不相关。
  • 感谢它已从 android 中删除,不便之处。

标签: php json mysqli auto-increment


【解决方案1】:

试试这个:mysqli_insert_id

if (mysqli_query($con, 'INSERT INTO '.$table.' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("'.$fbid.'","'.$social.'","'.$name.'","'.$fname.'","'.$username.'","'.$email.'","'.$picture.'","'.$other.'")') === false) {
    printf("Errormessage: %s\n", mysqli_error($con));
    die();
}
$id = mysqli_insert_id($con);

你也应该在出错后die

此外,您还应该将查询与数据类型进行比较===,这样可以确保它确实失败了,而不是返回一个评估为假的值。 -- 但它不太可能返回一些插入内容吗?

假设您在数据库中有一个如下所示的表:

id, username, user_level
1, dave, 0
2, jcaruso, 1

假设user_level 是一个整数,(0 = 用户,1 = 管理员)。 进行诸如SELECT user_level FROM table WHERE id=1 之类的选择将返回0,如果将其与== 进行比较,那将是正确的。 0==false.

【讨论】:

  • 非常感谢我添加了我要求的第一件事,并且我还在开始时设置了 id=0。我阅读了=== 以确保我明白你在说什么,但我仍然感到困惑。对于这个插入,我希望它返回 insert_id 就是全部。那么我会将它与什么进行类型比较呢?
  • 假设INSERT 失败。您应该将其与false 进行比较。但是,如果今天您使用的是 SELECT 语句,您可能会选择一个返回 0 的值,该值等于 false。为确保失败,您需要使用===。即使在 INSERT 这样的情况下,使用 === 也会使代码保持一致,以防万一您可能想要更改语句,但代码仍然可以正常工作。
  • 不要太密集,但你能告诉我我正在关注的实现,但我需要看看它。
  • 检查编辑,很抱歉造成混淆,这仅发生在返回数据的查询上,但最好还是使用三等号,以防万一您以后决定更改查询。跨度>
  • 好吧,我想我理解这个概念。在我的情况下,我在没有插入任何内容时返回 [null],在插入时返回 _id。您是否建议我将 [null] 设置为 0,然后将数据类型与 === 进行比较以确保它是 int。
【解决方案2】:
<?php 
//Make connection
$con = mysqli_connect('xxxxx','xxxxxxx','xxxxx') ;


//check connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//change db to andriodnfp db
mysqli_select_db($con, 'andriodnfp');

$table= 'USER';

$fbid = htmlspecialchars($_GET["fbid"]);
$social = htmlspecialchars($_GET["social"]);


$name = htmlspecialchars($_GET["name"]);
$name = !empty($name) ? "'$name'" : "NULL";

$fname = htmlspecialchars($_GET["fname"]);
$fname = !empty($fname) ? "'$fname'" : "NULL";

$username = htmlspecialchars($_GET["username"]);
$username = !empty($username) ? "'$username'" : "NULL";

$email = htmlspecialchars($_GET["email"]);
$email = !empty($email) ? "'$email'" : "NULL";

$picture = htmlspecialchars($_GET["picture"]);
$picture = !empty($picture) ? "'$picture'" : "NULL";

$other = htmlspecialchars($_GET["other"]);
$other = !empty($other) ? "'$other'" : "NULL";

//$posts = array($json);
$posts = array(1);

if (!$fbid == '') {

    if (!mysqli_query($con, 'INSERT INTO '.$table.' ( facebookID, social_outlet, Name, first_name, username, email, picture, significant_other) VALUES ("'.$fbid.'","'.$social.'","'.$name.'","'.$fname.'","'.$username.'","'.$email.'","'.$picture.'","'.$other.'")')) {
        printf("Errormessage: %s\n", mysqli_error($con));
    };

    $auto_id = 'autoincramented_id';

    //passyour primary key here
    $cql  = "SELECT MAX($auto_id) AS primary_id FROM {$table}";

    $result = mysqli_query($con, $cql);

    $id_result = mysql_fetch_assoc($result);

    //$posts = array($json);
    $posts = array('auto_increment_id'=>$id_result['primary_id']);


}

mysqli_close($con);


header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多