Jump to content

Recommended Posts

Hi...Right this may be hard to explain becuase i am not any good with mysql...but i have a website and i have a mysql database of which people sign up to my website and then i can view all the users...

 

Now i want to add something on my website which says "newest member: *their username*"

So it takes the username from the mysql table of the newest entry...is this possilbe and how?!!?!?!

 

Please explain in detail.....thank you so much in advance

Show the structure of the table? Do you have an primary key/auto_increment column? If so, couldn't you just do something like

 

SELECT username FROM members
ORDER BY id
LIMIT 1;

 

Also, SELECT LAST_INSERT_ID(); works but I don't think it will work for your purposes, I'm afraid.

 

Alternatively, add a column to store the date/time of join, then order on that instead of id (same query as above).

 

Hi Shirik thanks for your comment.

Right as for this bit:

 

SELECT username FROM members
ORDER BY id
LIMIT 1;

 

I would have no idea of how to insert that on my PHP webpage...so would you be able to write it so i could just copy it in...(obviosuly changing the parts i need to)

 

And yes i do have a auto_increment column......i think...anyway..ive added a screenshop of my database in phpmyadmin...Please get back to me :) thanks so much again

 

[attachment deleted by admin]

Something like this?

 

<?php
$latestUser = "SELECT id, uid FROM table_name ORDER BY id DESC LIMIT 1";
$result = mysql_query($latestUser ) or die(mysql_error());
while(list($id, $uid) = mysql_fetch_array($result, MYSQL_NUM))
{	
?>
<p>Newest Member: <?php echo $uid; ?></p>
<?php
}
?>

Hi genics thanks for the reply...im gettin this message when i go on my webpage...ive typed the table name in...

 

 

 

 

Warning: mysql_query() [function.mysql-query]: Access denied for user: 'nobody@localhost' (Using password: NO) in /data01/owlsfan/public_html/try.php on line 3

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /data01/owlsfan/public_html/try.php on line 3

Access denied for user: 'nobody@localhost' (Using password: NO)

Another thing...when you say Table_name...what do you mean...the actaul table name? because that is just "users"...but then how does the code know to pick it from which database becuase the database is called "owlsfan_simp" which consists of one table which is "users"

 

Thanks

you need to connect to the database

 

      /* Make connection to database */
      $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die('Couldnt Connect to the database');
      mysql_select_db(DB_NAME, $connection) or die(mysql_error());

 

replace:

DB_SERVER, DB_USER, DB_PASS, DB_NAME

 

with the corresponding information

Yep, as waldir pointed out - you need to connect to the database in order for my script to work.

 

Your strcuture will be:

 

database

-> table

---> user

 

So you need to first connect to your database with the following script:

 

$host = "server";
$user = "username";
$pass = "password";
$database = "database_name";

$connect = mysql_connect($host, $user, $pass) or die('Connection Failed');
mysql_select_db($database) or die(mysql_error());

 

Then you need to select the table and relevant fields from the database:

 

<?php
$latestUser = "SELECT id, uid FROM table_name ORDER BY id DESC LIMIT 1";

 

And then finally you need to display those selected fields:

 

$result = mysql_query($latestUser ) or die(mysql_error());
while(list($id, $uid) = mysql_fetch_array($result, MYSQL_NUM))
{	
?>
<p>Newest Member: <?php echo $uid; ?></p>
<?php
}
?>

 

So to put it all together:

 

<?php
$host = "server";
$user = "username";
$pass = "password";
$database = "database_name";

$connect = mysql_connect($host, $user, $pass) or die('Connection Failed');
mysql_select_db($database) or die(mysql_error());

$latestUser = "SELECT id, uid FROM table_name ORDER BY id DESC LIMIT 1";
$result = mysql_query($latestUser ) or die(mysql_error());
while(list($id, $uid) = mysql_fetch_array($result, MYSQL_NUM))
{	
?>
<p>Newest Member: <?php echo $uid; ?></p>
<?php
}
?>

 

So for you to get my script to work, you need to edit all the bits:

 

- server

- username

- password

- database_name

- table_name

Another thing...when you say Table_name...what do you mean...the actaul table name? because that is just "users"...but then how does the code know to pick it from which database becuase the database is called "owlsfan_simp" which consists of one table which is "users"

 

Thanks

 

The table name is the name of the table you posted a screenshot of. If that's called users then just replace table_name with users

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.