I want to select one field value from a record where another value in the record matches my specified value.
The "SKU" is unique, it isn't the PK, but it is unique. I select only the one field (Code) from the table, so the variable $gemCode should only contain that 1 value. However on the echo, it either outputs nothing like the below code, or if I do specify the field "$gemCode['Code'] it just spurts "Resource id #8" which I've never seen before.
$gemCode = mysql_query("SELECT Code FROM stock WHERE SKU = 'TN-YKJI-ESWB'");
echo "Gem Code: ".$gemCode."<br>";
This code is in a loop, for each line of a file which it'll be taking the SKU value from. Though even though the code has none of the loop in it (variables taken from the file etc) the output it does give, the "Resource id #8" number goes up each loop starting at 8:
Gem Code: Resource id #8
Gem Code: Resource id #9
Gem Code: Resource id #10
Gem Code: Resource id #11
Gem Code: Resource id #12
Gem Code: Resource id #13
Gem Code: Resource id #14
Gem Code: Resource id #15
I've had issues where before somehow data in my tables have spaces after them so "LIKE '%value%'" was needed, though I've checked and the values in question are don't have this, so its not like I'm looking from values that don't technically exist.
Should also mention I do have:
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('amazondb', $conn);
I'm really stumped, I thought it'd just be a simple select where.
Any help is much appreciated, thanks -Tom
You need to use mysql_fetch_assoc($gemCode) if you want to database result.
$gemCode = mysql_query("SELECT Code FROM stock WHERE SKU = 'TN-YKJI-ESWB'");
$gemArr = mysql_fetch_assoc($gemCode);
print_r($gemArr);
But, outsdie of "that's how you do it" you should really look into using mysqli_ instead of mysql. And if you're up to the challenge of learning PDO, you should try and use that (yes, even over mysqli_).
you need to fetch the data.
$gemData = mysql_query("SELECT Code FROM stock WHERE SKU = 'TN-YKJI-ESWB'");
$gemInfo = mysql_fetch_assoc($gemData);
echo "Gem Code: ".$gemInfo['Code']."<br>";
Complementing the previous answers, mysql_query() returns a resource, not the string.
As good practice, I suggest you remove the mysql_query from the loop and use "...WHERE SKU IN ('TN-YKJI-ESWB', 'yourSecondKey', ..., 'yourNKey')" and take a look on:
at php.net manual.