PHP Functions that retrieves from SQL

Hi
I guess I’m novice not ninja in PHP programming… I’m trying to make a function that runs a sql query and stores in variables the results so that i can after calling the function output the variables as i see fit.

my code is this:
calling the function:

getProductsById($value['prodid']);

the function:

function getProductsById($idToGet)
{
	echo $idToGet;
	include 'inc/db.inc.php';
	// SQL:
	$sql	=	"SELECT * FROM products WHERE prodid = '" . $idToGet . "'";
	// Run SQL:
	$productsResult	=	mysqli_query($link, $sql);
	// Error running SQL:
	if (!$productsResult) { echo '<p class="warning">Kunne ikke hente produkt.</p>'; }
	// Retrieve and Store:
	$productsRow			= mysqli_fetch_array($productsResult);
	$prodid					=	$productsRow['prodid'];
	$userid_users			=	$productsRow['userid_users'];
	$productCatetgoryid_productCategory	=	$productsRow['productCatetgoryid_productCategory'];
	$productCatalogNumber	=	$productsRow['productCatalogNumber'];
	$productName			=	$productsRow['productName'];
	$productSize			=	$productsRow['productSize'];
	$productUnitsPerCarton	=	$productsRow['productUnitsPerCarton'];
	$productUnitsPerItem	=	$productsRow['productUnitsPerItem'];
	$productDescription		=	$productsRow['productDescription'];
	$productFile			=	$productsRow['productFile'];
	$productFileDescription	=	$productsRow['productFileDescription'];
	$productExtraOne		=	$productsRow['productExtraOne'];
	$productExtraTwo		=	$productsRow['productExtraTwo'];
	$productExtraThree		=	$productsRow['productExtraThree'];
	$productPrice			=	$productsRow['productPrice'];
	$productDate			=	$productsRow['productDate'];
}

the values are sent correct to the function as i gotten it confirmed by echoing $idToGet but i dont get anything back from it - neither do i get any error

Help please

No.

You’d have to set that function to a variable. Then if the variable !== false, extract it.

$Row = getProductsById(1);
if($Row != false){
    extract($Row);
    echo $prodid;
}else{
    //not found
}

Again, it will make more sense if you use functions more effectively like I showed you above.

so if i understand you correctly this should work:

function getProductsById($idToGet)
{
	include 'inc/db.inc.php';
	// SQL:
	$sql	=	"SELECT * FROM products WHERE prodid = '" . $idToGet . "' LIMIT 1";
	// Run SQL:
	$productsResult	=	mysqli_query($link, $sql);
	// Error running SQL:
	if (!$productsResult) { echo '<p class="warning">Kunne ikke hente produkt.</p>'; }
	// Retrieve and Store:
	$productsRow = mysqli_fetch_array($productsResult);
	return $productsRow;
}
?>

and then do extract($productsRow);
?

That’s because the variables are trapped within the scope of a function. Variables inside a function aren’t affected by variables inside a function.

For example:

$i = 0;
function something(){
    $i++;
}
something();
echo $i;

To get a value from a function you need to return a variable. That variable, if you want multiple data, can either be an array or an object.

So, let’s make this simple and use an array. To help yourself out, break it down into easier, reusable functions.


/*
 * Returns an array of rows based on a query
 */
function selectMultipleFromDatabase($SQL){
    $Query = MySQLi_Query($SQL);
    if($Query !== false){
        return array(); // empty... You may want to log this as an  error.
    }
    $Return = array();
    while($Row = MySQLi_Fetch_Array($Query)){
        $Return[] = $Row;
    }
    return $Return;
}

/*
 * Returns a single row from the database, as an array.
 * Returns false if it doesn't exist.
 */
function selectSingleFromDatabase($SQL){
    $Rows = SelectMultipleFromDatabase($SQL);
    if(count($Rows) < 1){
        return false;
    }else{
        return $Rows[0];
    }
}


/*
 * Returns an array of a single product based on it's ID
 */
function getProductByID($ID){
    $ID = (int)$ID;
    $SQL = 'SELECT * FROM products WHERE prodid = ' . $ID . ' LIMIT 1';
    return selectSingleFromDatabase($SQL);
}


/*
 * Usage:
 */
$Product = getProductByID(4);
if($Product === false){
    echo 'Product with ID 4 cannot be found';
}else{
    echo 'Product with ID 4: ' . $Product['productName'];
}

If you wish to access the product information as variables rather than the array accesss method, extract() the array:

$Product = getProductByID(4);
 if($Product === false){
     echo 'Product with ID 4 cannot be found';
 }else{
    extract($Product);
     echo 'Product with ID 4: ' . $productName;
 }

The above hasn’t been tested, but it should help you out :slight_smile: