News:

Wondering if this will always be free?  See why free is better.

Main Menu

Need some help with a query

Started by cicka, December 17, 2010, 12:19:15 PM

Previous topic - Next topic

cicka

I have been trying to solve this for several days now and I tried everything but I just can not get it to work.

I am trying to make it at my forum that after you post 1 post a day you can send a personal message.

This is what I am doing. At the Subs-Post.php file after this code

// Send off a personal message.
function sendpm($recipients, $subject, $message, $store_outbox = false, $from = null, $pm_head = 0)
{
global $scripturl, $txt, $user_info, $language;
global $modSettings, $smcFunc;


I am adding this code

$request = $smcFunc['db_query']('', '
SELECT COUNT(id_msg) as number_posts
FROM {db_prefix}messages
WHERE id_member = {int:id_member}
AND poster_time > {int:poster_time}',
array(
'id_member' => $memID,
'poster_time' => time() - 86400
)
);

if($request['number_posts'] <= 1)
{
fatal_lang_error('dayily_posts_message');

}


But when I test it I get this errror when I try to send a pm

The database value you're trying to insert does not exist: id_member

I replaced $memID with $context['user_id'] and $user_info['user_id'] and I still get the same error.

But when I replaced $memID with $user_info['id'] and $context['id'], the error goes away but it will not work. I can not still send a pm even if I made 2 or more posts.

I have been wrapping my head over this for almost one week and I could not find the solution. Can someone please tell me what I am doing wrong?

Thanks much :)

Marcus Forsberg

You can't use $request directly, methinks. You need to use $smcFunc['db_fetch_row'] first, like so:


$row = $smcFunc['db_fetch_row']($request);


now $row will hold the value:


if($row['number_posts'] <= 1)
{
fatal_lang_error('dayily_posts_message');
}


:)

kat

cσσкιє мσηѕтєя is my hero. :)

Marcus Forsberg

Quote from: K@ on December 17, 2010, 02:16:22 PM
cσσкιє мσηѕтєя is my hero. :)

Tell me you typed my name without copy-pasting it and you'll be my hero too :P

cicka

Thanks much for your answercσσкιє мσηѕтєя but it did not work. I got still the same error message.


This is my whole code I used

$request = $smcFunc['db_query']('', '
SELECT COUNT(id_msg) as number_posts
FROM {db_prefix}messages
WHERE id_member = {int:id_member}
AND poster_time > {int:poster_time}',
array(
'id_member' => $memID,
'poster_time' => time() - 86400
)
);

$row = $smcFunc['db_fetch_row']($request);

if($row['number_posts'] <= 1)
{
fatal_lang_error('cant_access_upload_path');
}

kat

Quote from: cσσкιє мσηѕтєя on December 17, 2010, 02:17:20 PM
Tell me you typed my name without copy-pasting it and you'll be my hero too :P

Why make life difficult? ;)

Marcus Forsberg

Oops. :P

I trust there's nothing in the error log?

cicka

Quote from: cσσкιє мσηѕтєя on December 17, 2010, 02:27:14 PM
Oops. :P

I trust there's nothing in the error log?

Yes, the error log does not have errors at all.

Marcus Forsberg

Sorry, my bad. I think it should be this:

$row = $smcFunc['db_fetch_assoc']($request);

instead of

$row = $smcFunc['db_fetch_row']($request);

I tried that on my local and it seems to work as it should.

DoctorMalboro

I think (forgive me if i'm wrong) you'd miss this... $smcFunc['db_free_result']($select);

Marcus Forsberg

Aye, that should be added right below everything, however the query wouldn't fail without it.

snork13

a little sloppy, i'm at work...try this and then clean it up  ;)
$request = $smcFunc['db_query']('', '
SELECT COUNT(id_member)as number_posts
FROM {db_prefix}messages
WHERE id_member = "'.$user_info['id'].'"
AND poster_time >= {int:poster_time}',
array(
//'id_member' => $user_info['id'],
'poster_time' => time() - 86400
)
);
$row = $smcFunc['db_fetch_assoc']($request);
if($row['number_posts'] <= 4)
//if($request['number_posts'] >= 20)
{
//fatal_lang_error('dayily_posts_message');
//WHERE id_member = "'.$user_info['id'].'"
fatal_lang_error('no_access', false);

}
$smcFunc['db_free_result']($select);




Mods
Please DO NOT PM me with requests for individual support, but post your questions and concerns in the appropriate section of the forum where other users can benefit from them as well. Thank you.
I have been super busy as of late. Working on updates to all my modifications for 2.0.1

cicka

Quote from: cσσкιє мσηѕтєя on December 17, 2010, 02:35:53 PM
Sorry, my bad. I think it should be this:

$row = $smcFunc['db_fetch_assoc']($request);

instead of

$row = $smcFunc['db_fetch_row']($request);

I tried that on my local and it seems to work as it should.

That worked perfectly. Thank you for all your help.  :)


Quote from: DoctorMalboro on December 17, 2010, 02:37:34 PM
I think (forgive me if i'm wrong) you'd miss this... $smcFunc['db_free_result']($select);

I tried the code without this and it worked. Should I still add it below every other code or it will not be necessary?

Quote from: snork13 on December 17, 2010, 03:00:24 PM
a little sloppy, i'm at work...try this and then clean it up  ;)
$request = $smcFunc['db_query']('', '
SELECT COUNT(id_member)as number_posts
FROM {db_prefix}messages
WHERE id_member = "'.$user_info['id'].'"
AND poster_time >= {int:poster_time}',
array(
//'id_member' => $user_info['id'],
'poster_time' => time() - 86400
)
);
$row = $smcFunc['db_fetch_assoc']($request);
if($row['number_posts'] <= 4)
//if($request['number_posts'] >= 20)
{
//fatal_lang_error('dayily_posts_message');
//WHERE id_member = "'.$user_info['id'].'"
fatal_lang_error('no_access', false);

}
$smcFunc['db_free_result']($select);






Thank you for this solution too. I will also study it.


Thank you all guys. The support here is fantastic and it feels good to be part of such a wonderful community :)

Marcus Forsberg

Quote from: cicka on December 17, 2010, 03:13:28 PM
That worked perfectly. Thank you for all your help.  :)

No problem :)

Quote from: cicka on December 17, 2010, 03:13:28 PM
I tried the code without this and it worked. Should I still add it below every other code or it will not be necessary?

It may be added below this:

if($row['number_posts'] <= 1)
{
fatal_lang_error('cant_access_upload_path');
}


Like so:

if($row['number_posts'] <= 1)
{
fatal_lang_error('cant_access_upload_path');
}
$smcFunc['db_free_result']($request);


What it does is it frees all memory associated with $request.

cicka

Thank you once again for the explanation :) Not it is clear to me.

IceXaos

Quote from: cσσкιє мσηѕтєя on December 17, 2010, 03:16:20 PM
What it does is it frees all memory associated with $request.
May I ask what the difference is between: mysql_free_result($request); and $smcFunc['db_free_result']($request);

Marcus Forsberg

$smcFunc is a wrapper function used in SMF 2.0 to support not only MySQL but SQLite and PostgreSQL as well. Using mysql_free_result will not work for all three of them. But it does the same.

IceXaos

Quote from: cσσкιє мσηѕтєя on December 17, 2010, 03:33:54 PM
$smcFunc is a wrapper function used in SMF 2.0 to support not only MySQL but SQLite and PostgreSQL as well. Using mysql_free_result will not work for all three of them. But it does the same.
Gotcha, thanks.

Advertisement: