Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP/SQL Question

Options
  • 24-04-2010 1:27pm
    #1
    Closed Accounts Posts: 732 ✭✭✭


    I have 3 values entered into a database from a form, I am adding these values 2gether in php and I need to print out the biggest sum of these values together.

    Some code below:
    $fname = $_POST;
    $surname = $_POST;
    $student_number = $_POST;
    $exam = $_POST;
    $project = $_POST;
    $participation = $_POST;
    $totalmark = "";
    $sqlquery = "INSERT INTO details VALUES ('$fname', '$surname', '$student_number', '$exam', '$project', '$participation')";
    $results = mysql_query($sqlquery);

    I tried this but it wont work...

    $sqlquery2 = "SELECT MAX(totalmark) FROM ("SELECT exam + project + participation AS totalmark FROM details")";
    $results2 = mysql_query($sqlquery2);

    It doesn't work, maybe i have the quotes in the wrong places.... not sure why it wont work????


Comments

  • Registered Users Posts: 9,192 ✭✭✭RobertFoster


    If you're looking for the highest mark from all the entries in "details", try this:
    SELECT exam + project + participation AS totalmark FROM details ORDER BY totalmark DESC LIMIT 1
    
    ORDER BY specifies which value to order the table with.
    DESC highest totalmark -> lowest totalmark
    And LIMIT n limits the number of results to however many you'd like.

    You were going wrong in your $sqlquery2 because FROM is meant to reference a table, and not another SELECT query.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    [php]echo mysql_error();[/php]

    Might give you more insight


  • Closed Accounts Posts: 732 ✭✭✭scarymoon1


    Thanks for replies.... so to print to screen id do the below right?? But I dont think I did the echo statement right.

    <table width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td align="center"> <strong> Exam Details </strong> </td>
    </tr>
    <tr>
    <td>
    <table width="100%" cellpadding="5" cellspacing="5" border="1">
    <?php
    while($row = mysql_fetch_array($results2))
    {
    ?>
    <tr>
    <td align"right"> Highest Mark: </td>
    <td align ="left"><?php echo $row[$results2]?></td>
    </tr>


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    [php]
    <?php
    echo $row[$results2]
    ?>
    [/php]

    this is wrong. $results2 is an result set and will not give you the desire result you want to achieve.

    usually when working with database result sets you'll be looking to do this

    [php]
    <?php
    echo $row;

    //So in robert fosters sql statement to echo the total mark would be like this.
    $row;
    ?>
    [/php]


  • Closed Accounts Posts: 732 ✭✭✭scarymoon1


    thanks - that worked. one last question.... i now need to print the name of the person who has the highest mark, plus who got the lowest and average mark. will i need to do multiple_query?? Or can I do the following:

    <?php // code to get final grades
    $sqlquery2 = "SELECT exam + project + participation AS totalmark FROM details ORDER BY totalmark DESC LIMIT 1";
    $results2 = mysql_query($sqlquery2);
    $sqlquery3 = "SELECT fname, surname FROM details";
    $results3 = mysql_query($sqlquery3);
    ?>

    <body>
    <center><h1> Exam Results </h1></center>

    <table width="500" border="0" bgcolor="#CCCCCC">
    <?php
    while($row2 = mysql_fetch_array($results3))
    {
    ?>
    <tr>
    <td width="36%" align="right"> Student with highest result is: </td>
    <td width="64%" align="left"><?php echo $row?>
    </td>
    </tr>
    <?php
    }
    ?>

    <?php
    while($row = mysql_fetch_array($results2))
    {
    ?>
    <tr>
    <td width="36%" align="right"> Student with highest result is: </td>
    <td width="64%" align="left"><?php echo $row?>
    </td>
    </tr>
    <?php
    }
    ?>
    </table>
    </body>
    </html>


  • Advertisement
  • Closed Accounts Posts: 732 ✭✭✭scarymoon1


    sorry scrap my last post - ive figured it out :) thanks for replies.


Advertisement