Pergunta

I am not really good at coding, and don't know how I should call this.

I have made something to register, it saves the users to the mysql database. Now I want to make a php script displaying the last added user.

I'm currently using this script, but it keeps displaying 0.

<?php
$link = mysql_connect('localhost', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('hedery');

mysql_query("INSERT INTO mytable (users) values ('username')");
printf("Last added user: %d\n", mysql_insert_id());
?>

The database name is"hedery". The table is "users". And inside the users there is " id username password email activated banned firma"

Now I want to display the last added "username". Would be nice if soemone could tell me what I'm doing wrong here.

Thanks alot to the stackoverflow community btw. This site got me so far already..

Foi útil?

Solução

<?php
$link = mysql_connect('localhost', 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('hedery');

mysql_query("INSERT INTO mytable (users) values ('username')");
$result = mysql_query("SELECT * FROM `mytable` ORDER BY `id` DESC LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['username'];
?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top