0

I have here

  <table align="center">
  <tr>
     <td>ID No</td><td>:</td>
     <td><input type="text" name="idno" size="10px" maxlength="15px" ></td>
     <td><input type="submit" value="Search ID No." name="search"></td>
    </tr>
  </table>

  <?php
    include('connect.php');
    $sql="select * from stud where ID_No='$_POST[idno]';";
    $result=mysql_query($sql);
        $s=mysql_fetch_array($result);

     $addr = $s[Home_addr];
     echo"$addr";
   ?>

How to get the whole address?

This script only returns the first word from the database.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107

3 Answers3

1

You need to loop through the data:

<?php
include('connect.php');
$idno = (integer) $_POST['idno'];
$sql="select * from stud where ID_No='$idno';";
$result=mysql_query($sql);
while ($s=mysql_fetch_array($result)) {
    $addr = $s['Home_addr'];
    echo "$addr";
}
?>

Also, please don't use mysql_* functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. Also see Why shouldn't I use mysql functions in PHP?

Community
  • 1
  • 1
Daedalus
  • 7,586
  • 5
  • 36
  • 61
  • actually i was also using that code.. instead of displaying "San Pedro" it will display only "San".. the "Pedro" won't display.. how would I do this.. can you help me?? please :).. actually i'm a beginner.. i'm very thankful that you respond to my question.. – user1394959 May 17 '12 at 02:02
1

Iterate for mysql_fetch_array

<?php
    include('connect.php');
    $sql="select * from stud where ID_No='$_POST[idno]';";
    $result=mysql_query($sql);
    while($s=mysql_fetch_array($result))
    {

     $addr = $s[Home_addr];
     echo"$addr";
    }
   ?>
bitoshi.n
  • 2,278
  • 1
  • 16
  • 16
  • Not necessary for single-row results. id_no suggests it'd be a PK field. – Marc B May 15 '12 at 03:47
  • hello.. thank you for answering the question.. i would like to add how to display "San Pedro" from the database.. actually I was also using this code but this would only display "San".. the "pedro" would not display.. can you help me with this?? thank you for the respond.. – user1394959 May 17 '12 at 02:09
1

Sorry, off topic. But please do not put user input from the $_POST array directly into a query. You need to pass it through mysql_real_escape_string() first or else you will be vulnerable to SQL injection attacks.

Also, the keys of your associative arrays are strings so please quote them:

$s['Home_addr']

not

$s[Home_addr]

in the latter, PHP will first attempt to treat Home_addr as a constant but when it's not found it will fall back on treating it like a string so it does work but it's not reliable, probably slower, and simply wrong.

Okonomiyaki3000
  • 3,628
  • 23
  • 23