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

$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'];

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

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

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

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.

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.