Jump to content

[SOLVED] Problems with a MySQL Query


Glenugie

Recommended Posts

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~

Link to comment
https://forums.phpfreaks.com/topic/138648-solved-problems-with-a-mysql-query/
Share on other sites

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~

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.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.