Jump to content

Recommended Posts

Problem: Select 10 Newest Members and display

it as a link.

 

I just need for somebody to talk me through it,

and tell me what I am missing.

<?php
//connect to database
//run query

for($i = 0; $i < $num_result; $i++){
$row = mysql_fetch_array($result);
  echo"<a href ="  ">$row</a> ";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/47440-solved-problem-select-10-newest-members/
Share on other sites

The result returned by mysql_fetch_array is an array. 

var_dump($row);

That will spit out all the contents of the array (in an ugly manner).  Useful for debugging.

 

You are just trying to print (echo) an array.  What you want to do is print the values in the array.

 

echo"<a href ="  ">$row[0]</a> ";

 

That will print the value of the first column returned ($row[1] contains the second column, and so on).  You can also specify that mysql_fetch_array returns an associative array.  This allows you to reference the array values by the column name they come from. 

Example:

 

Suppose you have this table layout, the table is called USERS.

USERS

  FIRST_NAME

  LAST_NAME

  TELEPHONE_NUMBER

 

and you want to display the first names.  The sql query is "SELECT FIRST_NAME FROM USERS";  This will select all the first names in the database.  Then, when you want to display this data:

 

for($i = 0; $i < $num_result; $i++){
$row = mysql_fetch_array($result, MYSQL_ASSOC);
  echo"<a href ="  ">$row['FIRST_NAME']</a> ";
}

 

Note the addition of MYSQL_ASSOC.  This allows you to reference $row['COLUMN_NAME']; rather than $row[0].

 

Hope that helps.

Mike

Thanks, I understand. I got the link, but I still am trying to

display the 10 Newest User.

 

Problem: What if there are 15 users registered into the

database. 10 user will register today and the addiction

5 will register another day. Five out 10 from the first day

and the addiction five = 10 Newest Members.

 

I just need for somebody to talk me through it,

and tell me what I am missing.

<?php
$num_results = mysql_num_rows($result); 
for ($i = 0; $i < $num_results; $i++) {
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
	    echo"<a href =\"user_profile.php\">$row[user_name]</a> ";
}// End of For Loop
?>

 

 

can't you sort registered date by desc with a limit of 10!!

 

Something like this?

<?php
$sql = 'SELECT register_date FROM members_login ORDER BY 
register_date DESC LIMIT 10'; 

$query = mysql_query($sql); 

$data = mysql_fetch_assoc($query); 

echo $data['columnname']; 
?>

Take the limit off and list users and dates and see if order looks OK

 

<?php
$sql = 'SELECT user_name, register_date FROM members_login ORDER BY 
        register_date DESC';
$res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
while (list($u, $d) = mysql_fetch_row($res)) {
    echo "$u : $d <br/>" ;
}
?>

 

 

Take the limit off and list users and dates and see if order looks OK

 

This is the order I was given.

9 : User9

8 : User8

7 : User7

6 : User6

5 : User5

4 : User4

3 : User3

2 : User2

15 : User15

14 : User14

13 : User13

12 : User12

11 : User11

10 : User10

1 : User1

What was he output from my code, showing the dates?

 

Sorry about that Barand. Here it is:

 

User1 : 2007-04-17 00:00:00

User2 : 2007-04-17 00:00:00

User3 : 2007-04-17 00:00:00

User4 : 2007-04-17 00:00:00

User5 : 2007-04-17 00:00:00

User6 : 2007-04-17 00:00:00

User7 : 2007-04-17 00:00:00

User8 : 2007-04-17 00:00:00

User9 : 2007-04-17 00:00:00

User10 : 2007-04-17 00:00:00

User11 : 2007-04-17 00:00:00

User12 : 2007-04-17 00:00:00

User13 : 2007-04-17 00:00:00

User14 : 2007-04-17 00:00:00

User15 : 2007-04-17 00:00:00

As they all have the same date-time value then any combination of 10 records out of the 15 would be correct for the 10 latest.

 

What if I go in tomorrow and enter 15 more user. Then

that will be a total of User30, and I donnot like the way the time is

being displayed. What would be the best way to enter the time into

the database?

 

 

 

As they all have the same date-time value then any combination of 10 records out of the 15 would be correct for the 10 latest.

 

What if I go in tomorrow and enter 15 more user. Then

that will be a total of User30, and I donnot like the way the time is

being displayed. What would be the best way to enter the time into

the database?

 

 

 

 

If the register_date column isn't DATETIME then change it to that type. When you add a record put the MySQL value NOW() into that column.

 

That will store the register_date to the nearest second into that column.

The problem with TIMESTAMP columns is that they will update automatically so if, a few months down the line, you change the value of, say, the name field because of a misspelling, the timestamp field becomes the date of the change and not the original date registered.

As they all have the same date-time value then any combination of 10 records out of the 15 would be correct for the 10 latest.

 

What if I go in tomorrow and enter 15 more user. Then

that will be a total of User30, and I donnot like the way the time is

being displayed. What would be the best way to enter the time into

the database?

 

 

 

 

If the register_date column isn't DATETIME then change it to that type. When you add a record put the MySQL value NOW() into that column.

 

That will store the register_date to the nearest second into that column.

 

I had it at datetime, but I change it to timestamp. Should I change it

back?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.