Re: [PHP-DB] PHP4, mssql_* and SQL Server 2005

2008-11-05 Thread Krister Karlström

Hi!

Have you checked that the query is escaped correctly? All single quotes 
(') needs to be escaped with another single quote, but that has been for 
MS-SQL all the time so I'll doubt that the problem is escaping.


Another issue I noticed is that SQL Server 2005 handles SMALLDATETIME 
coulmns slightly different, and the default formatting of the retuned 
date is in some collations different than in SQL Server 2000. So if you 
have any queries relying on a where clause that checks a SMALLDATETIME 
column you might need to adjust the date format.


Migrating to SQL 2005 might also cause some problems with character 
encoding, since SQL 2005 internally works with Unicode. At least for us 
who needs other characters than 7-bit ASCII... I had big trouble with 
moving ISO-8859-15 XML documents to a SQL Server 2005 Unicode server and 
parsing them into a native XML datatype (which is a really nice 
feature!), but the problems can be overcomed with a litte extra work...


You could also check that you use the correct protocol version of 
FreeTDS, if you're running PHP under Linux. I don't know how PHP 
connects to MS-SQL on Windows though.


Currently I have no other thoughts of what could cause your problems, at 
least without not being able to actually see the failed queries.


Regards,
Krister Karlström, Helsinki, Finland

Chris wrote:

Please always cc the mailing list so others can offer extra 
advice/suggestions etc.


Weaver Hickerson wrote:

they work in query analyzer.


ok so the queries aren't the problem.

do you get an error? what is it?

Might need to play with these settings:

http://www.php.net/manual/en/function.mssql-min-error-severity.php
http://www.php.net/manual/en/function.mssql-min-message-severity.php

And they seem to work on a new server I setup for testing that has 
ZendCore and on which I installed the Microsoft PHP driver and ported 
the code to that driver. :( 


That does seem to point to the driver, not sure what other suggestions I 
can offer.




--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] PHP4, mssql_* and SQL Server 2005

2008-11-05 Thread Krister Karlström
Ouch! That was like the.. longest.. query that I've ever seen! I'm 
surprised that it actually works and even more impressed that someone 
actually figured that out... It might take a long time to run, since it 
uses so many subqueries (if there's lots of data or even a moderate 
amount of data). Are you sure there's no easier way to fetch your data? 
Most subqueries can be re-written using a join and some more 
where-comparisons instead...


Actually I'm more amazed that you actually can send that query and that 
it works.. There's a slight chance that your problems might be solved by 
rewriting that query... Maybe it's just to long or something, I don't 
know. I will however not take the time to refactor that query... :)


Are you running PHP under Linux or Windows? You said that you only found 
two queries that breaks? Wll, if the shorter ones works, but the longer 
ones hangs, then maybe that's the problem...? :)


Regards,
Krister Karlström, Helsinki, Finland

Weaver Hickerson wrote:


Krister -
I've included the query below - If I paste it in query analyzer on 2000, or 
2005 it works fine.

Just coming through PHP4 to 2005, it hangs.  I actually don't get an error, and 
then the browser says the server is too busy to handle the request.  Strange 
things like that. (I am using Zend Core with Apache and PHP 5).  I will check 
for smalldatetime fields.


SELECT   COUNT(DISTINCT C.CHART_ID) AS TOTAL,
 SUM(ISNULL(FEE_SCHEDULE.FEE,0) * UNITS) AS TOTALFEE
