Click to See Complete Forum and Search --> : Query and echo - need help


toplisek
09-22-2005, 09:46 AM
I have form and when this form is submitted it should show
answer with file Thank you.php and within this file there is text
e-mail was sent to email:...

As I have form and text Thank you in other table, there
should be code to show this E-mail.
I put code as following:
$query = "SELECT emailreceivable FROM emailfriend WHERE ...;
$result = mysql_query($query) or die ("SQL error: $query -- " . mysql_error());
echo "$emailreceivable";

How to define WHERE script that it will choose the last one inserted in table on server or there are other possibilities?

zingmatter
09-22-2005, 10:38 AM
For the most recently added row in a table try:
SELECT emailreceivable FROM emailfriend ORDER BY tableIndex DESC LIMIT 1

where tableIndex is your primary key field with the auto-increment.

toplisek
09-23-2005, 04:25 AM
I have put and it does not work:
$query = "SELECT emailreceivable FROM emailfriend ORDER BY tableindex DESC LIMIT 1";
$result = mysql_query($query) or die ("SQL error: $query -- " . mysql_error());
echo "$emailreceivable";
Do you know what is wrong?
It shows me error:
Parse error: parse error, unexpected $ in ...on line 318

zingmatter
09-23-2005, 05:36 AM
Is 'tableindex' the actual name of the index field in the emailfriend table?

toplisek
09-23-2005, 06:40 AM
This is the name of the field which is auto increment each time when form is filled.
1. I have now last number of recordes 95. Is this problem?
2. Should I put echo "$emailrec"; or
echo "$result";
If I put in code echo "$result"; there will show Resource id #3

How does server know that it must show last input and how to define variable in this case?

zingmatter
09-23-2005, 07:08 AM
You need to do this:

$query = "SELECT emailreceivable FROM emailfriend ORDER BY tableindex DESC LIMIT 1";
$result = mysql_query($query) or die ("SQL error: $query -- " . mysql_error());
if ($row = mysql_fetch_array($result)) {
echo $row["emailreceivable"];
}

toplisek
09-23-2005, 07:49 AM
thanks, you are very helpful.
it works.
Just this, if I would like to echo two fields like firstname and last name. What to do in this case? e.g.: First name and Last name are two separated fields
$query = "SELECT ... FROM emailfriend ORDER BY index DESC LIMIT 1";
$result = mysql_query($query) or die ("SQL error: $query -- " . mysql_error());
if ($row = mysql_fetch_array($result)) {
echo $row["firstname"]; echo echo $row["lastname"];
}

zingmatter
09-23-2005, 08:17 AM
All the fields can be rerieved as you need them using (in your example) $row["...whatever field name..."];
$row is an associative array, so you can also use $row[0], $row[1] etc... so long as you know what order the fields appear in the table.

If the recordset returns more than one row, rather than use if ($row = ...etc, you can use a while loop:

while ($row = mysql_fetch_array($result)) {

echo "firstname: ".$row["firstname"].", ";
echo "lastname: ".$row["lastname"]."<br>";

}

Hope this helps

toplisek
09-23-2005, 10:09 AM
Thanks, super, it works.