tags:

views:

27

answers:

3

Hello all,

I have the following piece of code, executing a pretty simple MySQL query:

$netnestquery = 'SELECT (`nested`+1) AS `nest` FROM `ipspace6` WHERE `id`<='.$adaddr.' AND `subnet`<='.$postmask.' AND `type`="net" AND `addr` NOT IN(SELECT `id` FROM `ipspace6` WHERE `addr`<'.$adaddr.' AND `type`="broadcast") ORDER BY `id`,`subnet` DESC LIMIT 1';

$netnestresults = mysql_query($netnestquery);
$netnestrow = mysql_fetch_array($netnestresults);
$nestlvl = $netnestrow['nest'];

echo '<br> NESTQ: '.$netnestquery;

Now, when I execute this in PHP, I get no results; an empty query. However, when I copy and paste the query echoed by my code (for debug purposes) into the mysql command line, I get a valid result:

mysql> SELECT (`nested` + 1) AS `nest` FROM `ipspace6` WHERE `id`<=50552019054038629283648959286463168512 AND `subnet`<=36 AND `type`='net' AND `addr` NOT IN (SELECT `id` FROM `ipspace6` WHERE `addr`<50552019054038629283648959286463168512 AND `type`='broadcast') ORDER BY `id`,`subnet` DESC LIMIT 1;
+------+
| nest |
+------+
|    1 | 
+------+
1 row in set (0.00 sec)

Can anybody tell me what I'm doing wrong? I can't put quotes around my variables, as then MySQL will try to evaluate the variable as a string, when it is, in fact, a very large decimal. I think I might just be making a stupid mistake somewhere, but I can't tell where.

A: 

Can you modify the line to say $netnestresults = mysql_query($netnestquery) or die(mysql_error());

It may be giving you an unknown error, such as a bad connection, missing DB, etc.

Jordan
Hi Jordan,Thanks for your response. I've tried that, and I get no errors whatsoever. However, when I do this:$netnestrow = mysql_fetch_array($netnestresults) or die(mysql_error());I get a blank page.
Andrew K
Are you able to select anything from the DB? Maybe could you do "Select nested as nest From ipspace6 Limit 0, 1" to see if anything comes back?
Jordan
Jordan,Yes that works fine, I get a valid result when I run that query.
Andrew K
For giggles, can you change the query to this:"SELECT (nested+1) AS nest FROM ipspace6 WHERE id <=$adaddr AND subnet<=$postmask AND type='net' AND addr NOT IN(SELECT id FROM ipspace6 WHERE addr<$adaddr AND type='broadcast') ORDER BY id, subnet DESC LIMIT 1";
Jordan
I tried your new query, and I still don't get any results from MySQL.
Andrew K
A: 

do an echo $netnestquery before calling mysql_query also add a die(mysql_error()) there.

A: 

WHERE `id`<=50552019054038629283648959286463168512

That's a pretty big number there.

PHP has issues with big numbers. The maximum size of an integer depends on how PHP was compiled, and if it's on a 64-bit system.

Have you checked that the variable containing that number hasn't been capped to a 32-bit or 64-bit integer? If it has been capped, you're going to need to take steps to make sure it's only being stored as a string in PHP. MySQL accepts strings that are entirely numeric as numbers without complaining.

(That being said, I'm not sure that MySQL can do anything with a number larger than 64-bits. The largest integer column is BIGINT, which is 64-bits. There's also NUMERIC, but it's treated as a floating point number, and that might not be what you want to do...)

Charles
Charles,I'm storing these numbers in a Decmial(39,0) UNSIGNED column. It seems to work just fine, but I ran into issues with MySQL trying to interpret these numbers as strings. See my other question (regarding exactly this) here: http://stackoverflow.com/questions/2993828/mysql-returns-wrong-values-on-select-statement
Andrew K