2013-06-20 69 views
-2

我有一個像下面這樣的小php頁面,我想從此插入中返回「auto-incremented_id」。Mysql返回值

唯一的要求是我可以從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

反正它不屬於'android'。 –

+0

這與如何在Anroid應用程序中讀取json有關。 –

+0

如果所討論的代碼位於PHP和MySQL之間,那麼即使使用其他語言,它們也不相關。 –

回答

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

+0

非常感謝我添加了我要求的第一件事,並且我還在開始時設置了id = 0。我讀了'==='以確保我明白你在說什麼,但我仍然感到困惑。對於這個插入,我希望它返回所有的insert_id。那麼我會比較一下類型比較? – jcaruso

+0

假設'INSERT'失敗。你應該把它與'false'進行比較。但是,如果今天您使用的是SELECT語句,則可能會選擇返回「0」的值,該值等於false。爲了確保它失敗了,你需要使用'==='。即使在像INSERT這樣的情況下,使用'==='也會使代碼保持一致,以防萬一您可能想更改語句,但代碼仍然正常工作。 –

+0

不是很密集,但你能告訴我實施我有點跟隨,但我需要看到它。 – jcaruso

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

?>