FROM HL7 H,
 CHART C
 LEFT OUTER JOIN CHART_CODE D
 LEFT OUTER JOIN CHART_CODE_MODIFIER CCM
   ON CCM.CHART_CODE_ID = D.CHART_CODE_ID
  AND CCM.CHART_CODE_MODIFIER_ID = (SELECT 
MIN(CHART_CODE_MODIFIER_ID)
FROM   
CHART_CODE_MODIFIER
WHERE  
CHART_CODE_ID = D.CHART_CODE_ID)
 LEFT OUTER JOIN FEE_SCHEDULE
   ON FEE_SCHEDULE.CPT_CODE = D.CHART_CODE
  AND FEE_SCHEDULE.START_DATE = (SELECT MSH_DTM
  FROM   HL7,
 CHART,
 CHART_CODE
  WHERE  
CHART_CODE.CHART_ID = CHART.CHART_ID
 AND 
CHART.HL7_ID = HL7.HL7_ID
 AND 
CHART_CODE.CHART_CODE_ID = D.CHART_CODE_ID)
  AND FEE_SCHEDULE.END_DATE = (SELECT MSH_DTM
FROM   HL7,
   CHART,
   CHART_CODE
WHERE  
CHART_CODE.CHART_ID = CHART.CHART_ID
   AND 
CHART.HL7_ID = HL7.HL7_ID
   AND 
CHART_CODE.CHART_CODE_ID = D.CHART_CODE_ID)
  AND FEE_SCHEDULE.SENDING_FACILITY = (SELECT 
SENDING_FACILITY
   FROM   HL7,
  CHART,
  
CHART_CODE
   WHERE  
CHART_CODE.CHART_ID = CHART.CHART_ID
  AND 
CHART.HL7_ID = HL7.HL7_ID
  AND 
CHART_CODE.CHART_CODE_ID = D.CHART_CODE_ID)
  AND FEE_SCHEDULE.CODE_TYPE = 'pro'
  AND FEE_SCHEDULE.MODIFIER = CCM.MODIFIER
 LEFT OUTER JOIN FEE_SCHEDULE FS
   ON FS.CPT_CODE = D.CHART_CODE
  AND FS.START_DATE = (SELECT MSH_DTM
FROM   HL7,
   CHART,
   CHART_CODE
WHERE  CHART_CODE.CHART_ID 
= CHART.CHART_ID
   AND CHART.HL7_ID = 
HL7.HL7_ID
   AND 
CHART_CODE.CHART_CODE_ID = D.CHART_CODE_ID)
  AND FS.END_DATE = (SELECT MSH_DTM
  FROM   HL7

Re: [PHP-DB] mysqli_stmt_bind_param question

2008-04-04 Thread Krister Karlström

Hi!

First of all you must connect to the server and then perform an init to 
get a statement. Maybe you dropped that code out here... Please have a 
look at the manual page:


http://www.php.net/manual/en/function.mysqli-stmt-prepare.php

In order to get the result you also need to bind the result to a 
variable and then fetch the data.


I'll guess that your error comes from an unsuccessful initialization of 
your prepared statement.


If you don't want to include your primary key column that is 
autogenerated you just ignore it. That means to write a proper query like:


INSERT INTO legioCurrent (column_1, column_2, ...)
VALUES (?,?,...)

I would also like to propose for you to use the object oriented style, 
since it's more readable and easier to get an overview of. You also 
don't need to mess around with so many parameters.


Greetings,
Krister Karlström, Helsinki, Finland

Jason Pruim wrote:


Hi Everyone,

I have a MySQL database that I am accessing from PHP. The table in 
question has a auto increment field on it and I don't want to include 
that in my add/edit/update query's to the database... But I can't seem 
to figure out how to ignore it? Everything I have done seems to fail.. I 
am using prepared statements so Im not sure it that is it, but I would 
like to keep using them since it reduces the security issues a little... 
Here is the code from one of my prepared statements:


//Create the statement
mysqli_stmt_prepare($stmt, INSERT INTO legionCurrent VALUES ( 
?,?,?,?,?,?,?,?));
mysqli_stmt_bind_param($stmt, '', $FName, $LName, $Add1, $Add2, 
$City, $State, $Zip, $XCode)or die(mysqli_error($addlink));

//Add the record
mysqli_stmt_execute($stmt) or die(mysqli_error($addlink));

Here is the error I get in my logs:

[Fri Apr  4 09:35:32 2008] [error] PHP Warning:  
mysqli_stmt_bind_param() [a 
href='function.mysqli-stmt-bind-param'function.mysqli-stmt-bind-param/a]: 
invalid object or resource mysqli_stmt\n in 
/Volumes/RAIDer/webserver/Documents/dev/OLDBv2/add.php on line 91


Line 91 is the mysqli_stmt_bind_param() line above

My database structure looks like this:

| FName  | LName | Add1| Add2| City | State | Zip  | 
XCode  | Reason  | Record


I know there is away to do it, but all my searching and reading hasn't 
told me anything... I've looked in the php manual, and mysql, plus 
various websites... and I just can't figure this one out... RTFM's are 
appreciated, as long as M is defined! :)


Thanks for taking the time to look!



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
[EMAIL PROTECTED]


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] mysqli_stmt_bind_param question

2008-04-04 Thread Krister Karlström

Hi again Jason!

Jason Pruim wrote:


