douyinlai2169 2013-10-31 07:51
浏览 67
已采纳

如何使用过程php从mysql获取user_id?

I am an android/java developer and Im struggling with php. I've made it as far as inserting a new user in the DB, now I want to get their ID.

What do I do with the result? I want to assign it to a variable.

$query = "SELECT user_id FROM users WHERE user_email = '" . $user_email . "'";
$result = 

PS: Im using mysql, not mysqli.

EDIT: Here is what I did:

$query = "SELECT user_id FROM users WHERE user_email = '" . $user_email ."';";
$store_info = mysql_fetch_array(mysql_query($query)); 
$user_id = $store_info['user_id'];
$response["message"] = "User created with id: " . $user_id;
echo json_encode($response);

And the error message after inserting (successfully) the user in the db:

null{"success":3,"message":"User created with id: "}
  • 写回答

5条回答 默认 最新

  • dpmfur2635 2013-10-31 07:53
    关注

    I assume that your are using MySQLi API

    $query = "SELECT user_id FROM users WHERE user_email = '$user_email'"; //Your Query
    
    $store_info = mysqli_fetch_array(mysqli_query($connection, $query)); 
    //Execute the query, fetch the result, it's just one result so no need for a while loop
    
    echo $store_info['user_id']; //echo id
    

    As per the comments, you requested a mysql_() version so here you go...

    $query = "SELECT user_id FROM users WHERE user_email = '$user_email'"; //Your Query
    
    $store_info = mysql_fetch_array(mysql_query($query)); 
    //Execute the query, fetch the result, it's just one result so no need for a while loop
    
    echo $store_info['user_id']; //echo id
    

    Still consider using mysqli_() or PDO instead. Why? Because mysql_() is now deprecated, read the red box on the documentation page which says...

    enter image description here

    Refer this answer for PDO tutorial

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line