php - Sql code for displaying one account from two relational tables -


i want display 1 account 2 relational table. example if inquire account number , enter it, details of account shown not every members. in output whole members , details inside database display. how can manage display 1 account 2 relational tables? know there wrong in sql.

<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('could not connect: ' . mysql_error()); } $sql = 'select member.*, account.* member, account member.mem_id = account.mem_id';  mysql_select_db('databasename'); $retval = mysql_query( $sql, $conn );  if(! $retval ) { die('could not data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, mysql_assoc)) { echo "account number:{$row['account_number']}  <br> ".  "first name: {$row['fname']} <br> ".  "last name: {$row['lname']} <br> ".  "address: {$row['address']} <br> ".  "contact: {$row['contact']} <br> ".  "share capital: {$row['share_capital']} <br> ".  "regular savings: {$row['regular_savings']} <br> ".  "power savings: {$row['power_savings']} <br> ".  "--------------------------------<br>"; }  echo "fetched data successfully\n"; mysql_close($conn); ?> 

now output.

account number:  first name: last name: address:  contact:  share capital:  regular savings: power savings: 

where haven't chosen anyone's answer correct, suspect may have data issue @ hand. assuming records 1 1, sure:

notice added $find_id @ top param

<?php       $dbhost     = 'localhost';     $dbuser     = 'root';     $dbpass     = 'password';     $find_id    = 12;      try     {          $conn = mysql_connect($dbhost, $dbuser, $dbpass);            if( !$conn )             throw new exception( "couldn't establish connection" );          if( !mysql_select_db('databasename', $conn )  )             throw new exception( "database couldn't selected" );          $sql = 'select m.*, a.* member m left join account on m.mem_id = a.mem_id                 m.mem_id = $find_id';          if( !($retval = mysql_query( $sql, $conn )) )             throw new exception( "no matching result, or query borked; " . mysql_error() );          if( !($row = mysql_fetch_assoc( $retval )) )             throw new exception( "no rows found, move along." );          print_r( $row );     }     catch( exception $x )     {         echo "an exception occurred: " . $x->getmessage();     } 

so that's ugly of all, few things note:

probably don't want using mysql_* functions; use pdo instead (which isn't on way out door) - and, use prepared params mitigate sql injection attacks. super nerdy tidbit can add is logic separate authentication table routines user-table routines since user tables, end being used "outside" of this. query auth establish id, then, use user table in separate thread. way, auth queries aren't tying both tables.

good luck! hope helps.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -