Jump to content

if () using table data


cryp7

Recommended Posts

Ok i did a quick search like normal and cudnt find ne thing

So what im looking for is the script to search the "user" table for the row "characters" by the "username", and if the value found for the spefic user is "1" then it needs to say sorry cant do it etc. and if its "0" then it should let the rest happen

[code]
<?php
mysql_connect("localhost", "xxx", "xxx") or
die("Could not connect: " . mysql_error());
mysql_select_db("xxx");

$create = mysql_query("SELECT characters FROM users WHERE username='$username'");
$results2 = mysql_query($create) or die(mysql_error());
if (mysql_num_rows($results2) == 1) {
die("You Already Have A Character, And Cant Make Another - Try Editing Your Character Instead <a href=\"character_edit.php\">Edit Here</a>");
}
if ($create == 0) {
echo "So Therefore You Can Make One Here:";
}
?>
[/code]

Thats what i got so far - and i get...
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #19' at line 1
[/quote]

so help would be very much appreciated.
Link to comment
Share on other sites

Example:

[code]
$create = "SELECT `characters` FROM `users` WHERE `username` = '$username' AND `characters` = '1'";
$results2 = mysql_query($create) or die(mysql_error());
if (mysql_num_rows($results2) > 0) {
die("You Already Have A Character, And Cant Make Another - Try Editing Your Character Instead <a href=\"character_edit.php\">Edit Here</a>");
} else {
   echo "So Therefore You Can Make One Here:";
}
[/code]
Link to comment
Share on other sites

well.. ur running the SQL query twice..

[code]
$create = mysql_query("SELECT characters FROM users WHERE username='$username'");
$results2 = mysql_query($create) or die(mysql_error());
[/code]

$create = mysql_query...
$result2 = mysql_query...

Just have:

[code]
$create = "SELECT characters FROM users WHERE username='$username'"
$results2 = mysql_query($create) or die(mysql_error());
[/code]


..Adam
Link to comment
Share on other sites

You're executing the function mysql_query() twice.
[code]<?php
$create = mysql_query("SELECT characters FROM users WHERE username='$username'");
$results2 = mysql_query($create) or die(mysql_error());
?>[/code]

Do this instead:
[code]<?php
$create = "SELECT characters FROM users WHERE username='$username'";
$results2 = mysql_query($create) or die('Problem with query: ' . $create . '<br>' . mysql_error());
?>[/code]

Ken
Link to comment
Share on other sites

EDIT: Ok i worked out forms :D

now waht i need to know is how to get values from a database so that i can put them into a drop down box on a form

ie. i want to find what items the user has that are "type" = "S" now to do this i need to search the "inventory" table for "item_id"s by "username", then seach "item" table by "item_id" to add a "name" etc to the "item_id" but i wanna sort these items by "type" "S"

e.g. user admin has item_id 1,2 and 3
these are:
1 - Blue Ball Skin - type "S"
2 - Green Ball Skin - type "S"
3 - Crazied Blob Skin - type "I"

so i need values 1 and 2, selected and then a way to add them to a drop down list in a form...

heres what i got so far:
[code]
<?php
mysql_connect("localhost", "xxx", "xxx") or
die("Could not connect: " . mysql_error());
mysql_select_db("xxx");

$allitems_id = mysql_query("SELECT id,item_id FROM inventory WHERE username='$username'");
$row10 = mysql_fetch_array($allitems_id);

$item_id = $row10['item_id'];
$type = "S";

$sitems_id = mysql_query("SELECT name,description,buy_price,sell_price,img_path FROM items WHERE item_id='$item_id' AND type='$type'");
$row11 = mysql_fetch_array($sitems_id);
printf ("Name: %s", $row11['name']);
mysql_free_result($sitems_id);
?>
[/code]

although this dosnts print the values, i think its on the right track...?

any help welcome thxs
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.