0

I have a php code for some MYSQL interogations,

Code is:

      $DBTYPE = 'mysql';
      $DBHOST = 'localhost';
      $DBUSER = 'tuser';
      $DBPASSWORD = 'password';
      $DBNAME = 'dbname';
      $link = mysql_connect($DBHOST, $DBUSER, $DBPASSWORD);
      mysql_select_db($DBNAME); 
      if (!$link) {
          die('Could not connect: ' . mysql_error());
      }
 //IMG**0**
       $hotelc = $hotelCodes[**0**];    
      $result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
  if(!$result) {
      die("Database query failed: " . mysql_error());
  }
  while ($row = mysql_fetch_array($result)) {
      $ImageURL**0** = $row["ImageURL"];
  }
  //IMG**1**
       $hotelc = $hotelCodes[**1**];    
      $result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
  if(!$result) {
      die("Database query failed: " . mysql_error());
  }
  while ($row = mysql_fetch_array($result)) {
      $ImageURL**1** = $row["ImageURL"];
  }
..........................
//IMG**x**
       $hotelc = $hotelCodes[**x**];    
      $result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
  if(!$result) {
      die("Database query failed: " . mysql_error());
  }
  while ($row = mysql_fetch_array($result)) {
      $ImageURL**x** = $row["ImageURL"];
  }

The repeating value on each code line is bolded.

How can i create a Mysql parameterized queries in php.n to avoid write all the lines .I need to extract ~100 $ImageURL from the Flat_table where the $hotelc is found.

Razvan Baba
  • 155
  • 9
  • 3
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 20 '15 at 14:55
  • 1
    When PHP 7 is released, the `mysql_` functions will be removed entirely – Machavity Jan 20 '15 at 15:01

3 Answers3

0

For example you have to repeat it $N times:

for($i=0; $i<$N; $i++)
{
    $hotelc = $hotelCodes[ $i ];    
    $result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
    if(!$result) {
        die("Database query failed: ".mysql_error());
    }
    while ($row = mysql_fetch_array($result)) {
        ${'ImageURL'+$i} = $row["ImageURL"];
    }
}
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24
0

To loop, use for:

for($n=0; $n<100; $n++){
  $hotelc = $hotelCodes[$n];    
  $result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
  if(!$result) {
      die("Database query failed: " . mysql_error());
  }
  while ($row = mysql_fetch_array($result)) {
      $ImageURL[$n] = $row["ImageURL"];
  }
}

But the inner functions inside the loop is inefficient because mysql query will be executed 100 times. You can query all the ImageURL by using IN() syntax in mysql:

//Wrap all hotelCodes into one string for query, like ["a","b"] to "'a','b'"
$len = count($hotelCodes);
foreach($hotelCodes as $key=>$code){
 $hotelCodes[$key] = "'".$code."'";
}

$codesStr = implode(",", $hotelCodes);

$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode IN (".$codeStr.")", $link);

//Other things...
Bandon
  • 809
  • 1
  • 6
  • 14
0

When writing a function, you look for the commonality. Furthermore, you want to minimize the database interaction. I am, for the sake of deprecation, going to assume $link uses mysqli_connect()

$ImageURL = array();
$list = implode('", "', $hotelCodes);
$result = mysqli_query($link, 'SELECT ImageURL FROM Flat_table where HotelCode IN "' . $list . '"');
while($row = mysql_fetch_assoc($result)) {
     $ImageURL[] = $row["ImageURL"];
}

This runs only one query and then loops through the results, generating an associative array. So echo $ImageURL[0]; will output your first URL.

Machavity
  • 30,841
  • 27
  • 92
  • 100