regarding mysql query with php

Codes here !

Moderators: macek, egami, gesf

myfahim
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Apr 26, 2012 11:13 pm

regarding mysql query with php

Postby myfahim » Mon May 28, 2012 7:40 am

Hi,

According to my PHP code, if there is more than one result it displays all result's all field. But I need there will show only one or two field of each result. And after click one of them it will show it's all field.

I am not a programmer, just do coding as I need with my little knowledge and help of forum.
Please someone help me.

Fahim

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: regarding mysql query with php

Postby Nullsig » Tue May 29, 2012 6:39 am

I think I understand what you are asking but I will be unable to help you without you submitting the code in question and a spec of what you are trying to attempt with more detail.

myfahim
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Apr 26, 2012 11:13 pm

Re: regarding mysql query with php

Postby myfahim » Tue May 29, 2012 7:50 am

Nullsig wrote:I think I understand what you are asking but I will be unable to help you without you submitting the code in question and a spec of what you are trying to attempt with more detail.

My code is given below:

query.html

Code: Select all

<html>
<body>
<form action="query.php" method="post" target="content">

<p align="center">

<font color="#FFFFFF">Account Name/Number: </font><input type="text" name="query" />

<input type="submit" />

</form>
</p>



</body>
</html>


query.php

Code: Select all

<?php
$search_words = $_POST['query'];
$total_char = strlen ($search_words);

$con = mysql_connect("localhost","root","123456");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("MY_DB", $con);

$result = mysql_query ("SELECT * FROM Accounts WHERE Account_Name LIKE '%$search_words%' OR Account_ID LIKE '%$search_words%'");

echo "<table border='1'>

<tr>
<th bgcolor=#66FF66>Account Number</th>
<th bgcolor=#66FF66>Account Name</th>
<th bgcolor=#66FF66>Account Type</th>
<th bgcolor=#66FF66>BTS</th>
<th bgcolor=#66FF66>Sector</th>
<th bgcolor=#66FF66>Client IP</th>
<th bgcolor=#66FF66>Client MAC</th>
<th bgcolor=#66FF66>Gateway Address</th>
<th bgcolor=#66FF66>Account Status</th>
<th bgcolor=#66FF66>Package</th>
<th bgcolor=#66FF66>Account Creation</th>
<th bgcolor=#66FF66>Client Address</th>
<th bgcolor=#66FF66>Contact Number</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td bgcolor=#FFFF99>" . $row['Account_ID'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Account_Name'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Account_Type'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['BTS'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Sector'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Client_IP'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Client_MAC'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['GW_Address'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Account_Status'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Package'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Time'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Client_Address'] . "</td>";
  echo "<td bgcolor=#FFFF99>" . $row['Contact_Number'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
}
?>


If in database, there are 2 entry of same "Account_Name", it displays both with all their fields. But I need it like: first it will show me "Account_ID" and "Account_Name" in query result with a hyper link or something like that. If I click a specific "Account_ID" of the result it will display me its all field.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: regarding mysql query with php

Postby Nullsig » Tue May 29, 2012 8:13 am

So the account IDs are different even if the account Name is the same?

myfahim
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Apr 26, 2012 11:13 pm

Re: regarding mysql query with php

Postby myfahim » Wed May 30, 2012 8:02 am

yes...account number is unique and auto generated

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: regarding mysql query with php

Postby Nullsig » Wed May 30, 2012 8:26 am

Okay.

So this is the code you will need for the first page:

Code: Select all

    <?php
    $search_words = $_POST['query'];
    $total_char = strlen ($search_words);

    $con = mysql_connect("localhost","root","123456");

    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("MY_DB", $con);

    $result = mysql_query ("SELECT * FROM Accounts WHERE Account_Name LIKE '%$search_words%' OR Account_ID LIKE '%$search_words%'");

    echo "<table border='1'>

    <tr>
    <th bgcolor=#66FF66>Account Number</th>
    <th bgcolor=#66FF66>Account Name</th>
    </tr>";

    while($row = mysql_fetch_array($result))
      {
      echo "<tr onclick=\"document.location.href = 'showFullAccount.php?accID=" . $row['Account_ID'] . "';\">";
      echo "<td bgcolor=#FFFF99>" . $row['Account_ID'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Account_Name'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    ?>



All I did was remove the columns for everything except the Account ID and the Account name. I also added an onclick event that will cause the page to navigate script that will display all of the information for a single account.


The code on the page would be roughly:

Code: Select all

    <?php
    $accountID = $_GET['accID'];

    $con = mysql_connect("localhost","root","123456");

    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("MY_DB", $con);

    $result = mysql_query ("SELECT * FROM Accounts WHERE Account_ID = '" . mysql_real_escape_string($accountID) . "'");

    echo "<table border='1'>

    <tr>
    <th bgcolor=#66FF66>Account Number</th>
    <th bgcolor=#66FF66>Account Name</th>
    <th bgcolor=#66FF66>Account Type</th>
    <th bgcolor=#66FF66>BTS</th>
    <th bgcolor=#66FF66>Sector</th>
    <th bgcolor=#66FF66>Client IP</th>
    <th bgcolor=#66FF66>Client MAC</th>
    <th bgcolor=#66FF66>Gateway Address</th>
    <th bgcolor=#66FF66>Account Status</th>
    <th bgcolor=#66FF66>Package</th>
    <th bgcolor=#66FF66>Account Creation</th>
    <th bgcolor=#66FF66>Client Address</th>
    <th bgcolor=#66FF66>Contact Number</th>
    </tr>";

    if($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td bgcolor=#FFFF99>" . $row['Account_ID'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Account_Name'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Account_Type'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['BTS'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Sector'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Client_IP'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Client_MAC'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['GW_Address'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Account_Status'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Package'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Time'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Client_Address'] . "</td>";
      echo "<td bgcolor=#FFFF99>" . $row['Contact_Number'] . "</td>";
      echo "</tr>";
      }else{echo "No account found with that ID";}
    echo "</table>";
    ?>


myfahim
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Apr 26, 2012 11:13 pm

Re: regarding mysql query with php

Postby myfahim » Sat Jun 02, 2012 2:09 am

yes...it is working...awesome!!!
thanks...


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests