1

So I have form:

    <form method="post">
    Name: <input type="text" name="name" value<? if(isset($_GET['edit'])) {echo '="'. person($_GET['edit']) .'"';} ?>/><br>
    Surname: <input type="text" name="surname" value<? if(isset($_GET['edit'])) {echo '="'. person($_GET['edit']) .'"';} ?>/><br>       
    Age: <input type="text" name="age" value<? if(isset($_GET['edit'])) {echo '="'. person($_GET['edit']) .'"';} ?>/><br>
    <input type="submit" value="Submit" name="submit"/> 
</form>

and

    function person($id) {
    $query = 'SELECT name, sname, age FROM persons WHERE ID='.$id.';';
    $result = mysql_query($query);
    return $row = mysql_fetch_row($result);
}

So Function returns an array, How can I get from this function to display me only: 'name' / 'sname' / 'age' in each of input value

1
  • iterate over the array using foreach and check key if matches print value else don't, you can use in_array here. Are you stucked somewhere? Commented Aug 30, 2012 at 8:36

4 Answers 4

4

At first use mysql_fetch_assoc for use letter keys.

Then you can do so in latest versions of PHP:

person($id)['name']

Or define separate array in older versions of PHP:

$personData = person($id);
$name = $personData['name'];
1
  • 1
    me too.+1 for your answer.how can i remain silent while this type of beautiful answer Commented Aug 30, 2012 at 9:24
0

It's just like a normal array, so you can use [id]. If you use mysql_fetch_row it must be numbers from 0 to n-1 where n is number of items in array in order that you did in query. If you have mysql_fetch_assoc you can use $test['name'], $test['sname'], $test['age'].

$test = person(5);

echo $test[0].$test[1].$test[2]; //will show name|sname|age (withouth '|').
0
$per=person('12');

to get name name, sname, age

$per[0] for name, $per[1] for sname and $per[2] for age;
0
$data = person($id);
$name = $data['name'];

But you should escape ur Parameters before using them in your sql statement! At least do something like:

<?php $_GET['edit'] = mysql_real_escape_string($_GET['edit']); ?>
<form method="post">
Name: <input type="text" name="name" value<? if(isset($_GET['edit'])) {echo '="'.person($_GET['edit']) .'"';} ?>/><br>
 ......

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.