PHP Table row

Hello people,

Sorry for my lack of knowledge in php. Im really a new to php and im practicing with some php code but for some reason i create 2 tables user and event and after a login enters to his page only get to displays just 1 row of the user event table, I wish if someone could help me or advise whats wrong with the code:

<?php session_start(); include('connect.php'); if(isset($_GET['email'])){ $email_name = $_GET['email']; }else{ echo "No user by that name"; exit(); } //find email $sql = "SELECT * FROM users WHERE email='".$email_name."'"; $query = $db->query($sql); //a user only has 1 row on the users table if($query->num_rows !=1){ echo "No user by that name"; exit(); }else{ $row = $query->fetch_object(); //$id is set to be = users id $id = $row->id; //selects event id and = users id $reminder = $db->query("SELECT * FROM event WHERE id='".$id."'"); //set if = or great than 0 display table if ($reminder->num_rows >=0){ $row = $reminder->fetch_object(); $title = $row->title; $note = $row->note; $date_time = $row->date_time; $alarm_type = $row->alarm_type; $repeat_event = $row->repeat_event; }else{ $title = "n/a"; $note = "n/a"; $date_time = "n/a"; $alarm_type = "n/a"; $repeat_event = "n/a"; } if(isset($_SESSION['logged_in'])&&$_SESSION['email']==$email_name){ $reminderOwner = true; } } ?>

Your Reminders

Hi <?php echo $email_name?>, welcome back. Log out


<?php if(isset($reminderOwner)):?> <?php endif; ?>
<table align='center'border='1' cellpadding='10'>
        <tr> <th>Title</th> <th>Note</th> <th>Date & Time</th> <th>Alarm Type</th> <th>Repeat</th> <th>Action</th></tr>
        <tr>
			<td><?php echo $title;?></td>
			<td><?php echo $note;?></td>
			<td><?php echo $date_time;?></td>
			<td><?php echo $alarm_type;?></td>
			<td><?php echo $repeat_event;?></td>
			<td><a href="edit.php">Edit</a>
			<a href="delete.php">Delete</a></td>
		</tr>
</table>

Add a new reminder

Thanks in advance

A guess…

A normal MySQLi query and checking of num_rows is something like this:

$result = $mysqli->query($sql);
$row_cnt = $result->num_rows;

So, yours is:

$query = $db->query($sql);
if($query->num_rows !=1){

Try changing yours to something like this:

$query = $db->query($sql);
//a user only has 1 row on the users table
$numrows = $query->num_rows;
if($numrows != 1){

Not sure, but, you might be able to do it with (($query->num_rows) != 1)…
Good luck, if this does fix it, let us know… We will check back in for your answer…

I did modify but no luck still displaying the first row

$sql = “SELECT * FROM users WHERE email=’”.$email_name."’";
$query = $db->query($sql);
//a user only has 1 row on the users table
$numrows = $query->num_rows;
if($numrows != 1){
echo “No user by that name”;
exit();

So, you are stating that every time you run it, it says “No user found by that name”???

Have you check the actual database to make sure the data is really in it?
You may be searching for a name that does not exist, in which case the program would be working correctly. Also, you are using MySQLi, which can get confusing sometimes. I did not see any odd code, though.

One way to test is to just display the results from your query and see what it is getting.
instead of testing row count, just display and die… Like this:
echo $query->num_rows;
And, see if it IS getting a row. If not, then the problem is in the query (SQL), itself…
You would have to look at the data inside the database and compare it to the query.
Just echo the query to see what it really is AFTER adding in the user name…
echo $sql;
This will let you see if the SQL is correct and if the name you are looking for is inside the database…

Try these echos and see what you find. Good luck…

let me put it this way, i have user table and event table with data, after a user from my table logs in is direct to his profile and works fine and display the event tables (only first row) for the user to manage he can view, edit, delete and add but on its profile only shows the first row of the event table, where it should display all the data or entries from this user. My page work but it only displays the first row of the user event table:
Your Reminders
Hi [email protected], welcome back. Log out

Title Note Date & Time Alarm Type Repeat Action
Dinner at apple bees 0000000 1 1 Edit Delete

Add a new reminder

This is what the output is but, this user has more rows and does not appear for some silly reason.

Oh, now I understand! Well, you have the code set up to only show one item, so the code is working as it was designed… This is the section that displays records for the selected user id:
if ($reminder->num_rows >=0){
$row = $reminder->fetch_object();
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;

}else{
$title = “n/a”;
$note = “n/a”;
$date_time = “n/a”;
$alarm_type = “n/a”;
$repeat_event = “n/a”;
}
***** It shows ONE record because it was designed that way. YOu would have to change the IF-esle so that inside the IF=true part it loops thru the entire results from the query, not just one entry.
Something like this might work…
[php]
if ($reminder->num_rows >=0){
while($row->fetch_object())
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}
}else{
$title = “n/a”;
$note = “n/a”;
$date_time = “n/a”;
$alarm_type = “n/a”;
$repeat_event = “n/a”;
}
[/php]
Since I do not use MySQLi as I have been told it is slow compared to MySQL, I am not sure of the exact syntax of this. You basically must do either a “while” or a “for” loop and loop thru the data in the recordset. You currently are only displaying one record. Sorry, I did not understand your request. I hope this has helped point you to the problem… Good luck!

Right…this is what i was think too, thank you so much for confirm it and help me out… i will change it

Good! And, sorry I was so blind earlier on… Let us know if you get it working correctly…

hello again,
i did tried a lot and still can solve this problem.
Add the While loop
if ($reminder->num_rows >=0){
while($row->fetch_object())
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}
}else{
$title = “n/a”;
$note = “n/a”;
$date_time = “n/a”;
$alarm_type = “n/a”;
$repeat_event = “n/a”;
}
and got this syntax error: Parse error: syntax error, unexpected T_ELSE in C:on line 35

Well, this is your problem:
}
}else{

The }else{ IS the } for that… So, you remove the first }

