Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

PHP
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
<?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>


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
Posted
Updated 19-Mar-13 1:29am
v3
Comments
max_nowak 19-Mar-13 8:01am    
I believe it might have something to do with that you execute the query for fetching the message twice. Once by $statement->execute() and the second time with mysqli->query(...). I could try this, dunno if it would work (I haven't worked with php a while).

...

/* execute query */
$stmt->execute();

/* instead of bind_result: */
$result = $stmt->get_result();

/* now you can fetch the results into an array - NICE */
while ($myrow = $result->fetch_assoc()) {

// use your $myrow array as you would with any other fetch
printf("%s is in district %s\n", $city, $myrow['district']);

}
Christopher Smit 19-Mar-13 8:45am    
I now get it to display the messages like this:

Message 1(under message 2)

Message 2

OR

Message 2(under message 1)

Message 1

This does not make sense to me.. But like I said I'm still new to this..
Christopher Smit 19-Mar-13 8:53am    
I have it solved.. Instead of calling a function from the functions script I simply added the entire function in the page that should display the data. I then print the table of data in the while loop.. :) Thanks for your help..
max_nowak 19-Mar-13 10:53am    
Glad you could solve it. Though I wished I could have been of more help (as I said I'm not a pro in php either;)). Btw you can post your solution here below if you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900