user3306996
user3306996

Reputation: 421

Android: MySQL database SQL statements

I have two tables. One table storing the user details and the other stores the files created by the user. I am now trying to save the userid, filename and moddate into the files table.

users table contains: userid, unique_id( hashed value ), username

files table contains: id, userid, filename, moddate

I could get hold of the unique_id value and i used the sql statement below to get the userid from the users table.

$id = mysqli_query($con,"SELECT userid FROM users WHERE unique_id= '$userid'");


 $result = mysqli_query($con,"INSERT INTO files(userid, name, mod_date) VALUES('$id','$name', NOW())");

But the userid value added in files table in the database keeps having the value 0. Did i do it the right way? Is the Insert statement correct?

Correction:

$id = mysqli_query($con,"SELECT userid FROM users WHERE unique_id= '$userid'");

$rowid = mysqli_fetch_row($id);


    $result = mysqli_query($con,"INSERT INTO file_info(userid, name, mod_date) VALUES('$rowid','$name', NOW())");

Upvotes: 0

Views: 71

Answers (1)

user3522003
user3522003

Reputation:

The mysqli_query will not return the selected id. It returns the mysqli_result object. You have to use mysqli_fetch_xxx functions to fetch the rows and get data from them. See the example here: http://www.php.net/manual/mysqli-result.fetch-row.php Look at this code:

$result = mysqli_query($con, "SELECT userid FROM users WHERE unique_id = '$userid'");
$row = mysqli_fetch_row($result);

if ($row){
    $id = $row[0];
    ...
    $result = mysqli_query($con, "INSERT INTO file_info (userid, name, mod_date)  VALUES('$id','$name', NOW())");    
} 

Upvotes: 1

Related Questions