Jump to content

[SOLVED] Why is the query ending up blank?


borabora12

Recommended Posts

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

 

$queryconpos  = mysql_query("SELECT @count := 0; SELECT @count := IF(dailydiff < 0, @count + 1, 0) as cntpos FROM the_table ORDER BY cntpos DESC LIMIT 1");
$resultconpos =  mysql_fetch_array($queryconpos, MYSQL_BOTH);
$conupdays = $resultconpos['cntpos'];
echo "&conupdays=$conupdays";

 

 

what am I doing wrong here that I am getting an error?

 

I tried inputing the following code in SQL sytex and it works:

 

SELECT @count := 0; SELECT @count := IF(dailydiff < 0, @count + 1, 0) as cntpos FROM the_table ORDER BY cntpos DESC LIMIT 1

 

I get an output of

 

cntpos

4

 

Link to comment
Share on other sites

The select @count was just so I could rerun the query.

 

 

It's causing an error because the mysql_query function only supports 1 query per call (security reasons).

 

 

The @count := 0 is also necessary so it's not null.  (NULL + x = NULL)

 

 

So, you have two options:

 

mysql_query("@count := 0;");

mysql_query(other query);

 

 

Or:

 

SELECT @count := IF(c < 0, IF(@count IS NULL, 1, @count + 1), 0) as cntpos FROM consec ORDER BY cntpos DESC LIMIT 1;

 

 

(Or a CASE statement could be used.  I would probably go with the first one, since the second one would add an extra IF call every row.

Link to comment
Share on other sites

hey corbin  ;D

 

so in other words it would be something like this:

 

$query1 = mysql_query("SELECT @count := 0");
$query2 = mysql_query("SELECT @count := IF(dailydiff < 0, @count + 1, 0) as cntpos FROM the_table ORDER BY cntpos DESC LIMIT 1");

$result = mysql_fetch_array($query1, $query2);

echo "$result['cntpos']"

thanks again for all the help!

 

 

Link to comment
Share on other sites

Hey corbin I got it to work! Thanks for all the help man!

 

Here is the solution just incase

$query = mysql_query("SELECT @count := 0");
$query = mysql_query("SELECT @count := IF(dailydiff < 0, @count + 1, 0) as cntpos FROM the_table ORDER BY cntpos DESC LIMIT 1");

$result = mysql_fetch_array($query);

echo "$result['cntpos']"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.