widget Posted November 24, 2008 Share Posted November 24, 2008 Using php and mySQL Table consists of user_name amulet_id What I am trying to do is display a table of 9 cells (3 by 3) kinda like a puzzle. Labeled 1 to 9 If the user has a puzzle piece, it shows in the table. example, if they have piece number 9 then in the database would be... user_name: pinkkitty amulet_id: 9 Here is my code $amulet_piece = mysql_query("SELECT * FROM amulet WHERE user_name = $username"); if ($amulet_piece[amulet_id] = 1) { $amulet1 = "<img src=$base_url/images/real image here.gif>"; } else { $amulet1 = "<a href=amulet.pro.php?amulet=1><img src=$base_url/faded image here.gif></a>"; } if ($amulet_piece[amulet_id] = 2) { $amulet2 = "<img src=$base_url/images/real image here.gif>"; } else { $amulet2 = "<a href=amulet.pro.php?amulet=2><img src=$base_url/faded image here.gif>"; } I have tried all sorts of combination's and I cant get it to work correctly, any help is greatly appreciated. I just know theres probably an easier way to accomplish what I am doing. Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 You'll need to use "mysql_fetch_array" or "mysql_fetch_assoc" to assign a variable to the result set. Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 erm example please Im a total noob really Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Also I've just noticed you need to use a double equals on your if statement otherwise you are assigning that value not validating it. This would also return as true. $amulet_piece = mysql_query("SELECT * FROM amulet WHERE user_name = $username"); if ($amulet_piece[amulet_id] == 1) { $amulet1 = "<img src=$base_url/images/real image here.gif>"; } else { $amulet1 = "<a href=amulet.pro.php?amulet=1><img src=$base_url/faded image here.gif></a>"; } Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 I had == at one point its one of the combination's I tried Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 $amuletQuery = mysql_query("SELECT * FROM amulet WHERE user_name = $username"); // slight edit added while ($amulet_piece = mysql_fetch_array($amuletQuery, MYSQL_ASSOC)) { // Your code here } Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 so would I still put the if(amuletpiece ==1) {do this} else {do that} etc etc Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Yes Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 $amuletQuery = mysql_query("SELECT * FROM amulet WHERE user_name = $username"); while ($amulet_piece = mysql_fetch_array($amuletQuery)) { if ($amulet_piece[amulet_id] == 1) { $amulet1 = "<img src=$base_url/images/user_images/opg_1/items/item_30893.gif>"; } else { $amulet1 = "<a href=amulet.pro.php?amulet=1><img src=$base_url/farm/images/1.gif></a>"; } } Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 try this instead: while ($amulet_piece = mysql_fetch_assoc($amuletQuery)) { } This will make $amulet_piece an array like $amulet_piece['id'] etc.. Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 same error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Silly question, but I assume you have a line in your code to connect to the database? Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 yes the script does an include all other database connections on the page work Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 This is a textbook example of this type of query: <?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf("ID: %s Name: %s", $row["id"], $row["name"]); } mysql_free_result($result); ?> You might want to try tweaking this script and see what outputs. Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Ah, it could be your query is erroring! Try '".$username."' instead of just $username. Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 tried that, it outputs the correct database information Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 changed .$username. nothing happened Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Did you try single quotes? Also try this: $link = mysql_connect("localhost", "mysql_user", "mysql_password"); mysql_select_db("db_name", $link); $amuletQuery = mysql_query("SELECT id, name FROM mytable",$link); echo mysql_errno($link) . ": " . mysql_error($link) . "\n"; Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 gave this result 0: the database connection is fine Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 All i need to do is, find out if the user has a number 1 2 3 4 5 6 7 8 and/or 9 then if so, display a certain image. Man its sooo frustrating and I really appreciate your help here Quote Link to comment Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 Theres something wrong with my if else Quote Link to comment Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Try == "1" instead of == 1 sometimes if the number format isn't correct this can sort the problem. I expect this can be solved by int_val also What does the code after this look like? Quote Link to comment Share on other sites More sharing options...
widget Posted November 25, 2008 Author Share Posted November 25, 2008 I moved the info to another table and all is working Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.