Problem with refrence to an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shahroq
Forum Newbie
Posts: 6
Joined: Wed Sep 21, 2005 8:05 am

Problem with refrence to an array

Post 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'
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
shahroq
Forum Newbie
Posts: 6
Joined: Wed Sep 21, 2005 8:05 am

Post by shahroq »

ye, i know this solution, but i don know whats the problem about my code.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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? :)
Post Reply