Connecting Tech Pros Worldwide Help | Site Map

Reading and displaying a Date from a mySql database

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:49 AM
paul@paullee.com
Guest
 
Posts: n/a
Default Reading and displaying a Date from a mySql database

Hi everyone,
I've just started using php, and although I am very impressed by it,
there are some things with which I am confounded!

What I'm trying to do is read in a Date from a mySql database and then
display it on screen (the Date is in the usual yyyy-mm-dd format).

If I use SQL on phpadmin
SELECT Date
FROM details
WHERE details.Name = "Paul Lee"
LIMIT 0 , 30

The result comes up 2005-01-01, so this seems to work.
But if I use the following:

$db = mysqli_connect("localhost","root","");
@mysqli_select_db($db, "personnel") or die ( "Unable to select
database" );
$query = 'SELECT Date FROM details WHERE details.Name = "Paul Lee"
LIMIT 0, 30';

$result=mysqli_query($db, $query);

echo "<br>";

echo $result;

mysqli_close($db);

I get "Object id #2" displayed on screen.
I have tried to use the explode function to separate the months, year
and day using the "-" as a delimeter, but this doesn't work, and I
can't seem to get the other php date/time functions to work either.
Thanks for your help

Paul


  #2  
Old July 17th, 2005, 10:49 AM
Steve
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

[color=blue]
> $result=mysqli_query($db, $query);
> [...]
> echo $result;
> [...]
> I get "Object id #2" displayed on screen.[/color]

mysqli_query() returns a resource that needs further processing...

See:
http://www.php.net/manual/en/functio...use-result.php

---
Steve

  #3  
Old July 17th, 2005, 10:49 AM
Alvaro G Vicario
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

*** paul@paullee.com wrote/escribió (19 Jan 2005 07:23:33 -0800):[color=blue]
> $result=mysqli_query($db, $query);
> echo $result;
>
> I get "Object id #2" displayed on screen.[/color]

From manual (http://es2.php.net/mysqli_query):

Return Values

Returns TRUE on success or FALSE on failure. For SELECT, SHOW, DESCRIBE or
EXPLAIN mysqli_query() will return a result object.

So it's normally not a good idea to print an object: printing works fine
with strings, integers... but not with objects.

Just look at the examples on the page, they're pretty clear.

In any case, if you're learning PHP I suggest you forget (by now) about the
mysqli_* functions: they're rather recent and only work with PHP 5+ and
MySQL 4.1+ (not the most common scenery). Try mysql_* instead:

http://es2.php.net/manual/en/ref.mysql.php

--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
  #4  
Old July 17th, 2005, 10:49 AM
paul@paullee.com
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

Hi,
There were some historical reasons why we went for mysqli rather than
mysql...no one can recall what they are though!

I've modified the code to use mysql, but at the line:

$db = mysql_connect("localhost","root","");

I get:

Fatal error: Call to undefined function mysql_connect() in
d:\wamp\www\time.php on line 15

Hmmm....!

Paul

  #5  
Old July 17th, 2005, 10:49 AM
paul@paullee.com
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

Hi,
There were some historical reasons why we went for mysqli rather than
mysql...no one can recall what they are though!

I've modified the code to use mysql, but at the line:

$db = mysql_connect("localhost","root","");

I get:

Fatal error: Call to undefined function mysql_connect() in
d:\wamp\www\time.php on line 15

Hmmm....!

Paul

  #6  
Old July 17th, 2005, 10:49 AM
paul@paullee.com
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

Incidentally, I can use mysqli_ ... to connect to a mysql database and
retrieve string data, such as Names etc.

  #7  
Old July 17th, 2005, 10:49 AM
Steve
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

[color=blue]
> Incidentally, I can use mysqli_ ... to connect to a mysql database[/color]
and[color=blue]
> retrieve string data, such as Names etc.[/color]

I don't get it. In your original post you give an example of code that
does not display anything from a database, string or otherwise (and
could not because it is incomplete).

Here you are saying that you CAN retrieve string data successfully.

Could you give us some sample code that DOES work for you, so we can
compare it with the code that doesn't work for you.

---
Steve

  #8  
Old July 17th, 2005, 10:50 AM
paul@paullee.com
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

Alright. This works:

$db = mysqli_connect("localhost","root","");
@mysqli_select_db($db, "personnel") or die ( "Unable to select
database" );
$query="SELECT * FROM details";

But this doesn't:

$db = mysqli_connect("localhost","root","");
@mysqli_select_db($db, "personnel") or die ( "Unable to select
database" );
$query = 'SELECT Date FROM details WHERE details.Name = "Paul Lee"';
Paul

  #9  
Old July 17th, 2005, 10:50 AM
Steve
Guest
 
Posts: n/a
Default Re: Reading and displaying a Date from a mySql database

> $query = 'SELECT Date FROM details WHERE details.Name = "Paul Lee"';

Depends on what "doesn't work" means, and we'd have to see the rest of
your code (the bit that tries to use the returned result) to be sure,
but one possible problem is that you have mixed-case columns names in
this SELECT - you will have to refer to them using the exact name you
gave in the SELECT statement, otherwise PHP will not find them in the
associative array.

---
Steve

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.