Customizing SMF > SMF Coding Discussion

db_query function problem

(1/2) > >>

The Wizard:
This code works and will display the contents of the shop_Dog in this case thats - dog6. The problem I'm having is that it displays dog6 in every post for all users and not the posts of the user who owns the dog. So how can I make this work?   


--- Code: ---function onUser () {
    global $smcFunc;

    $dog = '';

    $request = $smcFunc['db_query']('', '
            SELECT shop_Dog
            FROM {db_prefix}members',
array(
'shop_Dog' => $dog,
)
);

$temp = $smcFunc['db_fetch_assoc']($request);
echo $temp["shop_Dog"]; 

}
--- End code ---

Sorck:
It's simply searching for every user in the database... therefore will return a lot of results of which the first will be that of the lowest id user (usually 1, the admin/creator)

You need a WHERE id_member = {int:id_member} in your query (and you'll have to supply the id_member in your array).

Also, you have 'shop_Dog' in your array - what's the point of this? You're not injecting it into your query.

BTW, use lower case column names so it works with all of SMF's database options. ;) I've not corrected that below as it'd break your query. Change it via PHPMyAdmin and then change it to shop_dog :) (I don't know why it was shop_Dog in the first place as that's mixing two different styles of naming).


--- Code: ---function onUser () {
    global $smcFunc;
$id_member = 1; // not sure where this is running. if you just want the current viewers 'dog' then use $user_info['id']
    $dog = '';

    $request = $smcFunc['db_query']('', '
            SELECT shop_Dog
            FROM {db_prefix}members
            WHERE id_member = {int:id_member}
            LIMIT 0,1', // it's good practice to limit your query length
array(
'id_member' => $id_member,
)
);

$temp = $smcFunc['db_fetch_assoc']($request);
echo $temp["shop_Dog"]; 

}
--- End code ---

The Wizard:
When I run the code Sorck posted as is with no changes it works, but I still have the same problem it posts "dog6" in everybodys posts and not just in the posts of the dog owner.

When I try limiting the function to the curent user I get the following error:


--- Quote ---An Error Has Occurred! The database value you're trying to insert does not exist: id_member
--- End quote ---

Any ideas why this is happening?


--- Code: ---function onUser () {
    global $smcFunc;
//$id_member = 1; // not sure where this is running. if you just want the current viewers 'dog' then use $user_info['id']
   
$dog = $user_info['id'];    
   
   //$dog = '';

     $request = $smcFunc['db_query']('', '
             SELECT shop_Dog
             FROM {db_prefix}members
             WHERE id_member = {int:id_member}
             LIMIT 0,1', // it's good practice to limit your query length
array(
'id_member' => $id_member,
)
);

$temp = $smcFunc['db_fetch_assoc']($request);
echo $temp["shop_Dog"]; 

}
--- End code ---

vbgamer45:
Are you calling onUser inside the post display area is a function if so you should change it to something like

--- Code: ---unction onUser ($id_member) {
    global $smcFunc;
     $request = $smcFunc['db_query']('', '
             SELECT shop_Dog
             FROM {db_prefix}members
             WHERE id_member = {int:id_member}
             LIMIT 1', // it's good practice to limit your query length
array(
'id_member' => $id_member,
)
);

$temp = $smcFunc['db_fetch_assoc']($request);
echo $temp["shop_Dog"]; 
}

--- End code ---
Then call the function in post such as


onUser($user_info['id']);


But replace $user_info['id'] with the id of the member from the post

The Wizard:
hopefully this will make things more clear:

I trying to create a function that will check the shop_Dog row in the database and if the user has any data in that table the data then gets displayed under that users karma only.

Wiz



Navigation

[0] Message Index

[#] Next page

Go to full version