extract() not working

Posted on 2011-10-11
Last Modified: 2013-12-12
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

Question by:steva
    LVL 1

    Accepted 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.

    Author Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now