Also,
if ($reminder->num_rows >=0){
This line says if the num_rows is greater than or equal to 0…
I think you just meant to use
if ($reminder->num_rows >0){
that line says if the num_rows is greater than 0…

Hope all that helps…

Thank you for you reply now that a fix the code get this error
Fatal error: Call to undefined method stdClass::fetch_object() in C: LIne 27
if ($reminder->num_rows >0){
while($row->fetch_object()) //this is line 27
$row = $reminder->fetch_object();
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}else{
$title = “n/a”;
$note = “n/a”;
$date_time = “n/a”;
$alarm_type = “n/a”;
$repeat_event = “n/a”;
}

Well, I am very sorry again. Seems I am helping you in the evening when I am tired… LOL

Yes, you DO need the extra }, it was the closer for the WHILE… But, the error in the IF was probably the problem. Add the } back before the }else{ and tell us what the new error is and which line… Thanks…

No, thank you… for really helping out people with their issues, yeah i did put back again the brackets {} for the while loop but still get this error:
Fatal error: Call to undefined method stdClass::fetch_object() in C:\xampp\view.php on line 27
if ($reminder->num_rows>0){
while($row->fetch_object()){ //This is line 27
$row = $reminder->fetch_object();
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}
}

I think I got it… But, can you please repost the code from “//find email” to the “”…
And, then, I think I can get ya fixed up…

Here is the code:

//find email
$sql = “SELECT * FROM users WHERE email=’”.$email_name."’";
$query = $db->query($sql);
//a user only has 1 row on the users table
$numrows = $query->num_rows;
if($query->num_rows !=1){
echo “No user by that name”;
exit();

}else{
$row = $query->fetch_object();
//$id is set to be = users id
$id = $row->id;
//selects event id and = users id
$reminder = $db->query(“SELECT * FROM event WHERE id=’”.$id."’");
//set if = or great than 0 display table
if ($reminder->num_rows>0){
while($row->fetch_object()){
$row = $reminder->fetch_object();
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}
}else{
$title = “n/a”;
$note = “n/a”;
$date_time = “n/a”;
$alarm_type = “n/a”;
$repeat_event = “n/a”;
}
if(isset($_SESSION[‘logged_in’])&&$_SESSION[‘email’]==$email_name){
$reminderOwner = true;
}
}

?>

Here is the code:

//find email
$sql = “SELECT * FROM users WHERE email=’”.$email_name."’";
$query = $db->query($sql);
//a user only has 1 row on the users table
$numrows = $query->num_rows;
if($query->num_rows !=1){
echo “No user by that name”;
exit();

}else{
$row = $query->fetch_object();
//$id is set to be = users id
$id = $row->id;
//selects event id and = users id
$reminder = $db->query(“SELECT * FROM event WHERE id=’”.$id."’");
//set if = or great than 0 display table
if ($reminder->num_rows>0){
while($row->fetch_object()){
$row = $reminder->fetch_object();
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}
}else{
$title = “n/a”;
$note = “n/a”;
$date_time = “n/a”;
$alarm_type = “n/a”;
$repeat_event = “n/a”;
}
if(isset($_SESSION[‘logged_in’])&&$_SESSION[‘email’]==$email_name){
$reminderOwner = true;
}
}

?>

I feel so stupid… You have two query’s on INSIDE the other… (Well, basically!)
So, in your code you use one as $row and the next as $reminder, but, in your display code, you use $row, not $reminder!
I think what is happening is that the first query’s line:
$row = $query->fetch_object();
This line is pulling the $row(s) from the query as objects…

So, this line in the second query:
while($row->fetch_object()){
this line never actually creates the pulling of the rows from the query…
Not sure, but, I think this line should be:
while($row = $reminder->fetch_object()){

In this manner, it is creating the pulling of the rows from the query ($reminder)…
I think that is what you need… Good luck…

Thank you for replying, now the output is giving me the last entry for the user account:
Before the output was: (which is the first entry for user [email protected]
Your Reminders
Hi [email protected], welcome back. Log out

Title Note Date & Time Alarm Type Repeat Action
Dinner at apple bees 0000000 1 1 Edit Delete

Add a new reminder
After all the changes we made the output is: which is the last entry of the same user
Your Reminders
Hi [email protected], welcome back. Log out

Title Note Date & Time Alarm Type Repeat Action
Timing pleay 894751 3 3 Edit Delete

Add a new reminder

Still unable to show all the entries to the user. ?plop?

YEAH!!! finally got it to work plop lol
i just had to change from:
if ($reminder->num_rows>0){
while($row->fetch_object()){
$row = $reminder->fetch_object();
$title = $row->title;
$note = $row->note;
$date_time = $row->date_time;
$alarm_type = $row->alarm_type;
$repeat_event = $row->repeat_event;
}
}

to this:
if ($reminder->num_rows>0){
while($row = $reminder->fetch_object()){
echo “

”;
echo “” . $row->title . “”;
echo “” . $row->note . “”;
echo “” . $row->date_time . “”;
echo “” . $row->alarm_type . “”;
echo “” . $row->repeat_event . “”;
echo “Edit”;
echo “Delete”;
echo “”;
}
}
THANK YOU ERNIEALEX… your replies and help did help this code to run…

You are very welcome, sorry for the delay in seeing this one… Always a nice feeling to fix it… LOL…

Sponsor our Newsletter | Privacy Policy | Terms of Service