显示从数据库检索的数据的问题 [英] Problem displaying data retrieved from database

查看:46
本文介绍了显示从数据库检索的数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的网站上有一个消息系统,它使用发件人的姓名和收件人的姓名向数据库添加一条消息。检索消息以便能够读取它的功能有些作用,除了它只显示一行数据。如果有2行数据,则显示最后一行的数据两次。



这是我检索信息的功能:



Ok, so I have a message system in my website which adds a message to a database with the sender''s name and the reciever''s name. The function for retrieving the message to be able to read the it works somewhat, except that it only displays one row of data. If there is 2 rows of data it displays the last row''s data twice.

Here is my function for retrieving the message:

function getMessages($user) {
		$hostname = "localhost";
		$username = "";
		$db_password = "";
		$db_name = "tsn_database";
				
		$mysqli = @new mysqli($hostname, $username, $db_password, $db_name);
				
		if(mysqli_connect_errno()) {
			echo "Unable to connect";
		} else {
			$statement = $mysqli->prepare("SELECT * FROM messages WHERE sent_to='$user'");
			$statement->execute();
		
			$statement->bind_result($id, $date_sent, $sent_by, $sent_to, $message, $sent_by_delete, $sent_to_delete);
			$statement->fetch();
			$statement->close();
			
			$query = $mysqli->query("SELECT * FROM messages WHERE sent_to='$user'");

			if($query->num_rows > 0) {
				while($row = $query->fetch_assoc()) {
					global $result;
					$result = '<p>From: '. $sent_by .'<br/>'
					.'Date Received: '. $date_sent .'<br/>'
					.'<hr/>'
					. $message . '<br/><br/>'
					.'<a href="?cmd=delete&id='.$id.'">Delete Message</a></p>';
				}					
			} else {
				global $result;
				$result = "No Messages!";
			}
			return;
		}
	}





这是我的页面,应显示消息:





This is my page that should display the messages:

<?php
	session_start();
	
	$hostname = "localhost";
	$username = "";
	$db_password = "";
	$db_name = "tsn_database";
			
	$session = $_SESSION['valid_user'];
			
	$mysqli = @new mysqli($hostname, $username, $db_password, $db_name);
		
	if(mysqli_connect_errno()) {
		echo "Unable to connect";
	} else {
		$sql = "SELECT * FROM user_information WHERE user_email = '$session'";
		
		
		$statement = $mysqli->prepare($sql);
		$statement->execute();
		
		$statement->bind_result($id, $name, $surname, $gender, $contact, $email, $country, $age, $picture_path, $password);
		$statement->fetch();
		$statement->close();
	}

	include_once("Functions.php");

	//process deletions
	if($_GET['cmd'] == 'delete') {
		deleteMessages($_GET['id']);
	}
		
	//who is the user
	$user = $name;
	
	getMessages($user);

?>

<table>
     
        	<tr>
	        	<td> <?php echo $result ?> </td>
		</tr>

</table>





上面的代码仍然有其他代码但它没有任何内容做功能。如果您需要,请告诉我。



我在想我的函数脚本中的while循环可能有问题。但是我无法弄清楚如何解决这个问题。我仍然是一个有很多需要学习的学生,但确实需要这段代码才能工作。检索消息时,正如我所提到的,第二行显示两次。如果我删除了第二条消息,则第二条消息(相同的消息)都会消失,消息一则留在那里..它们被打印在消息一上..



但它应显示如下。



留言1



留言2



而不是:



留言1(隐藏在留言2下)



留言2



留言2





希望你了解我想说的话。



提前致谢,

Chris



The code above still has other pieces of code but it has nothing to do with the functions. Just let me know if you might need it.

I am thinking that there might be something wrong with my while loop in the functions script. But I cant figure out how to fix this.. I am still a student with much to learn but really need this piece of code to work. When the messages are retrieved, as I mentioned the second row is displayed twice. and if i delete the second message both of the second messages(which are the same) disappears and message one is left there.. They were printed over message one..

But it should display as follows.

message 1

message 2

and not as:

message 1(hidden under message 2)

message 2

message 2


Hope you understand what I am trying to say.

Thanks in advance,
Chris

推荐答案

user){
user) {


hostname = localhost;
hostname = "localhost";


用户名 = ;
username = "";


这篇关于显示从数据库检索的数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