Jump to content

Convert mysql query to php code


samadhan

Recommended Posts

Hi,

I am new to php coding. I have a mysql query which successfully runs and give output. Now I want to write it in php code so that I will get output and print it in table. I want to execute query for each application against all "aw..." tables.

Any help will be appreciated.

<html>
<?php
$Show= htmlspecialchars($_GET["Show"]);
$team= htmlspecialchars($_GET["team"]);
$tablename = htmlspecialchars($_GET["tablename"]);

$servername="localhost";
$username="root";
$password="";
?>

<?php

// Create connection with mySQL
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed:" . mysqli_connect_error());
}

//echo "Connected to mysql successfully";

//select a database to work with
mysqli_query( $conn , "USE plm_dashboard" );

Echo "<Body bgcolor="."white".">" ;
//echo $Show;
Echo "<br>";
Echo "<br>";
Echo "Applied Filters Are <br> ";
//echo "Team =  $team ";    echo "TableName= $tablename ";
$Local_team= "Reports";

if ($Show == "failed"){
        $Result_status = "passed";
    } else {
        // in this case the value of the $Result_status is 'all'
        $Result_status = "";
                }

echo "Result = $Show  <br>";    
    //$result =mysqli_query($conn ,"SELECT * FROM aw34_0605_tc1122 where Application="."'".$team."'");

$result = mysqli_query($conn ,"SELECT * FROM teamnames");
$result1 =mysqli_query($conn ,"SELECT * FROM aw34_0530_tc1015 (Select count(id) from aw34_0530_tc1015 where result ='passed' and application='ace')/(Select count(id) from aw34_0530_tc1015 where application='ace')*100");

    //$result =mysqli_query($conn ,"SELECT * FROM ". $tablename );
    //********** determine number of rows result set **********/
  // $row_cnt = $result->num_rows;

//mysqli_query()
//echo "Showing  $row_cnt Results ";    
//echo "<table border="."1".">
echo "<table border="."1"."align="."right".">
        <tr>
            <th>ID</th>
            <th>Internal Team Names</th>            
            <th width="."10px"." >Passed % for aw34_0530_tc1015</th>

        </tr>";


    while($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['ID'] . "</td>";
            echo "<td>" . $row['TeamInternalNames'] . "</td>";
            echo "<td>" . $row['TeamName'] . "</td>";
            // if ($row['Result'] == "passed") {
                // echo "<td style="."background-color:#33FF38".">P</td>";
            // } else {
                // echo "<td style="."background-color:#FF334C".">F</td>";
                // }

            echo "</tr>";

}
echo "</table>";
ECHO "BYE";

mysqli_close($conn);
Echo "<Body>";

?>
</html> 

Mysql query is: 

SELECT
(
    Select count(id) from aw34_0530_tc1015 where result ='passed' and application='ace'
) 
/ 
(
    Select count(id) from aw34_0530_tc1015 where application='ace'
)*100

post-204524-0-39362000-1497253233_thumb.png

post-204524-0-06013700-1497253238_thumb.png

Edited by samadhan
Link to comment
Share on other sites

I want to execute query for each application against all "aw..." tables.

 

This is now how you store data in a relational database -- or really any database.

 

Apparently you've kept adding new "aw" tables as new data came in, and now you have more than a dozen of duplicate tables with cryptic names and no idea how to put the data back together.

 

Before you make things even worse, you need to learn the basics of the relational model (there are plenty of online resources)  and repair your entire database. The second step would be to learn SQL. And then we can talk about PHP.

Link to comment
Share on other sites

One - don't mix php and html together.  Do your business logic like receiving the form input and validating it and doing your quries to get results and THEN do your html, inserting your results data (saved in php vars) where they need to be on the html page.

 

Two - don't use multiple php tags.  Write your php code with just one pair.  It will make perfect sense once you start doing it.

 

Three - you say " have a mysql query and now want to write it in php code".  Really?  I hope you mean PDO.

 

Four - read the manual ( you don know where the manual is?) about using pdo.  Some very good examples there.

 

Five - do as Jacques says - look at your (apparently) poor example of a database and make a better design.  Then your whole process will come together better.

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.