Mysql返回值

时间:2013-06-20 04:47:23

标签: php json mysqli auto-increment

我有一个像下面这样的小页面,我想从这个插页中返回“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
));

?>

2 个答案:

答案 0 :(得分:3)

试试这个: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

此外,您应该将查询与数据类型以及===进行比较,这可以确保它真正失败,而不是返回一个计算结果为false的值。 - 但它不太可能为插入物返回一些东西吗?

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

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

答案 1 :(得分:1)

<?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));

?>