0

I want to get user name from a database table from his id and put it in other data table

function upload_image($image_temp, $image_ext, $album_id, $image_n, $image_description) {
$album_id = (int)$album_id;
$image_n = mysql_real_escape_string(htmlentities($image_n));
$image_description = mysql_real_escape_string(htmlentities($image_description));
//$download_link = 'uploads/'. $album_id. '/'. $image['id']. '.'. $image_ext;
$mysql_date_now = date("Y-m-d (H:i:s)");
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);

mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."','$user_name', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_n', '$image_description', '','$mysql_date_now')");


$image_id = mysql_insert_id();
$download_link = 'uploads/'. $album_id. '/'. $image_id. '.'. $image_ext;

mysql_query("UPDATE `images` SET `download_link`='$download_link' WHERE `image_id`=$image_id ");
$selection = mysql_query("SELECT `user_two` FROM `follow` WHERE `user_one`='".$_SESSION['user_id']."'");

while ($row = mysql_fetch_array($selection)) {
mysql_query("INSERT INTO `notification` VALUES ('', '".$_SESSION['user_id']."', '".$row['user_two']."', '', UNIX_TIMESTAMP(), '$image_n', '$image_description', '$download_link')");
}

$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);

Thumbnail('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');

}

the problem is here

$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);

mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."','$user_name', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_n', '$image_description', '','$mysql_date_now')");

i get this in database (Resource id #14)

user2687618
  • 59
  • 3
  • 13
  • "SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']."") – Mihai Aug 21 '13 at 22:46
  • 2
    `mysql_query` returns a resource, not a database field. You need to do `$resource = mysql_query("SELECT \`name\`...)` and then `$record = mysql_fetch_array($resource)` and then `$user_name = $record['user_name']`. You did it correctly with your `$selection` query. – SaganRitual Aug 21 '13 at 22:50
  • thanks GreatBigBore it's working perfectly – user2687618 Aug 21 '13 at 22:56

2 Answers2

2

Your query doesn't return data: it returns a resource. You then have to work with the resource to retrieve your data, so in this line:

$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);

$user_name doesn't contain the information you want.

Try:

$result = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
list($user_name) = mysql_fetch_array($result);

Note: mysql is deprecated - use mysqli or PDO. The principle is the same.

-1

I think you should look here: MySQL syntax for Join Update

Or here: MySQL Insert & Joins

You could do this in just one query with some joining of the tables.

Community
  • 1
  • 1
denlau
  • 916
  • 2
  • 9
  • 21
  • He might be able to improve the efficiency of the queries this way, but the problem is in PHP and the use of `mysql`. –  Aug 21 '13 at 22:56