Hey everyone!

I have a members.php page/file in which i want logged in users to see the other users registered in the database. I'm not exactly quite sure how to do this and I'm struggling quite a while now with this so your help will be VERY much appreciated.

I will include connect.php, members.php and my database.sql

Here is my connect.php file

<?php 
session_start();
//connect.php

$server = 'localhost';
$username = 'root';
$password = '';
$database = 'mydatabase';

if(!mysql_connect('localhost', 'root', ''))
{
 	exit('Error: could not establish database connection');
}
if(!mysql_select_db($database))
{
 	exit('Error: could not select the database');
}
?>

Here is my members.php file

<!DOCTYPE HTML>
<head>
 	<title> ShareLink </title>
	<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
	<div id="wrapper">
		<?php
		
		//create_cat.php
		include 'connect.php';
		include 'header.php';
		
		if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1 )
			{
				//the user is not an admin
				echo '<br/>';
				echo 'Sorry! You have to be <a href="/signin.php"><b>logged in</b></a> to view all the <a href="signup.php" title="Become a registered user!"><b>registered</b></a> members.';
				echo '<br/><br/>';
			}
			else
			{
				echo '<h2>Registered users:</h2>';
				
				$sql = "SELECT user_name FROM users";
				
				//NOW I WANT TO DISPLAY ALL THE REGISTERED USERS
							
				//MUST I ECHO SOMETHING HERE?
												
				/////////////////////////////////////////////////////////
				///////////////  WHAT CODE MUST COME HERE? //////////////
				/////////////// AND IS IT SQL OR PHP CODE? //////////////
				/////////////////////////////////////////////////////////
			}
			include 'footer.php';
		?>
	</div>
</body>
</html>

Here is my database.sql file

CREATE TABLE users (  
user_id     INT(8) NOT NULL AUTO_INCREMENT,  
user_name   VARCHAR(30) NOT NULL,  
user_pass   VARCHAR(255) NOT NULL,  
user_email  VARCHAR(255) NOT NULL,  
user_date   DATETIME NOT NULL,  
user_level  INT(8) NOT NULL,  
UNIQUE INDEX user_name_unique (user_name),  
PRIMARY KEY (user_id)  
);

CREATE TABLE categories (  
cat_id          INT(8) NOT NULL AUTO_INCREMENT,  
cat_name        VARCHAR(255) NOT NULL,  
cat_description     VARCHAR(255) NOT NULL,  
UNIQUE INDEX cat_name_unique (cat_name),  
PRIMARY KEY (cat_id)  
);

CREATE TABLE topics (  
topic_id        INT(8) NOT NULL AUTO_INCREMENT,  
topic_subject       VARCHAR(255) NOT NULL,  
topic_date      DATETIME NOT NULL,  
topic_cat       INT(8) NOT NULL,  
topic_by        INT(8) NOT NULL,  
PRIMARY KEY (topic_id)  
); 

CREATE TABLE posts (  
post_id         INT(8) NOT NULL AUTO_INCREMENT,  
post_content        TEXT NOT NULL,  
post_date       DATETIME NOT NULL,  
post_topic      INT(8) NOT NULL,  
post_by     INT(8) NOT NULL,  
PRIMARY KEY (post_id)  
);

Dani AI

Generated

A few focused fixes and a safe example to get the members list working.

The main problems seen in this thread are: the session check logic (the existing isset comparisons are incorrect), use of the old mysql_* API, and passing the wrong variable into the fetch call (which causes the "resource, boolean given" error). appears to have a typo for the query call, and correctly pointed out that the fetch function must receive the query result resource. Always check the query return value before iterating, and escape output to avoid XSS.

A practical, current approach is to use mysqli or PDO, verify the session variables properly, check the query for errors, and then loop over the result set. Example (drop this into members.php after session_start and your secure connection code):

<?php
$mysqli = new mysqli('localhost','root','','mydatabase');
if ($mysqli->connect_errno) {
  die('DB connection error: ' . $mysqli->connect_error);
}

if (empty($_SESSION['signed_in']) || ($_SESSION['user_level'] ?? 0) != 1) {
  echo 'You must be logged in to view members.';
  exit;
}

if ($result = $mysqli->query('SELECT user_id, user_name FROM users')) {
  echo "<ul>\n";
  while ($row = $result->fetch_assoc()) {
    echo '<li>' . htmlspecialchars($row['user_name'], ENT_QUOTES) . "</li>\n";
  }
  echo "</ul>\n";
  $result->free();
} else {
  error_log('Query failed: ' . $mysqli->error);
  echo 'Unable to load users.';
}
$mysqli->close();
?>

Troubleshooting tips: if you see the fetch-row/bool error, inspect the query return and log the DB error; do not pass the SQL string to the fetch function. For new projects migrate away from mysql_* (use mysqli or PDO), use prepared statements where user input is involved, and store passwords with password_hash/password_verify.

Recommended Answers

All 4 Replies

along those lines

$query = mysql( $sql );
while ($array = mysql_fetch_array($query)) 
  echo $array['user_name'] . '<br/>';
I am struggling to fetch back the data stored in the database to be view in the system.

This is my code to view all databse after i logged by using the Medicine ID.

<?php

//session_start();
//session_destroy;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<table>
<tr>
<body background="bg2.jpg">
<td><br>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="grey" >
<tr>
<td><br><div align="center"><strong>MEDICINE RECORDS</strong></div></td>
</tr>
<tr>
<td><div align="center"><form action="updatestock2.php"  method="post">
<br><br><td><center><strong></strong></div></td></center> </left>
</tr>

<br><br><br><br>
<form action="updatestock2.php" method="POST">
<br><br>
<td>
<center>Medicine ID:</td><td><input type="text" name="medid"></center> </td>
</tr>


<tr>
<td> <br><br>
<center><input type="reset" value="Reset">
<input type="submit" name="send" value="Send">

</center></td></tr>
<br/><br/> <br/><br/>

</table>
</table>

<table border="3">
<center>
<td align="centre">
<tr> <br><br>
<td>Medicine Name</td>
<td>Total</td>


</tr>
</body>
</html>
<html>
<body>



<?php

  include 'base.php';
  if (isset ($_POST['send']))
{

$medid=$_POST['medid'];

$sql="select * FROM medicine where medid='$medid'";
$result=mysql_query ($sql);


 while($data = mysql_fetch_row($sql))



{

 echo("<tr><td>$data[0]</td><td>$data[1]</td></tr>$data[2]</table>");
}

?> 
<?php
}
?>
</center>
</body>
</html>







I KEEP GETTING THIS ERROR> 
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\fyp3\updatestock2.php on line 70

""

As for the second error, try replacing;

while($data = mysql_fetch_row($sql))

with

while($data = mysql_fetch_row($result))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.