Jump to content

can anybody make better this code?


xymcrush17

Recommended Posts

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 by xymcrush17
Link to comment
Share on other sites

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 by jcbones
Link to comment
Share on other sites

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%'";

Link to comment
Share on other sites

@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.

Link to comment
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.