Jump to content


Photo

if () using table data


  • Please log in to reply
4 replies to this topic

#1 cryp7

cryp7
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 26 March 2006 - 02:14 PM

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

<?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:";
}
?>

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.

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 03:01 PM

Example:

$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:";
}


#3 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 March 2006 - 03:35 PM

well.. ur running the SQL query twice..

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

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

Just have:

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


..Adam

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 March 2006 - 04:26 PM

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

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

Ken

#5 cryp7

cryp7
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 26 March 2006 - 04:29 PM

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:
<?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);
?>

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

any help welcome thxs




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users