xymcrush17 Posted April 28, 2013 Share Posted April 28, 2013 (edited) Hi everyone Here my PHP code .. its looking so bad,but work :s i have 2 table with the both row is same //PHP retrive data <? $id = isset($_POST['id']) ? (int)$_POST['id'] : 0; if($id > 0) { //Query the DB $db1 = mysql_query("SELECT * FROM table1 WHERE uid = " . $id); $db2 = mysql_query("SELECT * FROM table2 WHERE uid = " . $id); if($db1 === false && $db2 === false) { die("Database Error"); } echo " <center><b>Get UID '$id' </b></center><br>"; while ($row = mysql_fetch_assoc($db1)){ echo "UID :{$row['uid']} <br> ". "Username : {$row['username']} <br> ". "Phone : {$row['phone']} <br> ". "Email : {$row['acct_email']} <br> ". "--------------------------------<br>"; } while ($row = mysql_fetch_assoc($db2)){ echo "UID :{$row['uid']} <br> ". "Username : {$row['username']} <br> ". "Phone : {$row['phone']} <br> ". "Email : {$row['acct_email']} <br>" . "--------------------------------<br>"; } } ?> //php search <? $searchid = trim($_POST['name']); //check whether the name parsed is empty if($searchid == "") { echo "<center><h2>Please input Serial ID or Searching by Name</h2></center>"; exit(); } $query = "SELECT * FROM table WHERE username LIKE '%$searchid%'"; $query2 = "SELECT * FROM table WHERE username LIKE '%$searchid%'"; $results = mysqli_query($link, $query); $results2 = mysqli_query($link, $query2); if(mysqli_num_rows($results) >= 1) { echo " <center><b>Keyword '$searchid' is found </b></center><br><br>" ; $output = ""; while($row = mysqli_fetch_array($results)) { $output .= "User ID: " . $row['uid'] . "<br />"; $output .= "Username :" . $row['username'] . "<br />"; $output .= "phone: " . $row['phone'] . "<br />"; $output .= "Email: " . $row['acct_email'] . "<br /><br />"; $output .= "--------------------------------<br>"; } } if(mysqli_num_rows($results2) >= 1) { $output2 = ""; while($row = mysqli_fetch_array($results2)) { $output2 .= "User ID: " . $row['uid'] . "<br />"; $output2 .= "Username :" . $row['username'] . "<br />"; $output2 .= "Phone: " . $row['phone']. "<br />"; $output2 .= "Email: " . $row['acct_email'] . "<br />"; $output2 .= "--------------------------------<br>"; } echo "$output $output2"; } can anyone make this code more structure? ? Edited April 28, 2013 by xymcrush17 Quote Link to comment Share on other sites More sharing options...
jcbones Posted April 28, 2013 Share Posted April 28, 2013 (edited) Not really a lot of cleaning up to do: <?php //PHP retrive data $id = isset($_POST['id']) ? (int)$_POST['id'] : 0; if($id > 0) { //Query the DB $db1 = mysql_query("SELECT * FROM table1 WHERE uid = " . $id); $db2 = mysql_query("SELECT * FROM table2 WHERE uid = " . $id); if($db1 === false && $db2 === false) { die("Database Error"); } echo " <center><b>Get UID '$id' </b></center><br>"; while ($row = mysql_fetch_assoc($db1)){ echo "UID :{$row['uid']} <br> ". "Username : {$row['username']} <br> ". "Phone : {$row['phone']} <br> ". "Email : {$row['acct_email']} <br> ". "--------------------------------<br>"; } while ($row = mysql_fetch_assoc($db2)){ echo "UID :{$row['uid']} <br> ". "Username : {$row['username']} <br> ". "Phone : {$row['phone']} <br> ". "Email : {$row['acct_email']} <br>" . "--------------------------------<br>"; } } //php search $searchid = trim($_POST['name']); //check whether the name parsed is empty if($searchid == "") { echo "<center><h2>Please input Serial ID or Searching by Name</h2></center>"; exit(); } $query = "SELECT * FROM table WHERE username LIKE '%$searchid%'"; $query2 = "SELECT * FROM table WHERE username LIKE '%$searchid%'"; $results = mysqli_query($link, $query); $results2 = mysqli_query($link, $query2); if(mysqli_num_rows($results) >= 1) { echo " <center><b>Keyword '$searchid' is found </b></center><br><br>" ; $output = ""; while($row = mysqli_fetch_array($results)) { $output .= "User ID: " . $row['uid'] . "<br />" . "Username :" . $row['username'] . "<br />" . "Phone: " . $row['phone'] . "<br />" . "Email: " . $row['acct_email'] . "<br /><br />" . "--------------------------------<br>"; } } if(mysqli_num_rows($results2) >= 1) { $output2 = ""; while($row = mysqli_fetch_array($results2)) { $output2 .= "User ID: " . $row['uid'] . "<br />" . "Username :" . $row['username'] . "<br />" . "Phone: " . $row['phone']. "<br />" . "Email: " . $row['acct_email'] . "<br />" . "--------------------------------<br>"; } echo $output . ' ' . $output2; } I will suggest though that you migrate to mysqli or PDO. It shouldn't be to difficult as you have mysqli in your second half of the code. Mysql is outdated and not really good to use anymore. It will be taken out soon™ Edit: this forum hates tabs! Edited April 28, 2013 by jcbones Quote Link to comment Share on other sites More sharing options...
Barand Posted April 28, 2013 Share Posted April 28, 2013 Why are you using two tables (table1 and table2) with seemingly identical structures instead of a single table? And later, why do you run the same query twice? $query = "SELECT * FROM table WHERE username LIKE '%$searchid%'";$query2 = "SELECT * FROM table WHERE username LIKE '%$searchid%'"; Quote Link to comment Share on other sites More sharing options...
xymcrush17 Posted April 28, 2013 Author Share Posted April 28, 2013 @jcbones thank you for correcting.. i coded it with some refrence ive found in google between rertrive data and search . So the code looks so bad . And im searching for pagination in code search php. If youare not mind .. would you help me please. @barand I created table2 because There's duplicate value in uid colomn in table1. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.