spinner0205 Posted August 12, 2011 Share Posted August 12, 2011 I have the following code. $accepted_username comes from a posted form. I would like for it to select a field from the table in the $result query then turn that into a usable variable for the second query. I am very new to PHP so can I get some code examples for the $entryid variable, because every time I try to echo $result I get Resource #19. I know I can't echo a simple mysql query but I am unsure what to do to make it usable. $accepted_username = $_POST['accepted_username']; $result = mysql_query("SELECT entryid FROM `thatsact_mc_permissions`.`PrEntries` WHERE name = '$accepted_username';"); $entryid = ?????????; mysql_query("INSERT INTO `thatsact_mc_permissions`.`PrInheritance` (`uinheritid`, `childid`, `parentid`, `parentorder`) VALUES (NULL, '$entryid', '1', '0') ON DUPLICATE KEY UPDATE parentid=VALUES(parentid);"); Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/ Share on other sites More sharing options...
MasterACE14 Posted August 12, 2011 Share Posted August 12, 2011 $result = mysql_query("SELECT entryid FROM `thatsact_mc_permissions`.`PrEntries` WHERE name = '$accepted_username';"); $row = mysql_fetch_assoc($result); // 'Fetch' the result into an array $entryid = $row['entryid']; Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256242 Share on other sites More sharing options...
spinner0205 Posted August 12, 2011 Author Share Posted August 12, 2011 Thank you for such a fast reply but your code returns: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256248 Share on other sites More sharing options...
MasterACE14 Posted August 12, 2011 Share Posted August 12, 2011 try this... $result = mysql_query("SELECT entryid FROM `thatsact_mc_permissions`.`PrEntries` WHERE name = '$accepted_username';"); if(mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); // 'Fetch' the result into an array $entryid = $row['entryid']; } else { echo "There is no records!"; } Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256252 Share on other sites More sharing options...
spinner0205 Posted August 12, 2011 Author Share Posted August 12, 2011 Now I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource And FYI, the table it is selecting from is very populated. Here is the entire code if it matters; <?php require ('sql.php'); //gather the accepted app username as post data $accepted_username = $_POST['accepted_username']; //insert to prentries (wont if already there) mysql_query("INSERT INTO `thatsact_minecraft_permissions`.`PrEntries` (`entryid`, `name`, `worldid`, `type`) VALUES (NULL, '$accepted_username', '1', '0');"); //get entryid of user for next query $result = mysql_query("SELECT entryid FROM `thatsact_mc_permissions`.`PrEntries` WHERE name = '$accepted_username';"); if(mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); $entryid = $row['entryid']; } else { echo "There is no records!"; } //insert to prinheritance using entryid mysql_query("INSERT INTO `thatsact_minecraft_permissions`.`PrInheritance` (`uinheritid`, `childid`, `parentid`, `parentorder`) VALUES (NULL, '$entryid', '1', '0') ON DUPLICATE KEY UPDATE parentid=VALUES(parentid);"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256255 Share on other sites More sharing options...
MasterACE14 Posted August 12, 2011 Share Posted August 12, 2011 what is the value of this? $_POST['accepted_username'] Also you need to validate that input. Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256257 Share on other sites More sharing options...
spinner0205 Posted August 12, 2011 Author Share Posted August 12, 2011 The value for that post is bob just as a test user. And I was figuring on doing the validating stuff after I got it working fully. Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256263 Share on other sites More sharing options...
chintansshah Posted August 12, 2011 Share Posted August 12, 2011 1. Can you print insert query and check if it's working in phpmyadmin 2. Which datatype you are using for 'name' field 3. use mysql_error to get exact error in query, ref: http://php.net/manual/en/function.mysql-error.php Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256267 Share on other sites More sharing options...
spinner0205 Posted August 12, 2011 Author Share Posted August 12, 2011 Changed to; $query = "SELECT entryid FROM `thatsact_minecraft_permissions`.`PrEntries` WHERE name = '$accepted_username';"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($res); $entryid = $row['entryid']; but doesn't seem to change anything. Same error. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Also, ran the SELECT query in phpMyAdmin, selects the entryid perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256271 Share on other sites More sharing options...
spinner0205 Posted August 12, 2011 Author Share Posted August 12, 2011 The name field datatype is varchar(32). EDIT: Got it working, not quite sure what did between all the suggestions but thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/244580-fetch-field-from-one-query-to-use-in-the-next-query/#findComment-1256275 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.