2

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: "}
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • I added a new answer: you have a syntax error in SQL – user4035 Oct 31 '13 at 08:04
  • 1
    [Please, stop using mysql_* functions](http://stackoverflow.com/q/12859942/1238019) in new code, they are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Instead of, have a look on [prepared statements](http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html), and use [Mysqli](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php). – zessx Oct 31 '13 at 08:21
  • I just wanted to get my api up and running quickly. When I do that I will start learning php and mysqli and pdo. Just wanted quick results so I can motivate myself – Kaloyan Roussev Oct 31 '13 at 08:24
  • @J.Kowalski So, you got an empty result from the database. Does user with this email exist in the database? Can you show us the dump? – user4035 Oct 31 '13 at 08:27
  • INSERT INTO `users` (`user_id`, `user_name`, `user_email`, `user_password`, `user_sex`, `user_avatar`, `user_date_registered`, `user_last_seen`, `user_points`) VALUES (18, 'asd', 'asd2e', 'kirobr', '', '', '2013-10-31 08:22:12', '0000-00-00 00:00:00', 0); – Kaloyan Roussev Oct 31 '13 at 08:29
  • @J.Kowalski I don't see any email in your query – Mr. Alien Oct 31 '13 at 08:31
  • well I just inserted some random symbols (the debugging form has no validation) for testing – Kaloyan Roussev Oct 31 '13 at 08:34

5 Answers5

6

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

Community
  • 1
  • 1
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
3

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"];
user4035
  • 22,508
  • 11
  • 59
  • 94
0

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)
onionpsy
  • 1,502
  • 11
  • 15
0

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);
Sandesh
  • 349
  • 2
  • 8
0

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 . "'";
user4035
  • 22,508
  • 11
  • 59
  • 94