Page 1 of 1

Problem with refrence to an array

Posted: Sun Oct 09, 2005 12:42 pm
by shahroq
i want to build a sql query:

Code: Select all

<?php 
$varName = 'f_ins_rslt_';
$key = 1234;

${$varName.$key}[1] = 'Yes';
${$varName.$key}[2] = 'No';
${$varName.$key}[3] = 'Yes';

echo ${$varName.$key}[3];
echo "<br>";

$sql_query = "
		INSERT INTO `tbl1`
		(
		`car_id`,
		`ins1`,
		`ins2`,
		`ins3`
		)
		VALUES
		(
		'1234',
		'${$varName.$key}[1]',
		'${$varName.$key}[2]',
		'${$varName.$key}[3]'
		";						

echo $sql_query;
?>
but in my case the output is:

Code: Select all

INSERT INTO `tbl1` ( `car_id`, `ins1`, `ins2`, `ins3` ) 
VALUES ( '1234', 'Array[1]', 'Array[2]', 'Array[3]'
i want to 'Array[1]' replaced by its value :'Yes'

Posted: Sun Oct 09, 2005 1:00 pm
by feyd
it's easier to jump out of the string to do that..

Code: Select all

<?php
$varName = 'f_ins_rslt_';
$key = 1234;

${$varName.$key}[1] = 'Yes';
${$varName.$key}[2] = 'No';
${$varName.$key}[3] = 'Yes';

echo ${$varName.$key}[3];
echo "<br>";

$sql_query = "
      INSERT INTO `tbl1`
      (
      `car_id`,
      `ins1`,
      `ins2`,
      `ins3`
      )
      VALUES
      (
      '1234',
      '".${$varName.$key}[1]."',
      '".${$varName.$key}[2]."',
      '".${$varName.$key}[3]."'
      )";

echo $sql_query;
?>
you were missing a closing paren as well..

Posted: Sun Oct 09, 2005 3:13 pm
by shahroq
ye, i know this solution, but i don know whats the problem about my code.

Posted: Sun Oct 09, 2005 3:24 pm
by pilau
Your code is less messy and more trouble-free if you jump out of the string to enter variables.

And you lacked one bracket, as well. What could be more simple to get than that? :)