On Apr 4, 2008, at 2:35 PM, Krister Karlström wrote:


You also don't need to mess around with so many parameters.


What do you mean by this?


Since you're working in object context you mostly just refer to your 
variable with the name of the object and the calls the method with the 
arrow - operator. But anyway, if you're not familiar with object 
orientation then I think it's no point for me to go into this discussion 
right now... :-)


On the manual page, to which I posted a link to you in my previous post, 
there's the same code in both procedural style and in object oriented 
style. I think you'll get the point by comparing those two examples.


Greetings,
Krister Karlström, Helsinki, Finland

--
* Ing. Krister Karlström, Zend Certified Engineer *
* Systemutvecklare, IT-Centralen  *
* Arcada - Nylands Svenska Yrkeshögskola  *
* Jan-Magnus Janssons plats 1, 00550 Helsingfors, Finland *
* Tel: +358(20)7699699  GSM: +358(50)5328390  *
* E-mail: [EMAIL PROTECTED]   *

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] mssql_connect error only on command line

2008-04-02 Thread Krister Karlström

Hi!

That description did not help us very much... I think you're just 
connecting using the wrong credentials, or using no at all. From where 
do you grab the hostname, username and password? Maybe it's stored in 
your session or something, and that session is not working in your 
command line environment..? It could be just about anything that's your 
problem! :)


My advice is to take a look att the parameters using to connect to the 
server, maybe echo them to stdout before doing your connect... It might 
be that they are not defined when running your script on command-line.


In general, very few script are written in such a way that they work the 
same way in a webserver environment and on command line. You should 
design your scripts for either the environment.


Greetings,
Krister Karlström, Helsinki, Finland

Molteni Davide wrote:


 Hi,

I have a problem with all my php scripts that use MS SQL Server, but only
when I start them from the command line
I'm using Apache 2.2.4 with PHP 5.2.5 on a server Win2k3 with SP2, and MS
SQL Server 2000 on another Win2k3 server.
If I run the same script using a webbrower as an internet page it works
fine,
but if I run it from the command line (c:\php scipt.php) I will receive
messages like this:
Warning: mssql_connect(): Unable to connect to server: servername,1433 in
c:\script.php on line #
Can someone have an idea what could be the problem?
Can someone please help me?
What should I do to make it working?
Tanks a lot in advance.

Davide



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] mssql_connect error only on command line

2008-04-02 Thread Krister Karlström

Hi!

Please have a look at the manual pages (if you have not allready done 
that). There might be some information for you in the user comments:


http://www.php.net/manual/en/function.mssql-connect.php

Manual page about command line scripting:

http://www.php.net/manual/en/features.commandline.php

Now you're using windows so I don't think that FreeTDS is used, right? 
But if you were on a Linux environment you should note that you need to 
have the server configured in the config file for FreeTDS, exactly in 
the same way your connecting. That means if you use fully qualified 
domain names the server should be registered with the same hostname in 
freetds.conf. But that has probably nothing to do with this now since 
you're using Windows...


The documentation of mssql_connect() says that you can specify a 
portnumber like you did, but I found some examples on the net using a 
colon (:) instead. Try that also, or try with the default port number... 
I'm not sure if that is gonna help you at all, but it's worth trying...


Or maybe you have some problem with name resolution through DNS... Try 
using the IP adress when connecting... It's really hard to tell what's 
wrong. However, I have seldom used PHP in a Windows environment so maybe 
I'm the wrong person to answer your questions! :) I do however have a 
command line application running on Windows XP that successfully uses 
MySQL on a Linux server, never had any trouble with that...


Greetings,
Krister Karlström, Helsinki, Finland

Molteni Davide wrote:


Hi,

thanks for your quick answer,

but in the script I don't use sessions variables to store host, user and
password, I wrote something like that:
$sc=mssql_connect(hostname,1433,userid,password);

Can you please tell me where I can find information about how to design
scripts for either the environment?

Thanks a lot.

Davide

2008/4/2, Krister Karlström [EMAIL PROTECTED]:

Hi!

That description did not help us very much... I think you're just
connecting using the wrong credentials, or using no at all. From where do
you grab the hostname, username and password? Maybe it's stored in your
session or something, and that session is not working in your command line
environment..? It could be just about anything that's your problem! :)

My advice is to take a look att the parameters using to connect to the
server, maybe echo them to stdout before doing your connect... It might be
that they are not defined when running your script on command-line.

