tags:

views:

515

answers:

7

Hello,

I have the following code:

<?php

$link = mysql_connect('localhost', 'root', 'geheim');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(ebay);


$query = "SELECT * FROM Auctions";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  foreach($row as $field=>$value)
  {
    echo "$field: {$value} <br />";
  }
}
mysql_close($link);

?>

Which works seemingly fine, and gives this output:

Connected successfully
Notice: Use of undefined constant ebay - assumed 'ebay' in C:\Programme\EasyPHP 2.0b1\www\test.php on line 10
ARTICLE_NO: '110268889894' 
ARTICLE_NAME: 'ORIGINAL 2008 ED HARDY GÜRTEL* MYSTERY LOVE * M *BLACK' 
SUBTITLE: '' 
CURRENT_BID: $0.00 
START_PRICE: $0.00 
BID_COUNT: 7 
QUANT_TOTAL: 0 
QUANT_SOLD: 1 
STARTS: 0000-00-00 00:00:00 
ENDS: 0000-00-00 00:00:00 
ORIGIN_END: 0000-00-00 00:00:00 
SELLER_ID: 7/8/2008 17:18:37 
BEST_BIDDER_ID: 11/5/2008 16:23:37 
FINISHED: 10/6/2008 17:23:37

The problem is that the data does not seem to be correct, the start and end times for example, is this because I set the mysql field to type DATETIME and it does not understand the numbers?

Is there a way to get the insert date from the current date when inserting with mysql(not php)

I would like to display the records as one row per line, how would I do this? And if possible, clicking on a record would display more information above it.

+2  A: 

You can insert the value NOW() - INSERT INTO my_table (my_date) VALUES(NOW())

To display one per line, move the <br> outside the foreach loop.

As for clicking - you'll need to explain what you want a bit more.

Greg
+3  A: 

You get this error

Notice: Use of undefined constant ebay - assumed 'ebay' in C:\Programme\EasyPHP 2.0b1\www\test.php on line 10

because

mysql_select_db(ebay);

should be

mysql_select_db("ebay");
Tom Ritter
A: 

It sounds like you may be undertaking a fairly large project, here, and that's tough when you're not very experienced with the language.

I know this doesn't answer your question directly, but if you're looking for a user-friendly interface for your MySQL database, you may want to look into PHPMyAdmin, an open-source MySQL front-end written in PHP. It's very well done and easy to use.

Lucas Oman
+1  A: 

PHP appears to be rendering the dates correctly, is it possible that they are simply empty within the database? I would suggest that you verify your database values to ensure that the Starts and Ends fields are indeed not empty.

Second, if you want to have a field default the current time when you insert a record in MySQL, you can use the following definition for the field:

ALTER TABLE Auctions
    ADD COLUMN startTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;

As far as rendering your output, I would use something like the following:

 <?php
 $records = array();

 while($row = mysql_fetch_array($result, MYSQL_ASSOC))
 {
      $records[] = $row;
 }

 include('view.php');    
 ?>

View.php:

 <table>
 <?php
  foreach($records as $row}
  {
  ?>
   <tr>
       <td><a href="view_record.php?num="<?php echo $row['Article Number']?>"><?php echo $row['Article Number'] ?></a></td>
       <td><?php echo $row['Article Name'] ?></td>
   </tr>
   <?php
   }
   ?>
   </table>

I have included the anchor tag on the Article Number as an example of one way in which you click on a link from the rendered table and look at further information for a specific record.

Noah Goodrich
A: 

Sounds to me like your INSERT statement isn't working properly. Are you sure the data you have in your DB table is correct? Try using a MySQL client to check that.

Tom
+9  A: 

I know this is a place to learn, so I do not try to discourage anyone to help you.

I just can't avoid to notice that your questions are really beginner questions. You may want to learn from a book or a programming course before trying to code an entire app.

This way you will prevent you from having to come here and go back each time you are stuck : when you are training, it happens a lot, and you will end spending more time waiting for answers in this website than actually coding...

EDIT : I did not mean to be rude. I am just saying that there are fora, newsgroups; websites and books that are just exactly what you need. Trying to solve this kind of problem in SO, just are just not getting the best of it.

And I do thing there is the right tool for the right task. Teaching the very basics is definitely not the SO strong point because it has not been design for it.

e-satis
Definitely, as e-satis said, not wanting to discourage but I can't help but notice these questions are a mere 10 minutes apart going from no DB access to a DB related error. SO is more of a last resort. Signal to noise ratio is going to be horrible if we don't first try to solve problems ourselves.
Abyss Knight
Who Cares if this is a begginner question. If it is a waste of your time, then don't spend any time on the question. Move on, let someone else try and help out. Isn't that the point, helping out?
kevtrout
You are right. We say that for him. We can choose not to anwser, but if he relies on SO for that, HIS time could be wasted. SO is great but slow, and requires some XP to make the difference a nice answer from junk... The SO system is just probably not what he needs. No tools suit to any task.
e-satis
A: 

It seems very probable (one might be inclined to say that it is obvious) that the problem is not this snippet of code but the part where you insert the data into the DB.

Svante