Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading and displaying a Date from a mySql database

paul@paullee.com
Guest
 
Posts: n/a
#1: Jul 17 '05
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


Steve
Guest
 
Posts: n/a
#2: Jul 17 '05

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

Alvaro G Vicario
Guest
 
Posts: n/a
#3: Jul 17 '05

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
--
paul@paullee.com
Guest
 
Posts: n/a
#4: Jul 17 '05

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

paul@paullee.com
Guest
 
Posts: n/a
#5: Jul 17 '05

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

paul@paullee.com
Guest
 
Posts: n/a
#6: Jul 17 '05

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.

Steve
Guest
 
Posts: n/a
#7: Jul 17 '05

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

paul@paullee.com
Guest
 
Posts: n/a
#8: Jul 17 '05

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

Steve
Guest
 
Posts: n/a
#9: Jul 17 '05

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

Closed Thread