[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

extract() not working

Does someone see a reason why extract() in line 7 below shouldn't work.  The database access is working because I can see in line 6 that the timestamp at $row[0] is correct and the firstName at $row[1] is correct. But extract($row) doesn't create the variables $timestamp and $firstName.

Thanks for any insight.

        $db = db_connect();
	$query = "SELECT timestamp, firstName FROM sales WHERE transactionID = '$transactionID'";
	$result = mysqli_query($db, $query)
		or die ("Couldn't execute query");  
	$row = mysqli_fetch_row($result);	
	echo "\$row[0] = $row[0], \$row[1] = $row[1]";
	echo "\$timestamp is $timestamp \$firstName is $firstName";

Open in new window

1 Solution
Per PHP extract, you cannot use a numerically-indexed array: "a numerically indexed array will not produce results unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID."

So pass EXTR_PREFIX_ALL as the second argument to extract.
stevaAuthor Commented:
Ah! Yes.  I was using mysqli_fetch_row() to get the query results, which returns an enumerated array, which extract() doesn't work on,  unless you include your own prefix.  .I should have used mysqli_fetch_assoc(), which brings back an associative array, which extract() does work on.


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now