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 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写