Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

How to get the user_id from mysql using procedural php?

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: "}

Upvotes: 2

Views: 527

Answers (5)

Mr. Alien
Mr. Alien

Reputation: 157314

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

Upvotes: 6

user4035
user4035

Reputation: 23729

Your error comes because of the error in SQL query: you used = operator twice:

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

Must be:

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

Upvotes: 0

Sandesh
Sandesh

Reputation: 349

Connection:

define("HOST","localhost");
define("USER","mysql_username");
define("PASS","password");

$conn = mysql_connect(HOST,USER,PASS) or die("<h3>Sorry, could not connect to MySQL. Please Try Again</h3>");
$db = mysql_select_db(DBNAME,$conn) or die("<h3>Sorry, could not connect to Database. Please Try Again</h3>")

Query:

$query = "SELECT user_id FROM users WHERE user_email = = '" . $user_email . "'";
$result = mysql_query($query);
$row=mysql_fetch_assoc($result);

Upvotes: 0

onionpsy
onionpsy

Reputation: 1522

If you use mysql (but you shouldn't, it's deprecated) :

$result = mysql_query("SELECT user_id FROM users WHERE user_email = '$user_email'");
$row = mysql_fetch_row($result);

echo $row[0]; // you result (id)

Upvotes: 0

user4035
user4035

Reputation: 23729

Here is the PDO variant:

<?php
//credentials
$host = 'localhost';
$user = "user";
$password = '';
$db_name = 'test';
$port = 3306;

//connection to the database
try
{
    $connection = new PDO("mysql:host=$host;port=$port;dbname=$db_name", $user, $password);
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}

//prepare and execute SELECT statement
$sth = $connection->prepare("SELECT user_id FROM users WHERE user_email = :email");
$sth->execute(array(':email' => $user_email));

$record = $sth->fetch(PDO::FETCH_ASSOC);
print $record["user_id"];

Upvotes: 3

Related Questions