In general, very few script are written in such a way that they work the
same way in a webserver environment and on command line. You should design
your scripts for either the environment.

Greetings,
Krister Karlström, Helsinki, Finland

Molteni Davide wrote:

 Hi,

I have a problem with all my php scripts that use MS SQL Server, but
only
when I start them from the command line
I'm using Apache 2.2.4 with PHP 5.2.5 on a server Win2k3 with SP2, and
MS
SQL Server 2000 on another Win2k3 server.
If I run the same script using a webbrower as an internet page it works
fine,
but if I run it from the command line (c:\php scipt.php) I will receive
messages like this:
Warning: mssql_connect(): Unable to connect to server: servername,1433
in
c:\script.php on line #
Can someone have an idea what could be the problem?
Can someone please help me?
What should I do to make it working?
Tanks a lot in advance.

Davide







--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Re: Help with JOIN query

2008-03-07 Thread Krister Karlström

Hi!

You can't use the MAX() function if you're not using a GROUP BY clause. 
The MAX() function can only grab the maximum value of a grouped column, 
as with MIN(), COUNT(), AVG() etc..


Greetings,
Krister Karlström, Helsinki

Jonathan Crawford wrote:

I think this is what you mean. You just want the timestamp and action from B in addition to something from A (I guessed product_ref), right?  The MAX() function should take care of getting the latest timestamp. 


explicit join:

SELECT MAX(TableB.timestamp), TableB.action, TableA.product_ref FROM TableB 
JOIN TableA ON TableA.record_id = TableB.record_id

ORDER BY TableB.action

or if you want to join your tables implicitly in your WHERE clause, similar to 
what you had before, implicit join:

SELECT MAX(TableB.timestamp), TableB.action, TableA.product_ref FROM TableB 
WHERE TableA.record_id = TableB.record_id

ORDER BY TableB.action

The problem with the implicit joins versus explicit joins is that you can't 
ever do OUTER JOINs, where you want many from one table and one (or many) from 
another table. For example if you want all sales reps and their sales, even if 
they don't have any. Implicit (or explicit INNER) JOINs will not show you all 
of the data.

Jonathan Crawford
[EMAIL PROTECTED]



 Original message 

Date: 6 Mar 2008 18:46:18 -
From: [EMAIL PROTECTED]  
Subject: php-db Digest 6 Mar 2008 18:46:18 - Issue 3990  
To: php-db@lists.php.net



php-db Digest 6 Mar 2008 18:46:18 - Issue 3990

Topics (messages 44700 through 44700):

Help with JOIN query
44700 by: Graham Cossey

Administrivia:

To subscribe to the digest, e-mail:
[EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]

To post to the list, e-mail:
php-db@lists.php.net


--

Date: Thu, 6 Mar 2008 18:46:14 +
From: Graham Cossey [EMAIL PROTECTED]  
Subject: Help with JOIN query  
To: php-db@lists.php.net


I can't see how to accomplish what I need so if anyone has any
suggestions they would be gratefully received...

I'm using mysql 4.0.20 by the way.

I have two tables :

TableA
record_id
product_ref

TableB
timestamp
record_id
action

I want to create a SELECT that joins these 2 tables where the JOIN to
TableB only returns the most recent entry by timestamp.

At present (using PHP) I do a SELECT on TableA then for each record
returned I perform a 2nd SELECT something like :

SELECT timestamp, action FROM TableB WHERE record_id = '$record_id'
ORDER BY timestamp DESC LIMIT 1

I now want to do it with one query to enable sorting the results by
'action' from TableB.

Any suggestions?

Hopefully I've made sense, if not I'll happily try and explain further
on request.

--
Graham




--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Re: Help with JOIN query

2008-03-07 Thread Krister Karlström
Yes, I'm totally with you Graham and I have created your tables and 
added some test data (you saw it in a previous post).. But I can't seem 
to come up with any solution for you, at least with MySQL 4.0.x.


So if no one else has any brilliant solutions for this I think you'll 
need to stick with your solution to make the request in two steps, using 
PHP and an array to sort and put everyting together. Now, hopefully, you 
don't have hundreds of kilos of rows in your tables.. :)


There might be solutions for this problem with never versions of MySQL, 
like using subqueries combined with joins like someone mentioned, but 
I'm not sure. I however, have never tested (or even needed to test) 
something like that, so I can't help you with that.


If it is not important to keep the history of actions for each product 
you could simply update TableB, but I'll guess that's not the case - 
otherwise you wouldn't probably been asking us.. :)


