I am trying to greatly shorten code on my pages.
Is it possible to include variables in my fetch fields?

The below will all exist on same php page.

Here is the code I want included in my fetch

// variables to include in fetch
$abc = stripslashes($row->abc);
$def = stripslashes($row->def);
$ghi = stripslashes($row->ghi);
$query = "SELECT * FROM $dbtable WHERE ID = $id";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
if ($row)
	{
		$name = stripslashes($row->name);
		$parentID = $row->parentID;
		$content = stripslashes($row->content);
		
		// include abc, def, ghi
	}

Unless I misunderstand you I think this may be what you're looking for.

while($row = mysql_fetch_array($result)){
			$resultArray[$c] = 	$row[$returnItem];
			$c++;
		}
Member Avatar for diafol

if you declate/initialize the 3 variables before running the query, they'll most likely throw an error/or =null/false. If you place the 3 vars in an include file and then place the include command, it should work.

You could write a function with global vars e.g.

function set_vars($row){
  global $abc,$def,$ghi;
  $abc = stripslashes($row->abc);
  $def = stripslashes($row->def);
  $ghi = stripslashes($row->ghi);
}

$query = "SELECT * FROM $dbtable WHERE ID = $id";
$result = mysql_query($query);
$row = mysql_fetch_object($result);
if ($row)
	{
		$name = stripslashes($row->name);
		$parentID = $row->parentID;
		$content = stripslashes($row->content);
		set_vars($row);
	}

I don't know if that'll work. Perhaps some better programmers out there can suggest something more succinct and subtle.

Thank you for your reply.

I have about a dozen or so edit pages.
On each query, there are common values I want to fetch, such as name, parentID, content.

Then there are unique values per page that I want to include (examples: abc, def, ghi). These unique values, which can be a mix of integers or text, will be located on the same php page, somewhere outside and above the query.

I understand the concepts of arrays, but am not 100%.
Is there a way to do it without an array?
If not then pls explain what the c++ does.

Or perhaps, how would I use a function that returns code (rather than a string/single value) that can be used in my posted fetch query.

Thanks again.

you can initialize variable above your query

Ardav,

Thank you so much!
That worked. Simple. But it worked.
I see what you mean by "maybe there's a better way".
I would be interested to see how others might do something like that. But in the meantime, this does the trick.
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.