Upgrade database problems db_query()

Started by Coon_Dog, January 21, 2024, 12:56:41 AM

Previous topic - Next topic

Coon_Dog

Upgraded my forum from 1.1.21 to 2.0.19.  Trying to convert db_query() to new format $smcFunc['db_query'].  Took me awhile to narrow it down to this.  It looks so easy, but not the first time.  Gonna keep working on it, but any help would be appreciated.  Maybe convert these two and I will able to figure the rest out.  Thanks!

From shoutbox.php
   $shouts = db_query("
         SELECT s.userid, s.shoutDate, s.pageText, mem.realName
         FROM {$db_prefix}shout AS s
            LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
         ORDER BY id DESC
         LIMIT 10", __FILE__, __LINE__);

From shout.php
   db_query("
      INSERT INTO {$db_prefix}shout
         (userid, shoutDate, pageText)
      VALUES ($ID_MEMBER, '" . time() . "', '$_REQUEST[txtShout]')", __FILE__, __LINE__);

shoutbox.php (read from database)
<?php
echo '
<table cellspacing="0" cellpadding="0" border="0" align="center" width="100%" style="position: relative;">
<tr>
<td width="100%" style="padding-left: 5px;" valign="top" align="center">
<div class="headertitles" style="width: 95%;"><img src="'
, $settings['images_url'], '/blank.gif" height="12" alt="" /></div>
<div class="headerbodies" style="width: 95%; position: relative; margin-right: 5px; background-image: url('
, $settings['images_url'], '/box_bg.gif);">
<img src="'
, $settings['images_url'], '/' , $context['user']['language'] , '/shoutbox.gif" style="position: absolute; left: ', $context['browser']['is_ie5'] || $context['browser']['is_ie4'] ? '0' : '-1px', '; top: -16px; clear: both;" alt="" />
<div class="normaltext" align="left" style="width: 99%; height: 135px; position: relative; margin-top: 5px;">'
, shoutbox(), '</div>
</div>
</td>
</tr>
</table>'
;


function
shoutbox()
{
include('Settings.php');
include_once($sourcedir . '/Subs.php');
global $user_info;

$shouts = db_query("
SELECT s.userid, s.shoutDate, s.pageText, mem.realName
FROM
{$db_prefix}shout AS s
LEFT JOIN
{$db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
ORDER BY id DESC
LIMIT 10"
, __FILE__, __LINE__);

if(!$user_info['is_guest'])
{
echo '
<form name="form1" method="post" action="'
, $boardurl, '/index.php?action=shout">
<input type="text" name="txtShout" style="width: 80%;"><input type="submit" name="Submit" value="Shout">
</form>'
;
}
echo '
<div id="Layer1" style="position:relative; width:100%; height:95px; z-index:1; overflow: auto;" align="left">'
;

while($row = mysql_fetch_array($shouts))
{
echo '[', timeformat($row["shoutDate"]), '] ', $row['realName'], ': ', doUBBC($row["pageText"], true), '<br/>';
}
mysql_free_result($shouts);

echo '
</div>'
;
}

?>


shout.php (write to database)
<?php
/******************************************************************************
* shout.php                                                                   *
*******************************************************************************
* SMF: Simple Machines Forum                                                  *
* Open-Source Project Inspired by Zef Hemel ([email protected])                *
* =========================================================================== *
* Shoutbox version 1b, by Dave Hurt                                           *
******************************************************************************/
if (!defined('SMF'))
die('Hacking attempt...');

// Post to the shoutbox!
function Shoutbox()
{
global $user_info, $sourcedir, $ID_MEMBER, $db_prefix;

// If they didn't send the text to shout or are a guest... well, what good are they :P?!
if (!isset($_REQUEST['txtShout']) || $user_info['is_guest'])
redirectexit();

// Now, load Subs-Post.php and preparse the code... this just makes sure they aren't hax0ring or anything.
require_once($sourcedir . '/Subs-Post.php');
preparsecode($_REQUEST['txtShout']);

// Insert time!
db_query("
INSERT INTO
{$db_prefix}shout
(userid, shoutDate, pageText)
VALUES (
$ID_MEMBER, '" . time() . "', '$_REQUEST[txtShout]')", __FILE__, __LINE__);

//update shout counter in user profile...
$result = db_query("
SELECT shouts
FROM
{$db_prefix}members
WHERE ID_MEMBER=
$ID_MEMBER", __FILE__, __LINE__);

$row = mysql_fetch_array($result);
$shouts = $row['shouts']+1;

db_query("
UPDATE
{$db_prefix}members
SET shouts =
$shouts
WHERE ID_MEMBER=
$ID_MEMBER", __FILE__, __LINE__);

mysql_free_result($result);

// Now, redirect back to the main forum page.
redirectexit();
}

?>

Doug Heffernan

Quote from: Coon_Dog on January 21, 2024, 12:56:41 AMUpgraded my forum from 1.1.21 to 2.0.19.

May I ask why didn't you upgrade to the latest current version, 2.1.4?

Quote from: Coon_Dog on January 21, 2024, 12:56:41 AMTrying to convert db_query() to new format $smcFunc['db_query'].  Took me awhile to narrow it down to this

Have a look at this article.

https://wiki.simplemachines.org/smf/Mod_authors:_Changes_in_SMF_2.0

Arantor

Code (before) Select
   $shouts = db_query("
         SELECT s.userid, s.shoutDate, s.pageText, mem.realName
         FROM {$db_prefix}shout AS s
            LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
         ORDER BY id DESC
         LIMIT 10", __FILE__, __LINE__);

Code (after) Select
   $shouts = $smcFunc['db_query']('
         SELECT s.userid, s.shoutDate, s.pageText, mem.realName
         FROM {db_prefix}shout AS s
            LEFT JOIN {db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
         ORDER BY id DESC
         LIMIT 10');


Code (before) Select
   db_query("
      INSERT INTO {$db_prefix}shout
         (userid, shoutDate, pageText)
      VALUES ($ID_MEMBER, '" . time() . "', '$_REQUEST[txtShout]')", __FILE__, __LINE__);

Code (after) Select
$smcFunc['db_insert']('insert',
    '{db_prefix}shout',
    ['user_id' => 'int', 'shoutDate' => 'int', 'pageText' => 'string'],
    [$context['user']['id'], time(), $_REQUEST['txtShout']],
    []
);

NB one presumes $_REQUEST['txtShout'] has been through preparse_code() ready to be given to parse_bbc, anything else is possibly a security hole.

I *think* that's right, it's been a while since I did strictly 2.0 changes.
Holder of controversial views, all of which my own.


Coon_Dog

Quote from: Doug Heffernan on January 21, 2024, 06:11:53 AM
Quote from: Coon_Dog on January 21, 2024, 12:56:41 AMUpgraded my forum from 1.1.21 to 2.0.19.

May I ask why didn't you upgrade to the latest current version, 2.1.4?

Quote from: Coon_Dog on January 21, 2024, 12:56:41 AMTrying to convert db_query() to new format $smcFunc['db_query'].  Took me awhile to narrow it down to this

Have a look at this article.

https://wiki.simplemachines.org/smf/Mod_authors:_Changes_in_SMF_2.0


My theme Helios by Bloc does not work with SMF 2.1.  2.0.19 gets me to PHP 8 to make my host happy for now.  Figure in couple years there will be more themes and mods for 2.1.  Don't get a lot of traffic anymore.

Thanks for the link it will help.  I see a couple more things like Column Names and $ID_MEMBER.  I am more confused now trying to dissect that first example.  Been searching learning little by little, but still not there.

Thanks!

Coon_Dog

Quote from: Arantor on January 21, 2024, 06:41:53 AM
Code (before) Select
   $shouts = db_query("
         SELECT s.userid, s.shoutDate, s.pageText, mem.realName
         FROM {$db_prefix}shout AS s
            LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
         ORDER BY id DESC
         LIMIT 10", __FILE__, __LINE__);

Code (after) Select
   $shouts = $smcFunc['db_query']('
         SELECT s.userid, s.shoutDate, s.pageText, mem.realName
         FROM {db_prefix}shout AS s
            LEFT JOIN {db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
         ORDER BY id DESC
         LIMIT 10');


Code (before) Select
   db_query("
      INSERT INTO {$db_prefix}shout
         (userid, shoutDate, pageText)
      VALUES ($ID_MEMBER, '" . time() . "', '$_REQUEST[txtShout]')", __FILE__, __LINE__);

Code (after) Select
$smcFunc['db_insert']('insert',
    '{db_prefix}shout',
    ['user_id' => 'int', 'shoutDate' => 'int', 'pageText' => 'string'],
    [$context['user']['id'], time(), $_REQUEST['txtShout']],
    []
);

NB one presumes $_REQUEST['txtShout'] has been through preparse_code() ready to be given to parse_bbc, anything else is possibly a security hole.

I *think* that's right, it's been a while since I did strictly 2.0 changes.


Now im getting "Value of type null is not callable"  After didnt work I changed id_member to lower case.  Shout names remain the same.  Didnt seem to matter.  Still "Value of type null is not callable"

Doug Heffernan

Quote from: Coon_Dog on January 21, 2024, 07:17:52 AMNow im getting "Value of type null is not callable"  After didnt work I changed id_member to lower case.  Shout names remain the same.  Didnt seem to matter.  Still "Value of type null is not callable"

You should adapt the columns names of smf 1.1. to their updated names used in smf 2.0.

Quote from: Arantor on January 21, 2024, 06:41:53 AM$smcFunc['db_insert']('insert',
    '{db_prefix}shout',
    ['user_id' => 'int', 'shoutDate' => 'int', 'pageText' => 'string'],
    [$context['user']['id'], time(), $_REQUEST['txtShout']],
    []
);


The userid in the shoutbox table appears to be without the underscore. (userid)

Quote from: Arantor on January 21, 2024, 06:41:53 AM$shouts = $smcFunc['db_query']('
         SELECT s.userid, s.shoutDate, s.pageText, mem.realName
         FROM {db_prefix}shout AS s
            LEFT JOIN {db_prefix}members AS mem ON (mem.ID_MEMBER = s.userid)
         ORDER BY id DESC
         LIMIT 10');

Also, shouldn't realName be real_name and ID_MEMBER be id_member?

Coon_Dog

#6
I changed ID_MEMBER to id_member, and yes you are correct about realName to real_name.  Still same error "Value of type null is not callable"

I ran code in 2nd box to change and it, didn't do anything that I can see.  (im a idiot.  This is the write code that Arantor posted..  It also errored out with "Value of type null is not callable"  I can see why.  LOL)  Do these column names matter in shout, as long as they match?

No line numbers, but I put Test Text above and below and pod missing bottom.  It is in this database inquiry. 

Will keep trying.  Might have to sleep on it.

This is the code not working now.
   $shouts = $smcFunc['db_query']('
         SELECT s.userid, s.shoutDate, s.pageText, mem.real_name
         FROM {db_prefix}shout AS s
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = s.userid)
         ORDER BY id DESC
         LIMIT 10');





Doug Heffernan

What shoutbox mod are you using? Did you download it from here or is it custom made? If the former, have you checked the mod page if it has been updated to work with Smf 2.0.x?

Coon_Dog

Been using since 1.0 about 20 years.  Its not in SMF downloads have to install manually.  No idea where I got it.  LOL  Not much code to it.  Thought I could figure out.

Changed all the column names and updated names in shout files.  All in sync with 2.0.  Still no luck. 



  $shouts = $smcFunc['db_query']('
        SELECT s.user_id, s.shout_date, s.page_text, mem.real_name
        FROM {db_prefix}shout AS s
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = s.user_id)
        ORDER BY id DESC
        LIMIT 10');

Arantor

Did you add $smcFunc to the list of global variables? It is a variable, it needs to be imported as such.
Holder of controversial views, all of which my own.


Coon_Dog

I think we are getting somewhere!  Globals was a piece of it.  Will dig into this error getting close!  Thanks!

Getting three errors before adding globals in error log.
Value of type null is not callable

2: Trying to access array offset on value of type null
File: /homepages/39/d269219047/htdocs/smf3/Themes/default/shoutbox.php
Line: 22
Note: line 22 is $shouts = $smcFunc['db_query']('

2: Undefined variable $smcFunc
File: /homepages/39/d269219047/htdocs/smf3/Themes/default/shoutbox.php
Line: 22
Note: line 22 is $shouts = $smcFunc['db_query']('



After adding globals getting one error. (global $user_info, $smcFunc;)

Too few arguments to function SMF_DB_MySQLi::query(), 1 passed in /homepages/39/d269219047/htdocs/smf3/Themes/default/shoutbox.php on line 22 and at least 2 expected
Note: line 22 is $shouts = $smcFunc['db_query']('

vbgamer45

Possible to post as an attachment the whole shoutbox.php file?
Community Suite for SMF - Grow your forum with SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com - Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Arantor

OK so the 1 passed error is on me.

  $shouts = $smcFunc['db_query']('', '
        SELECT s.user_id, s.shout_date, s.page_text, mem.real_name
        FROM {db_prefix}shout AS s
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = s.user_id)
        ORDER BY id DESC
        LIMIT 10');
Holder of controversial views, all of which my own.


Coon_Dog

TThanks Arantor & Doug Heffernan for all the help!  Gonna take a minute and see what I can do.  Will let you know how it goes.

Quote from: vbgamer45 on January 21, 2024, 11:47:12 AMPossible to post as an attachment the whole shoutbox.php file?

shoutbox.php (Think I have this file figured out.  Other than doUBBC(censorText and shouldnt be a problem.  Deleted and it ran through.)
<?php
echo '
<table cellspacing="0" cellpadding="0" border="0" align="center" width="100%" style="position: relative;">
<tr>
<td width="100%" style="padding-left: 5px;" valign="top" align="center">
<div class="headertitles" style="width: 95%;"><img src="'
, $settings['images_url'], '/blank.gif" height="12" alt="" /></div>
<div class="headerbodies" style="width: 95%; position: relative; margin-right: 5px; background-image: url('
, $settings['images_url'], '/box_bg.gif);">
<img src="'
, $settings['images_url'], '/' , $context['user']['language'] , '/shoutbox.gif" style="position: absolute; left: ', $context['browser']['is_ie5'] || $context['browser']['is_ie4'] ? '0' : '-1px', '; top: -16px; clear: both;" alt="" />
<div class="normaltext" align="left" style="width: 99%; height: 135px; position: relative; margin-top: 5px;">'
, shoutbox(), '</div>
</div>
</td>
</tr>
</table>'
;


function
shoutbox()
{
include('Settings.php');
include_once($sourcedir . '/Subs.php');
global $user_info, $smcFunc;

 
$shouts = $smcFunc['db_query']('', '
       SELECT s.user_id, s.shout_date, s.page_text, mem.real_name
       FROM {db_prefix}shout AS s
           LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = s.user_id)
       ORDER BY id DESC
       LIMIT 10'
);

if(!$user_info['is_guest'])
{
echo '
<form name="form1" method="post" action="'
, $boardurl, '/index.php?action=shout">
<input type="text" name="txtShout" style="width: 80%;"><input type="submit" name="Submit" value="Shout">
</form>'
;
}
echo '
<div id="Layer1" style="position:relative; width:100%; height:95px; z-index:1; overflow: auto;" align="left">'
;

while($row = mysql_fetch_array($shouts))
{
echo '[', timeformat($row["shout_date"]), '] ', $row['real_name'], ': ', doUBBC(censorText($row["page_text"]true), '<br/>';
}
mysql_free_result($shouts);

echo '
</div>'
;
}

?>

shout.php (Now to work on writing to database.  See how it goes!)
<?php
/******************************************************************************
* shout.php                                                                   *
*******************************************************************************
* SMF: Simple Machines Forum                                                  *
* Open-Source Project Inspired by Zef Hemel ([email protected])                *
* =========================================================================== *
* Shoutbox version 1b, by Dave Hurt                                           *
******************************************************************************/
if (!defined('SMF'))
die('Hacking attempt...');

// Post to the shoutbox!
function Shoutbox()
{
global $user_info, $sourcedir, $ID_MEMBER, $db_prefix;

// If they didn't send the text to shout or are a guest... well, what good are they :P?!
if (!isset($_REQUEST['txtShout']) || $user_info['is_guest'])
redirectexit();

// Now, load Subs-Post.php and preparse the code... this just makes sure they aren't hax0ring or anything.
require_once($sourcedir . '/Subs-Post.php');
preparsecode($_REQUEST['txtShout']);

// Insert time!
db_query("
INSERT INTO
{$db_prefix}shout
(userid, shoutDate, pageText)
VALUES (
$ID_MEMBER, '" . time() . "', '$_REQUEST[txtShout]')", __FILE__, __LINE__);

//update shout counter in user profile...
$result = db_query("
SELECT shouts
FROM
{$db_prefix}members
WHERE ID_MEMBER=
$ID_MEMBER", __FILE__, __LINE__);

$row = mysql_fetch_array($result);
$shouts = $row['shouts']+1;

db_query("
UPDATE
{$db_prefix}members
SET shouts =
$shouts
WHERE ID_MEMBER=
$ID_MEMBER", __FILE__, __LINE__);

mysql_free_result($result);

// Now, redirect back to the main forum page.
redirectexit();
}

?>

Arantor

First up, stop using $ID_MEMBER, it doesn't exist in SMF 2.0 - either $user_info['id'] or $context['user']['id'] will work (latter needs context in globals)

INSERT is much the same as I outlined before - use $smcFunc['db_insert'] not $smcFunc['db_query'].

You can also skip the super inefficient SELECT-to-UPDATE and just do it in a single update:

$smcFunc['db_query']('', '
  UPDATE {db_prefix}members
  SET shouts = shouts + 1
  WHERE id_member = {int:id_member}',
  [
    'id_member' => $user_info['id'],
  ]
);

You don't need to do the select to get the shout count, or the fetch array or the free result for this.
Holder of controversial views, all of which my own.


Coon_Dog

That code right there got it going.  Thanks for all your help.  Cant find mark solved button.  I finally found the attach button.  Don't know how I missed it. LOL

Advertisement: