Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: cicka on December 17, 2010, 12:19:15 PM

Title: Need some help with a query
Post by: cicka on December 17, 2010, 12:19:15 PM
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 :)
Title: Re: Need some help with a query
Post by: Marcus Forsberg on December 17, 2010, 02:09:59 PM
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');
}


:)
Title: Re: Need some help with a query
Post by: kat on December 17, 2010, 02:16:22 PM
cσσкιє мσηѕтєя is my hero. :)
Title: Re: Need some help with a query
Post by: Marcus Forsberg on December 17, 2010, 02:17:20 PM
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
Title: Re: Need some help with a query
Post by: cicka on December 17, 2010, 02:24:26 PM
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');
}
Title: Re: Need some help with a query
Post by: kat on December 17, 2010, 02:25:15 PM
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? ;)
Title: Re: Need some help with a query
Post by: Marcus Forsberg on December 17, 2010, 02:27:14 PM
Oops. :P

I trust there's nothing in the error log?
Title: Re: Need some help with a query
Post by: cicka on December 17, 2010, 02:32:33 PM
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.
Title: Re: Need some help with a query
Post by: Marcus Forsberg 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.
Title: Re: Need some help with a query
Post by: 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);
Title: Re: Need some help with a query
Post by: Marcus Forsberg on December 17, 2010, 02:39:17 PM
Aye, that should be added right below everything, however the query wouldn't fail without it.
Title: Re: Need some help with a query
Post by: 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);




Title: Re: Need some help with a query
Post by: cicka on December 17, 2010, 03:13:28 PM
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 :)
Title: Re: Need some help with a query
Post by: Marcus Forsberg on December 17, 2010, 03:16:20 PM
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.
Title: Re: Need some help with a query
Post by: cicka on December 17, 2010, 03:20:00 PM
Thank you once again for the explanation :) Not it is clear to me.
Title: Re: Need some help with a query
Post by: IceXaos on December 17, 2010, 03:24:18 PM
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);
Title: Re: Need some help with a query
Post by: Marcus Forsberg 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.
Title: Re: Need some help with a query
Post by: IceXaos on December 17, 2010, 03:42:27 PM
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.