Another solution would be to move the previous action to an other table, 
like TableC for instance... Then TableC would be your history, TableB 
would only have your latest action.. But then again, we would have a 
one-to-one relation which basically means that you don't need two tables 
anymore. But you could at least consider this, if you are able to alter 
the database design a bit.


Greetings,
Krister Karlström, Helsinki

Graham Cossey wrote:


Thank you for your input Jonathan but it's not quite what I need. I
need the latest action from TableB (if it exists) determined by the
record_id matching TableA and where there are more than one matching
record in TableB select the one with the latest timestamp.

As an over-simplified example of what I'm trying to achieve :

TableA
record_id   product_ref
1 product1
2 product2

TableB
timestamp  record_id  action
20080301 1start
20080302 1middle
20080301 2start
20080302 2middle
20080303 2end

What I need returned is :

1,product1,middle
2,product2,end

---
Graham



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Help with JOIN query

2008-03-06 Thread Krister Karlström

Hi!

Graham Cossey wrote:


TableA
record_id
product_ref

TableB
timestamp
record_id
action

I want to create a SELECT that joins these 2 tables where the JOIN to
TableB only returns the most recent entry by timestamp.


For instance, to select all columns:

select * from TableA
join TableB on TableA.record_id = TableB.record_id
order by timestamp desc
limit 1

So you just join everything, then order by time in descening order and 
then just returning the first record = the newest record in the 
database. If you don't want all columns, then simply replace the star 
with the names of the columns you want.


I hope that this is what you wanted the query to do.. :)

Cheers,
Krister Karlström, Helsinki, Finland

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Help with JOIN query

2008-03-06 Thread Krister Karlström

Hi!

Graham Cossey wrote:


I was hoping to avoid joining everything as there can be many entries
in TableB for each record in TableA.

Also wouldn't your query only return one record? I need to return all
records from TableA with the latest action from TableB as well.


Yes, sorry - I realised that after I sent away my first reply.. :) I 
have a colleague who always says that people don't READ their mail - 
the just LOOK at their mail.. It's so true! I missed some of your points.


But maybe you got some ideas from my other mail, which I seem to have 
posted only to you directly.. Oh well, this is a tricky one anyway...


/Krister Karlström

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Help with JOIN query

2008-03-06 Thread Krister Karlström

Hi again!

We're getting a bit of topic here, since this is pure SQL.. But anyway...

I've played around with this one a bit since it seemed quite 
interesting... The best I can do is to get the oldest action...


select TableA.record_id, product_ref, action, time_stamp from TableA 
join TableB on TableA.record_id = TableB.record_id group by record_id;


Here's the test data:

mysql select TableA.record_id, product_ref, action, time_stamp from 
TableA join TableB on TableA.record_id = TableB.record_id;

+---+-+++
| record_id | product_ref | action | time_stamp |
+---+-+++
| 1 | 100 | A  | 20080306220037 |
| 1 | 100 | C  | 20080306220041 |
| 1 | 100 | E  | 20080306220045 |
| 2 | 102 | A  | 20080306220052 |
| 3 | 110 | A  | 20080306220055 |
| 3 | 110 | E  | 20080306220058 |
| 4 | 120 | B  | 20080306220105 |
| 4 | 120 | C  | 20080306220109 |
+---+-+++

