Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6783

Re: parameters with numbers

From Christoph Becker <cmbecker69@gmx.de>
Newsgroups comp.lang.php
Subject Re: parameters with numbers
Date 2012-08-14 23:27 +0200
Organization Aioe.org NNTP Server
Message-ID <k0efqe$sea$1@speranza.aioe.org> (permalink)
References <slrnk2lchn.drm.houghi@penne.houghi>

Show all headers | View raw


houghi wrote:
> I have something like the following:
> 
> <?php
> include_once('connect.php');
> $ncast_1 = 'nm0001147';
> $ncast_2 = 'nm0518718';
> $ncast_3 = 'nm0001212';
> $ncast_4 = 'nm0004825';
> for ($i = 1; $i <= 4; $i++) {
> 	$sql = "SELECT * FROM cast WHERE nconst LIKE '$ncast_1'";
> 	$result = mysql_query($sql) or die(mysql_error());
> 	$row = mysql_fetch_array( $result );
> 	$cast_[$i] = $row['name'];
> 	echo "$cast_[$i] $i <br>";
> }
> mysql_close($conn);
> ?>
> 
> This works as expected, but not as I want it to work. The $ncast_1 after
> the LIKE in the $sql should vary with the value of $i.
> 
> I have tried using various things, like $ncast_[$i] which did not work.
> At this moment I am in the state of typing almost random characters and
> quotes and escapes.
> 
> I have something working as it is four only different parameters and I
> just repeated the code, instead of using a loop. However I would like to
> learn and have no idea what to type into google to do the search.

Indeed it's possible to construct the name of a variable dynamically.
Just replace the first line of the for loop's body with:

$ncast = "ncast_$i";
$sql = "SELECT * FROM cast WHERE nconst LIKE '$$ncast'";

But I would recommend using an array instead as you have done with
$cast_; see <http://php.net/manual/en/language.types.array.php> or
google for "php array".

HTH
-- 
Christoph M. Becker

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

parameters with numbers houghi <houghi@houghi.org.invalid> - 2012-08-14 22:16 +0200
  Re: parameters with numbers Christoph Becker <cmbecker69@gmx.de> - 2012-08-14 23:27 +0200
  Re: parameters with numbers Denis McMahon <denismfmcmahon@gmail.com> - 2012-08-14 22:15 +0000
    Re: parameters with numbers houghi <houghi@houghi.org.invalid> - 2012-08-15 00:31 +0200

csiph-web