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)

3
  • "SELECT name FROM users WHERE user_id = ".$_SESSION['user_id']."") Commented Aug 21, 2013 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. Commented Aug 21, 2013 at 22:50
  • thanks GreatBigBore it's working perfectly Commented Aug 21, 2013 at 22:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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.

1 Comment

He might be able to improve the efficiency of the queries this way, but the problem is in PHP and the use of mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.