我想从他的id中获取数据库表中的用户名 并将其放在其他数据表中
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.'/');
}
问题出在这里
$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')");
我在数据库中得到这个(资源ID#14)
答案 0 :(得分:2)
您的查询不返回数据:它返回一个资源。然后,您必须使用资源来检索数据,所以在这一行:
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
$ user_name不包含您想要的信息。
尝试:
$result = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
list($user_name) = mysql_fetch_array($result);
注意:不推荐使用mysql - 使用mysqli或PDO。原则是一样的。
答案 1 :(得分:-1)