Jump to content

another help please.


asherinho

Recommended Posts

Iam a beginer to php thats why i frequently ask questions.

I have a table in the database havin 3 fields:- firstname,lastname,age.i want to fetch the age basing on the firstname selected from the form's select menu which has all the names present in the firstname field.the form is saved as php since i had to first fetch the names to be available in the select menu.here are the codes and the warning i got

 

<?php

$db=mysql_connect("localhost","root");

mysql_select_db("my_db",$db);

$query="SELECT age FROM person WHERE firstname=$firstname";

$result=mysql_query($query);

$record=mysql_fetch_assoc($result);

list($fieldname,$fieldvalue)=each($record);

echo $fieldvalue;

?>

 

Warning: Variable passed to each() is not an array or object in C:\Program Files\xampp\webalizer\age.php on line 15

 

Link to comment
https://forums.phpfreaks.com/topic/62825-another-help-please/
Share on other sites

Assuming your form uses the POST method....

 

<?php

  $db = mysql_connect("localhost","root");
  mysql_select_db("my_db",$db);
  $firstname = mysql_real_escape_string($_POST['firstname']);
  $query="SELECT age FROM person WHERE firstname='$firstname'";
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) {
      $record = mysql_fetch_assoc($result);
      list($fieldname,$fieldvalue) = each($record);
      echo $fieldvalue;
    } else {
      echo "No results found for $firstname<br />";
    }
  } else {
    echo "Query failed<br />" . mysql_error() . "<br />$query";
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/62825-another-help-please/#findComment-312783
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.