Glenugie Posted December 28, 2008 Share Posted December 28, 2008 I am working on a project using PHP and MySQL, I am currently doing part which dynamically changes an image using the value in a database. I have it connect to the database, and start a session. Here is the query: <? 13- $username = $_SESSION["username"]; 14- $result = mysql_query("SELECT character_image FROM Users AS character_image WHERE username=$username"); 15- $row = mysql_fetch_array($result); 16- $CharImage = $row['character_image']; ?> I have tested that $username has a value by echoing it, and it does. It returns this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in htdocs\charsheet.php on line 15 And the location of the image it tries to access is what was entered as text, but it misses out the variable part, as the query is failing. Any advice as to why the query is failing would be appreciated ~Glenugie~ Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/ Share on other sites More sharing options...
revraz Posted December 28, 2008 Share Posted December 28, 2008 You need single quotes around '$username' in your query. Use mysql_error() after your query to help troublehsoot. Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-724908 Share on other sites More sharing options...
Glenugie Posted December 28, 2008 Author Share Posted December 28, 2008 Thanks, that fixed it, can't believe it was so simple Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-724910 Share on other sites More sharing options...
Glenugie Posted December 28, 2008 Author Share Posted December 28, 2008 Rather than start a new thread, I have another problem, different query. I am trying to create a simple inventory system, this inventory system is to reference an items table, which holds the names of each item, which is normally represented as an item. The inventory holds the item code, which linked by relationship to the items code. The code for the query is: 5- $result= mysql_query("SELECT Inventory.Item_Code, Items.ItemID, Items.Name FROM (Inventory INNER JOIN Items ON Inventory.Item_Code = Items.ItemID)"); 6- $row = mysql_fetch_array($result); The error returned is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in htdocs\inventory.php on line 6 It is designed to basically get the real item name from the items database from the item code in the inventory database. I think I may have my syntax slightly wrong. Any help with this second problem would of course be appreciated. Thanks. ~Glenugie~ Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-724950 Share on other sites More sharing options...
Glenugie Posted December 28, 2008 Author Share Posted December 28, 2008 Sorry, forgot to post this, I'm running on MySQL server version 5.0.45 and client version 5.0.27 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-724991 Share on other sites More sharing options...
corbin Posted December 28, 2008 Share Posted December 28, 2008 Try removing the parenthesis after the FROM. Not sure why that would do it, but it's all I can think of. Also, try changing $result= mysql_query("SELECT Inventory.Item_Code, Items.ItemID, Items.Name FROM (Inventory INNER JOIN Items ON Inventory.Item_Code = Items.ItemID)"); To $result= mysql_query("SELECT Inventory.Item_Code, Items.ItemID, Items.Name FROM (Inventory INNER JOIN Items ON Inventory.Item_Code = Items.ItemID)") or die(mysql_error()); (In a production environment, you would never want to use an or die() clause to check for a query error, but for dev'ing it's usually fine ;p.) Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-724997 Share on other sites More sharing options...
Glenugie Posted December 28, 2008 Author Share Posted December 28, 2008 Thank you, that worked wonderfully Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-725002 Share on other sites More sharing options...
corbin Posted December 28, 2008 Share Posted December 28, 2008 No prob. Quote Link to comment https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/#findComment-725011 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.