And with the query above we get the opposite of the desired behavior, 
the oldest action (if that's the order in the database):


mysql select TableA.record_id, product_ref, action, time_stamp from 
TableA join TableB on TableA.record_id = TableB.record_id

group by record_id;
+---+-+++
| record_id | product_ref | action | time_stamp |
+---+-+++
| 1 | 100 | A  | 20080306220037 |
| 2 | 102 | A  | 20080306220052 |
| 3 | 110 | A  | 20080306220055 |
| 4 | 120 | B  | 20080306220105 |
+---+-+++
4 rows in set (0.00 sec)

Now is the question: Does anyone know how to get the 'group by' clause 
to leave a specific row 'visible' at top? Like the last inserted or by 
the order of another column...


Since MySQL 4.1 there are also a GROUP_CONCAT() function that can 
concatenate multiple 'rows' to a string in a desired order, but it does 
not support the limit statement... so that won't help us much I think. 
We can get all the actions in a string with the newest first, but then 
some post-stripping of the data is needed.


It seems like you need to do this with two queries in PHP, if no one has 
an answer to the question stated above. You can always buffer your 
result in an array in PHP and do whatever sorting you want to before 
using your data...


With the MAX() function we can found out when the last action was made, 
but we get the wrong action with the correct time:


mysql select TableA.record_id, product_ref, action, max(time_stamp) 
from TableA join TableB on TableA.record_id = TableB.record_id

group by record_id;
+---+-++-+
| record_id | product_ref | action | max(time_stamp) |
+---+-++-+
| 1 | 100 | A  |  20080306220045 |
| 2 | 102 | A  |  20080306220052 |
| 3 | 110 | A  |  20080306220058 |
| 4 | 120 | B  |  20080306220109 |
+---+-++-+
4 rows in set (0.00 sec)

Hmm... Now I'm stuck! :)

Greetings,
Krister Karlström, Helsinki, Finland

Graham Cossey wrote:


I can't see how to accomplish what I need so if anyone has any
suggestions they would be gratefully received...

I'm using mysql 4.0.20 by the way.

I have two tables :

TableA
record_id
product_ref

TableB
timestamp
record_id
action

I want to create a SELECT that joins these 2 tables where the JOIN to
TableB only returns the most recent entry by timestamp.

At present (using PHP) I do a SELECT on TableA then for each record
returned I perform a 2nd SELECT something like :

SELECT timestamp, action FROM TableB WHERE record_id = '$record_id'
ORDER BY timestamp DESC LIMIT 1

I now want to do it with one query to enable sorting the results by
'action' from TableB.

Any suggestions?

Hopefully I've made sense, if not I'll happily try and explain further
on request.



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] Help with JOIN query

2008-03-06 Thread Krister Karlström

This will give you:

ERROR : Invalid use of group function

It seems like the use of an aggregate (or how is it spelled?) function 
is not allowed in a join statement...


/Krister

Jon L. wrote:


You can try adding a quick test to the ON statement...

SELECT * FROM TableA
INNER JOIN TableB
  ON TableA.record_id = TableB.record_id
AND TableB.timestamp = MAX(TableB.timestamp)


Now, I haven't tested it.
I can only say the theory of it is accurate.

- Jon L.

On Thu, Mar 6, 2008 at 12:46 PM, Graham Cossey [EMAIL PROTECTED]
wrote:


I can't see how to accomplish what I need so if anyone has any
suggestions they would be gratefully received...

I'm using mysql 4.0.20 by the way.

I have two tables :

TableA
record_id
product_ref

TableB
timestamp
record_id
action

I want to create a SELECT that joins these 2 tables where the JOIN to
TableB only returns the most recent entry by timestamp.

At present (using PHP) I do a SELECT on TableA then for each record
returned I perform a 2nd SELECT something like :

SELECT timestamp, action FROM TableB WHERE record_id = '$record_id'
ORDER BY timestamp DESC LIMIT 1

I now want to do it with one query to enable sorting the results by
'action' from TableB.

Any suggestions?

Hopefully I've made sense, if not I'll happily try and explain further
on request.

--
Graham

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DB] problem in recorset that seems temporary

2008-01-25 Thread Krister Karlström

Hi!

I did not quite get the point here, but maybe that's not needed to solve 
the problem... :-)


First of all, you seem to have enabled the register_global setting, 
since I can't see from where the parameter $id is posted. Use the $_GET 
or $_POST superglobal array instead. Take big care of input filtering 
also and do not put dirty data directly into your query.


Second, the function mysql_db_query() has been deprecated for a long 
time now, use mysql_select_db() and mysql_query() instead.


I think your problem is in your while-loop:

You loop over the result set $result and fetch the row as an object.
Then you execute a delete statement and at the same time overwriting 
your previous result set $result, thus you will break the loop with an 
error that the provided parameter is not a result set.


I also guess you have a typo in your last statement where you (probably) 
try to delete the gallery. I guess that you maybe want to use the 
variable $galerie_id..? Well, I can't tell without any more information 
about your application.


Greetings,
Krister Karlström, Helsinki, Finland

Ruprecht Helms wrote:


while($row = mysql_fetch_object($result))
{
$galerie_id=$row-ID;
$result=mysql_db_query(pferdeservice-karle,DELETE FROM
Galerie_kultur WHERE GalerieID=$id);
}
$result=mysql_db_query(pferdeservice-karle,DELETE FROM Galerie WHERE
ID=$id);
echo mysql_error();


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php