Jump to content

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

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.