Jump to content

If !$variable nested in function always reads null?


davidnowlin

Recommended Posts

:confused:

Been scratching my head about this all day.  Cannot figure it out.  Any help would be appreciated.

 

<?php

$selectClause = 'SELECT * FROM idInfo WHERE';
$whereClause = '';

function addWhere($fxnName, $fxnValue) {
if (!$whereClause) {
	return $fxnName . '=\'' . $fxnValue . '\'';
} else {
	return ' AND ' . $fxnName . '=\'' . $fxnValue . '\'';
}
}
$whereClause.=addWhere("firstName", $firstName);
$whereClause.=addWhere("lastName", $lastName);
$whereClause.=addWhere("idNum", $idNum);

echo 'mysql_query("' . $selectClause . ' ' . $whereClause . '"); <br /><br />';
?>

 

This is meant to generate a query of the type:

 

SELECT * FROM idInfo WHERE firstName='firstNameINPUT' AND lastName='lastNameINPUT' AND idNum='idNumINPUT'

 

For some reason, though, the if statement always reads false.  No matter how I try to work the syntax

if (!$whereClause)
if ($whereClause)
if ($whereClause == NULL)
if ($whereClause != NULL)
if (empty($whereClause)
etc.

 

I've tried all of those and more, experimenting with and without quotes.  They always return a FALSE for the existence of the variable, so that the string never contains the word "AND" as it should by the end.  I've tried these same if statements outside the function and they work fine.  Inside the function, it doesn't matter what the value of the variable is, the function runs as though it has no value.

 

Comments?

Suggestions?

Link to comment
Share on other sites

Genius.  Worked like a charm.  Just had to set the variable as a parameter of the function.

 

Thanks!

 

By the way, making people answer questions about coding as part of the capcha for submitting questions about coding: also genius, and potentially hilarious.

 

:D

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.