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. Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/ 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. Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697561 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697562 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>"; } Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697564 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697567 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 } Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697568 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697569 Share on other sites More sharing options...
mtoynbee Posted November 24, 2008 Share Posted November 24, 2008 Yes Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697570 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697571 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>"; } } Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697572 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.. Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697573 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697576 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? Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697578 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697579 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. Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697583 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. Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697588 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697589 Share on other sites More sharing options...
widget Posted November 24, 2008 Author Share Posted November 24, 2008 changed .$username. nothing happened Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697591 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"; Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697604 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697612 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697614 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697620 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? Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-697645 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 Link to comment https://forums.phpfreaks.com/topic/134014-solved-going-insane-here-help-please/#findComment-698610 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.