Need Help

Discussion in 'MySQL' started by joyantony07, Jul 30, 2008.

  1. #1
    Hi
    I am very new in mysql, I got stuck to retrieve data from the database. I have a table contains 4 fields Date, Ordertime, ordervalue and Orderdes Some how I am managed to made the query with php

    $sql_link = mysql_connect('localhost', 'admin', '1admin');
    mysql_select_db('order', $sql_link);

    $ordervalue = $_REQUEST['ordervalue'];
    $query = "SELECT date, ordertime, orderdes from order_table where ordervalue ='" . $ordervalue . "' ORDER BY date, ordertime";

    $result = mysql_query($query);
    $numofrows = @mysql_num_rows($result);

    print '<table width="525" border="0" cellpadding="2" cellspacing="1">';
    print '<TR><td width="20%"><b>Date<b /></td><td width="20%"><b>Time<b /></td>
    <td width="60%"><b> Order Description <b /></td</TR>';
    for($i = 0; $i < $numofrows; $i++)
    {
    $row = @mysql_fetch_array($result);
    if($i % 2)
    {
    print '<TR bgcolor="#EDEBEF">';
    }
    else
    {
    print '<TR bgcolor="#D7DBF6">';
    }
    print "<td>" . $row['date']."</td><td>" . $row['ordertime']."</td><td>" . $row['orderdes']."</td>\n";
    print "</TR>\n";
    }
    print "</table>\n";

    I am getting the result like the below table

    Date Time Order Description
    2008-07-16 10:15 Item #15
    2008-07-16 12:45 Item #26
    2008-07-19 18:00 Item #05

    Now I am trying to alter my table like below but I got stuck very badly, please help me friends!

    2008-08-16
    10:15 Item #15
    12:45 Item #26
    2008-08-19
    18:00 Item #05
     
    joyantony07, Jul 30, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    I would do it like this:

    
    
    $ordervalue = (float)$_REQUEST['ordervalue'];
    $query = "SELECT date, ordertime, orderdes from order_table where ordervalue ='" . $ordervalue . "' ORDER BY date, ordertime";
    
    $numofrows = @mysql_num_rows($query);
    
    print '
    <table width="525" border="0" cellpadding="2" cellspacing="1">
    <tr>
    <td width="20%"><b>Date</b></td>
    <td width="20%"><b>Time</b></td>
    <td width="60%"><b> Order Description </b></td>
    </tr>';
    
    $i = 0;
    
    while($row = @mysql_fetch_array($result))
    {
    
    	if($i%2!=0)
    	{
    		print '<tr bgcolor="#EDEBEF">';
    	}
    	else
    	{
    		print '<tr bgcolor="#D7DBF6">';
    	}
    
    	print "<td>" . $row['date']."</td><td>" . $row['ordertime']."</td><td>" . $row['orderdes']."</td>\n";
    	print "</tr>\n";
    
    $i++;
    }
    print "</table>\n";
    
    
    PHP:
    You should get an editor that validates code. You had several tags that were open, or incorrectly formatted. Also I added the (float) to the request variable to help protect against sql injection.
     
    jestep, Jul 30, 2008 IP
  3. andrewgjohnson

    andrewgjohnson Active Member

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Just a note: MySQL has a column type called DATETIME that allows you to store the exact moment the order took place; rather than separating it into separate DATE and TIME fields.
     
    andrewgjohnson, Jul 30, 2008 IP