Reply to topic  [ 10 posts ] 
MySQL help 
Author Message
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Hi all, I've got a database for my job list and it works a treat, however, I want to change the format of the date column. I currently have:

Code:
$mysqli = mysqli_connect("localhost", "root", "root", "Job_list");
      $sql = "SELECT * FROM Jobs ORDER BY Job_number DESC";
      $res = mysqli_query($mysqli, $sql);
      ?>

      <table class="joblist">
      <tr class="joblisttitle">
      <td><a href="order/ascjobnumb.php">Job number</a><br /><img src="pics/downarrer.gif" /></td><td><a href="order/ascclient.php">Client</a></td><td><b>Description</b></td><td><b>Date</b></td><td><a href="order/ascstatus.php">Status</a></td><td><b>Notes</b></td>
      </tr>

      <?php
      while ($line = mysqli_fetch_array ($res, MYSQLI_ASSOC)) {
      ?>
      
      <tr>
      <td id="jobnumber"><?php echo $line[Job_number];?></td>
      <td id="client"><?php echo $line[Client];?></td>
      <td id="description"><?php echo $line[Description];?></td>
      <td id="date"><?php echo $line[Date];?></td>
      <td id="status"><?php echo $line[Status];?></td>
      <td id="notes"><?php echo $line[Notes];?></td>
      <td><form action="editJob.php" method="POST"><input type="hidden" name="jobnumber" value="<?php echo $line[Job_number];?>" /><input type="hidden" name="status" value="<?php echo $line[Status];?>" /><input type="image" img src="pics/edit.gif" alt="Edit" /></td></form>
      </tr>

      <?php      
      }
      ?>
      
      </table>


I know I need to use the DATE_FORMAT() somehow as:

Code:
SELECT DATE_FORMAT('Date', '%d/%m/%Y')


The trouble is, I don't know where to put it. If I put it as part of the search query, it doesn't work, nor does it work if I do it separately.

_________________
I've finally invented something that works!

A Mac User.


Mon Jan 10, 2011 1:43 pm
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Well, I've done it in PHP by using the following:

Code:
      <?php
      while ($line = mysqli_fetch_array ($res, MYSQLI_ASSOC)) {
      $date = $line[Date];
      $date = strtotime($date);
      $date = date('d/m/Y', $date);
      ?>
      
      <tr>
      <td id="jobnumber"><?php echo $line[Job_number];?></td>
      <td id="client"><?php echo $line[Client];?></td>
      <td id="description"><?php echo $line[Description];?></td>
      <td id="date"><?php echo $date;?></td>
      <td id="status"><?php echo $line[Status];?></td>
      <td id="notes"><?php echo $line[Notes];?></td>
      <td><form action="editJob.php" method="POST"><input type="hidden" name="jobnumber" value="<?php echo $line[Job_number];?>" /><input type="hidden" name="status" value="<?php echo $line[Status];?>" /><input type="image" img src="pics/edit.gif" alt="Edit" /></td></form>
      </tr>

      <?php      
      }
      ?>
      
      </table>


I'd still like to know how to make MySql do the work if anyone knows how though.

_________________
I've finally invented something that works!

A Mac User.


Mon Jan 10, 2011 3:31 pm
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Had the whole thing working a treat on MAMP, so installed it on my ISP's server and it mostly works. However, I get the following error when trying to update a record:

Quote:
Could not update record: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'time.' WHERE Job_number = '1021'' at line 1


The code I'm using is:


Code:
$sql = "UPDATE Jobs SET Description = '$_POST[description]', Status = '$_POST[status]', Notes = '$_POST[notes]' WHERE Job_number = '$_POST[jobnumber]'";


I really can't see anything wrong in that and it worked fine on MAMP. MySql version 5.1 on my ISP, not sure what MAMP is on at the moment as I'm at home and it's at work.

_________________
I've finally invented something that works!

A Mac User.


Mon Jan 10, 2011 7:27 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
The main problem I've had from jumping from MAMP to Apache is capitalisation issues, MAMP seems somewhat lax about capitalisation whereas Apache is really strict. Might be work checking variable names, etc.


Mon Jan 10, 2011 10:02 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
I might just do that, I've already had on capitalisation issue, but that was just a simple URL.

_________________
I've finally invented something that works!

A Mac User.


Mon Jan 10, 2011 10:56 pm
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Well, tried it at work this morning and seems to be running fine. Don't know what all that was about.

_________________
I've finally invented something that works!

A Mac User.


Tue Jan 11, 2011 9:53 am
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
The SELECT DATE_FORMAT('Date', '%d/%m/%Y') works as follows:

SELECT col1, col2, col3, DATE_FORMAT(col4, '%d/%m/%Y) as myDate FROM ...

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Wed Jan 12, 2011 4:40 am
Profile ICQ
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Aah, thanks Dave, so I can't do a select all, have to enter each column and specify which one to format. Thanks, makes sense.

_________________
I've finally invented something that works!

A Mac User.


Wed Jan 12, 2011 11:24 am
Profile
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
tombolt wrote:
Aah, thanks Dave, so I can't do a select all, have to enter each column and specify which one to format. Thanks, makes sense.

Yep. Although I think mysql drops the "as" (SQL Server and Oracle use AS, I believe, the small changes always confuse me and I haven't done any SQL for a while), so it would be SELECT FORMAT_DATE(...) myDate FROM ...

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Thu Jan 13, 2011 4:30 am
Profile ICQ
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Cool thanks.

_________________
I've finally invented something that works!

A Mac User.


Thu Jan 13, 2011 1:12 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.