Jump to content

Fetch field from one query to use in the next query


spinner0205

Recommended Posts

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);");

Link to comment
Share on other sites

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!";
}

Link to comment
Share on other sites

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);");
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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