MYSQL Syntax Check

They have: 42 posts

Joined: Dec 2002

Hello Again Everyone,

I am VERY new to mysql and I have been having a problem getting the synax right for a query I need to run.

I was hoping that someone from this forum could take a peek at my syntax for this query and tell me where I may be messing up:

When I run this in mysql I get an error message but I THINK I am close.

<?php
            $sql
= "SELECT field_name FROM  default_listingsDBElements WHERE (paypaluser = 'yes')";   
?>

Any help would you can offer would be great.

Thanks
NYColt

s0da's picture

He has: 157 posts

Joined: Mar 2004

try backticks around the fieldnames; you might have "badwords" that are being used.

SELECT `field_name` FROM `default_listingsDBElemenets` WHERE `paypaluser`='yes'

They have: 42 posts

Joined: Dec 2002

this is the error message I get:

Unknown column 'paypaluser' in 'where clause'

s0da's picture

He has: 157 posts

Joined: Mar 2004

is that column in your table?

They have: 42 posts

Joined: Dec 2002

The table name is:default_listingsDBElemenets
The Column is:field_name
The field is: paypaluser
The field value is: yes or no ( i am looking for all that say yes)

s0da's picture

He has: 157 posts

Joined: Mar 2004

well either the field `paypaluser` is NOT in the column OR the SQL server is messed up somehow.

They have: 42 posts

Joined: Dec 2002

ok this is the query I needed, which works:

if($sql = "SELECT field_name FROM " . $config[table_prefix] . "default_listingsDBElements WHERE
field_name='paypaluser' and field_value = 'yes' ")

What I want to do now is call a function called:

<?php
renderpaypal
();
?>

if the result of the query equals yes.

Any suggestions how I can call that function within the if statement?

Thanks for any help.

NYColt

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Okay, you're not actually querying the database, eh? You're just setting up the question, not asking it.

<?php
// longhand for clarity

$sql = \"SELECT ... \";
$qry = mysql_query($sql);
if (!
$qry) { echo mysql_error(); }

// shorthand for brevity

if (!
$qry = mysql_query(\"SELECT ...\")) echo mysql_error();
?>

Normally at this point you get the results of the query and then manipulate them (results with mysql_fetch_array or similar).

I would suggest you rethink your query to be to get the value of paypaluser, then set a condition then.

i.e.

<?php
// select the value for paypaluser (yes/no) from the table for this user
$sql = \"SELECT paypaluser FROM \" . $config[table_prefix] . \"default_listingsDBElements WHERE user_field_name = user_field_value\");

$qry = mysql_query($sql);
if (!
$qry) echo mysql_error();

while (
$row = mysql_fetch_array($qry)) {
   
$paypaluser = $row['paypaluser'];
    if (
$paypaluser == 'yes') {
        renderpaypal();
    }
    else {
        // do something else, choose another payment method?
    }
}
?>

They have: 461 posts

Joined: Jul 2003

i'm not sure of the structure, could you give us something to have a common basis?

give us partial structure. we might be able to actually help you modify to a better structure:

ie: if you need a yes/no feild, a tiny int using 0/1 (true/false) may actually be better than a text feild that will use yes/no.

id est:
main table structure:

pid int # player id number
player char(15) # username
paypal tinyint(1) # uses paypal
stormpay tinyint(1) # uses stormpay

php snippet:

# this grabs everyone who uses paypal/stormpay
$sql="SELECT username FROM users WHERE";
switch($paytype){
case '0': $sql.=" paypal=1"; break; # this gets the paypal users
case '1': $sql.=" AND stormpay=1;" break; # this add stormpay
case '2': $sql.=" stormpay=1;"; break; # storm pay only
}

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.