Function works with only one database table

I have this function that I am trying to use it to display data from my database.
For some reason, if I change to SELECT * FROM members the function returns empty rows in the database. If I add another row in the members database and run the function again, the function returns 1 extra row. I find it very weird as it works for one table and doesn’t work for another…
Any suggestions??

Thanks

public function getData()
{

	$stmt = $this->link->prepare(" SELECT * FROM employers ");
	$stmt->bind_param();
	if ($stmt->execute())
	{
		$result = $stmt->get_result();
		while($row = $result->fetch_array(MYSQLI_ASSOC))
			{ 
			$row = array_map('stripslashes', $row);
			$dataArray[] = $row;
			}
	} 
	return $dataArray;		
}

I am calling the function to display like this:

$array = $administrator_query->getData();

echo "<table border= 1px><br>"; // start a table tag in the HTML
echo "<tr>
		<th>ID</th>
		<th>Password</th>
		<th>First Name</th>  
		<th>Last Name</th>
	   </tr>";

foreach ($array as $row) 
{
echo 
"<tr>
	<td>" . $row['user_id'] . "</td>
	<td>" . $row['user_password'] . "</td>
	<td>" . $row['user_first_name'] .  "</td>
	<td>" . $row['user_last_name'] .  "</td>
</tr>";  //$row['index'] the index here is a field name
}	
echo "</table>";
print_r($array);

On the line after the call. See what your array looks like.

Same thing. For employers it wont do anything, just print in the table as before, and for members It will just print the empty rows.

Both the mysqli_* extension and PDO have functions which will return all of a result set, you might want to look into them if you always are going to use every row of a result set

Add some echo statements and see where it’s failing. Does it run the query correctly but not populate the array, or is there a problem with the query? As @StarLion asked, what does print_r() give when you call it after creating the array? Are you calling employers then members, so could it be that you’re not clearing something out? I’m surprised it returns “empty rows”, or do you mean it returns nothing? Do you need to define the $dataArray as an array before the loop - I’ve seen that done but not sure if it’s mandatory.

But to me, add loads of echo() so you can trace the variables through as they are created and updated to narrow down where it stops doing what you expect it to.

I’m more interested in the structure of the array object output by print_r. The command, if actually done, should have spat out an object structure into your page. Please show me at least one complete row from this output.

@StarLion
@droopsnoot


The first screenshot is when I change the query to SELECT * FROM members

This screenshot is when changing back to “employers”

Now I am adding a new row in the database

Printing again:

There are 4 rows instead of three like before.
The database is there, it is working it is adding rows, and when printing it knows how many rows to print its just not putting the data in the rows as it does for “employers”.
I think there is nothing in the array because when I try a echo $dataArray in the function it just prints out Array

You can’t echo $dataArray, you have to print_r or var_dump it because it’s an array. You can only echo elements of the array.

When you change to the different table, you are correctly changing the index names in your display loop to reflect the different column names, aren’t you? member_firstname instead of user_firstname, and so on.

@droopsnoot
Ok. print_r($dataArray) prints this. But why I cant passed back to where I am trying to print it? Also I have change the index names in my loop.

What do you get if you print_r($array) in your calling code? You’ve established that the query is working correctly, building the array in the function, so we just need to step through it to see where the problem starts to happen. If it’s the same when you print_r($array), then show the code you use to display the loop.

@droopsnoot

If I do print_r($array) where I am calling the function, it won’t print nothing. I think it is not passing the array back.

But if it knows enough to print more than one row, it must be returning something just to mark that. Does var_dump($array) after the call give any different information?

I doesn’t give anything. I will keep trying…

So if you look at your output, your fields are named member_id etc…

but your output code is outputing

<td>" . $row['user_id'] . "</td>

user_id != member_id…

I did try changing that as well. no result. there is something with the array or with the table. I will keep looking into it. Thanks anyway for your suggestions.

@StarLion
@droopsnoot

Thanks for the suggestions. In the end I have dropped my table “members” and created a new one. I made sure I pass in the output to the right name fields like member_id, etc.
In the end it worked, so I don’t know what was wrong. Maybe when creating the members database something went wrong. Anyway, all solved. Thanks for the help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.