 |
|
04-21-2004, 12:36 PM
|
Database Help
|
Posts: 12
Location: New Zealand
|
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT TOP 1 first FROM contacts ORDER BY enter_dt desc";
mysql_query($query);
echo "What goes here";
mysql_close();
?>
That selects the last entry made into table 'contacts' but I dont know how to input it into my page, I want to echo it but im not sure how. Any ideas?
|
|
|
|
04-21-2004, 12:44 PM
|
|
Posts: 23
Location: UK
|
For a start: I expect you need quotes around localhost (unless its a constant...).
Have a look at php.net:
http://uk.php.net/manual/en/ref.mysql.php
Basically you need:
PHP Code:
include("dbinfo.inc.php");
$db=mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT TOP 1 first FROM contacts ORDER BY enter_dt desc";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
echo $row['FIELDNAME'];
}
mysql_close($db);
You will need to replace FIELDNAME with the name of the field, i assume 'first'.
|
|
|
|
04-21-2004, 12:51 PM
|
Help
|
Posts: 12
Location: New Zealand
|
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/caspian/public_html/stuff/members/k.php on line 6
This has happend before, im not sure why
|
|
|
|
04-21-2004, 12:55 PM
|
|
Posts: 23
Location: UK
|
That is becasue your SQL query is wrong...
Perhaps start with a more simple SQL query, something like:
Code:
SELECT * FROM contacts
See if that works, then maybe add to that. I have personally never used the TOP command... not sure about that bit (maybe it should go at the end of statement).
|
|
|
|
04-21-2004, 12:58 PM
|
Ok
|
Posts: 12
Location: New Zealand
|
I got the TOP code from someone in another forum, he knows allot about sql but is gone now so ill have to wait. ill try select from
|
|
|
|
04-21-2004, 12:58 PM
|
|
Posts: 23
Location: UK
|
Maybe it should be:
Code:
SELECT first FROM contacts ORDER BY enter_dt DESC LIMIT 1
|
|
|
|
04-21-2004, 01:11 PM
|
Nope
|
Posts: 12
Location: New Zealand
|
Query is still wrong, ill try changing them around a bit
|
|
|
|
04-21-2004, 01:15 PM
|
|
Posts: 23
Location: UK
|
How about :
Code:
SELECT `first` FROM `contacts` ORDER BY `enter_dt` DESC LIMIT 1
Maybe 'first' is a keyword...
|
|
|
|
04-21-2004, 01:19 PM
|
Nope
|
Posts: 12
Location: New Zealand
|
Nope, I thought it would be simple lol, all I wanted to do was show the newest member who signs up on my site
|
|
|
|
04-21-2004, 01:25 PM
|
|
Posts: 23
Location: UK
|
Heh, it shouldn't be this difficult.
Didn't the
Code:
SELECT * FROM `contacts`
code work?
Are you sure your tables/db are set up right? Paste your code here so I can see (and the errors you are getting).
|
|
|
|
04-21-2004, 01:29 PM
|
|
Posts: 12
Location: New Zealand
|
<?
include("dbinfo.inc.php");
$db=mysql_connect('localhost',$username,$password) ;
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT TOP 1 first FROM contacts ORDER BY enter_dt desc";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
echo $row['first'];
}
mysql_close($db);
?>
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
echo "Database created";
?>
select *from contacts shows all info in the row, not just 'first'
|
|
|
|
04-21-2004, 01:41 PM
|
|
Posts: 23
Location: UK
|
Looking at the second lump of code: you don't seem to have a `enter_dt` field... that would, of course, be a problem...!
Perhaps you have added that seperately...
Ok, you could try one of the following two (assuming the db is ok):
PHP Code:
include("dbinfo.inc.php");
$db=mysql_connect('localhost',$username,$password) ;
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT TOP 1 FROM `contacts` ORDER BY `enter_dt` DESC";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
echo $row['first'];
}
mysql_close($db);
I'm not sure if you need to specify which field in the SQL above, if so, try and use *.
PHP Code:
include("dbinfo.inc.php");
$db=mysql_connect('localhost',$username,$password) ;
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT * FROM `contacts` ORDER BY `enter_dt` DESC";
$result=mysql_query($query);
$row=mysql_fetch_array($result)
echo $row['first'];
mysql_close($db);
This one should get all of the rows, but just print the first.
If this dosen't work then....!!
Post the errors you are getting btw.
Good luck!
|
|
|
|
04-21-2004, 01:48 PM
|
|
Posts: 12
Location: New Zealand
|
enter_dt...i never made that row...LOL @ me..too overconfident
|
|
|
|
04-21-2004, 01:48 PM
|
|
Posts: 23
Location: UK
|
Well I guess we learn from our mistakes... hopefully!
I can't find anything about this TOP command in the mysql documentation, it may be an ODBC (MS) thing... I think you need to use "LIMIT 1" at the end of the statement...
|
|
|
|
04-21-2004, 01:59 PM
|
|
Posts: 12
Location: New Zealand
|
for the enter_dt, is it just a plain row? varchar, no_null?
|
|
|
|
04-21-2004, 02:16 PM
|
|
Posts: 23
Location: UK
|
Well I guess thats up to you...
You could use DATE, TIME, DATETIME... whatever you want, even a string of some sort I suppose.
However, I would recomend using DATETIME, this will store the date and time together. (Probably in the format of 'yyyy-mm-dd hh:mm:ss').
It's a field btw, not a row.
|
|
|
|
04-21-2004, 02:54 PM
|
|
Posts: 90
Location: Canada
|
Well screw reading all that...
Why dont you just make a new mySQL table called...
Newest or somin and when someone signs up write the user name there aswell?

__________________
PHP Code:
<?
$User = 'wipeout'
$Class = 'zombie'
phpGroupy('$User, da ,$Class')
?>
|
|
|
|
04-21-2004, 03:41 PM
|
|
Posts: 23
Location: UK
|
wipeout, da ,zombie
I'm not really sure what you are trying to say... but it sounds like you are suggesting that you should have a seperate table to store one row for the newest user... why!?
Perhaps if you did read the whole thread you would understand the problem.
|
|
|
|
04-21-2004, 05:34 PM
|
|
Posts: 90
Location: Canada
|
Quote:
|
Originally Posted by wipeout
>>>Well screw reading all that...<<<<
|
Meh I prefer lite reading....
I am way to lazy to read it all...
I mean comon did you look at my post?
I would rather make a table or a text file to write the user to on sign up then go through pointless unneeded scripting...
Whats wrong with that? It saves time, file size, and all and all it works just aswell.
Meh either way....
I was just making a subjestion...
If you think its supid and lazy, well ok...
But don't make me look inferior just becuase I am lazy lol

__________________
PHP Code:
<?
$User = 'wipeout'
$Class = 'zombie'
phpGroupy('$User, da ,$Class')
?>
|
|
|
|
04-21-2004, 07:15 PM
|
|
Posts: 3,108
Location: Toronto, Ontario
|
You wouldn't do that, wipeout, because its highly unpractical and inefficient.
|
|
|
|
|
« Reply to Database Help
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|