PDA

View Full Version : SQL problems


The)TideHunter(
05-31-2007, 12:13 PM
Hey, i've never really used sql before, i've tried but to no avail.
What im asking for is a query that will add a new section to the table (preferablly called "coins") and 2 functions which are Get and Set for the coins.

So for example, every member has a coins section (just like posts) and 2 functions like:

function Get takes userid returns coinsamount

function Set takes userid, coinsamount.

I'm really having trouble doing this, any help appreiciated.

Eikinskjaldi
05-31-2007, 10:21 PM
Hey, i've never really used sql before, i've tried but to no avail.
What im asking for is a query that will add a new section to the table (preferablly called "coins") and 2 functions which are Get and Set for the coins.

So for example, every member has a coins section (just like posts) and 2 functions like:

function Get takes userid returns coinsamount

function Set takes userid, coinsamount.

I'm really having trouble doing this, any help appreiciated.

You need to add the coins field to the table

alter table tablename add column coins int(11);

getter
select coins from tablename where userid=something

setter
update tablename set coins=something where userid=somethingelse

The)TideHunter(
06-02-2007, 07:18 AM
Thanks so much, exactly what i needed.

EDIT: I'm having problems, i tried this code:

<?php

require_once('./global.php');
require_once(DIR . '/includes/functions_bigthree.php');
require_once(DIR . '/includes/functions_forumlist.php');
// sql coins test
$db->query_read_slave("update user set coins=5 where userid=1");

$coins = $db->query_read_slave("select coins from user where userid=1");

echo $coins;
?>

And i added the column coins successfully to table users.
Whenever i try the page it returns "Resource id #16".

Eikinskjaldi
06-03-2007, 10:56 PM
query_read_slave sets the database object with a cursor, it does not return the actual values.


also when not doing a select you should use query_write. (I think...dont quote me on this one)



// sql coins test
$db->query_write("update user set coins=5 where userid=1");

$coins_query = $db->query_read_slave("select coins from user where userid=1");

while ($coins = $db->fetch_array($coins_query))
echo $coins['coins'];

Dismounted
06-04-2007, 12:08 PM
also when not doing a select you should use query_write. (I think...dont quote me on this one)
Partly correct, use query_write when WRITING to the database and query_read when READING. Pretty logical actually.

The)TideHunter(
06-04-2007, 03:55 PM
Thanks alot guys, its working now.