Forum Moderators: coopster

Message Too Old, No Replies

'not a valid MySQL result'

Query plus counting

         

StupidScript

1:00 am on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Re: My database thread found here [webmasterworld.com]:

Executing the MySQL query below into outfile from a shell works great, and returns (for example):

Query OK, 430 rows affected (1 min 10.83 sec)

So I figured I could write a PHP script to avoid having to type the query into the shell, each time:

$dbconn=mysql_connect("localhost","uid","pwd");

mysql_select_db("database",$dbconn);

$result=mysql_query("select tracking_code,count(tracking_code) into outfile 'result.txt'

 from records group by ip_address",$dbconn);

And to get the same "affected rows" count offered by the shell execution, I added:

$count=mysql_result($result,0,0);

I also tried

mysql_affected_rows()
,
mysql_num_rows()
and some other, user-submitted things found on the PHP Manual pages.

In each case, the execution of the PHP script resulted in the outfile holding the right data, but I keep getting

... supplied argument is not a valid MySQL result resource ...
for any of the counting functions I try.

I'm running a recent XAMPP installation on Win98SE, if that makes any difference.

Any help is truly appreciated.

jatar_k

1:56 am on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



strange that mysql_num_rows didn't work, did you use it before or after doing whatever else you needed with the array?

StupidScript

6:20 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The script is as above, in the same order as above:

$dbconn ...

mysql_select_db ...

$result ...

$count ...

echo "$count Rows\n";

That's the whole thing ... well, I also include a

where
clause to select records from particular date ranges. That's the part I don't want to have to type into the shell, each time. But other than the additional
where timestamp > 109924980 && timestamp < 109938245
, it's the same. If I can get it counting, I'll use an array of timestamp data and loop through, but I'm not there, yet.

What's particularly odd, to me, is that the shell execution reported 'affected rows', yet

mysql_affected_rows
returns the error (I know it shouldn't be appropriate, anyway) as does
mysql_num_rows
(which is for SELECT statements like this one).

Maybe the outfile instruction is appropriating the buffer used by

mysql_num_rows
et al. in a way such that it's not available to the subsequent functions?

[edited by: StupidScript at 6:37 pm (utc) on Jan. 12, 2006]

jatar_k

6:29 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, you know I looked at it a bunch of times and I missed the outfile, I just assumed you were reading it into php and then writing to file. Yes that is why.

If you really wanted to get the number of rows you could repeat the query just as a regular select and then use mysql_num_rows

<added>

$result=mysql_query("select tracking_code,count(tracking_code) into outfile 'result.txt' ...

I bet that in this case it won't return a resource at all, the value of $result is probably null, 1 or 0 as there is no result, the query finishes all by itself.

StupidScript

6:45 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Indeed,
$result = 1
and the query without the outfile:
$resultB =
the count I'm looking for (using
mysql_num_rows
, as I should be.)

Thanks a lot for the brain time, jatar_k!

I'm off to try and make this whole thing efficient, now ...