0

I am having an trouble in displaying values in PHP.

Value pass through URL career.php?mode=1,2,3

Here is my code

$id = $_GET['mode']; // Id get from URL
    //echo $id;
    $query = "SELECT `job_title` FROM `job` WHERE `job_id` IN ($id)";
    $res    = mysqli_query($conn, $query);

    foreach(($row = mysqli_fetch_array($res)) as $key1){
          $key[] = $key1;
    }

PHP Code:

<?php echo $key;?>

Hence it is in looping so it shows the last looped value. Is it possible to display the all values through loop.

Help me out guys!!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Prashanth
  • 328
  • 5
  • 21

5 Answers5

2

You have multiple options to output an array.

Echo and foreach

You c an loop through your array and echo each $key and $value

foreach ($keys as $key => $value) {
    echo $key." : ".$value."<br />";
}

print_r and var_dump

This is mostly used in debugging.

print_r($keys);

var_dump($keys);

Imploding

You can implode your array to echo the concatted values.

// The first parameter is the devider or separator
echo implode('', $keys);

Is it possible to display more than one value from the column?

Yes, ofcourse. Look at the following example:

foreach ($keys as $value) {
    echo $value['columnone'];
    echo $value['columntwo'];
    echo $value['columnthree'];
}

Resources

Peter
  • 8,776
  • 6
  • 62
  • 95
1

As your code in open for sql injection you need to use bind and prepare statement . Use while loop to echo your data as

$ids[] = $_GET['mode']; // store it into array
$query = "SELECT `job_title` FROM `job` WHERE `job_id` IN (";
$query .= implode(',', array_fill(0, count($ids), '?'));// bind your param
$query .= ') ';
$stmt = $conn->prepare($query);
call_user_func_array(array($stmt, 'bind_param'), $ids);//Call a callback with an array of parameters
$stmt->execute();
$stmt->bind_result($job_title);// bind result

while ($stmt->fetch()) {// use wlile loop here
    printf("%s\n", $job_title);//echo  result
}
Saty
  • 22,443
  • 7
  • 33
  • 51
0

You can do something like this:

$mysqli = new mysqli("server", "user", "password", "db");
$id = $_GET['mode'];
$query = "SELECT `job_title` FROM `job` WHERE `job_id` IN ($id)";    
if($res = $mysqli->query($query)){    
    while($obj = $result->fetch_object()){
        echo $obj->job_title;
    }
}
$res->close(); 
Gerard Cuadras
  • 1,655
  • 1
  • 17
  • 23
0

Try this:

<?
    $servername = "localhost";
    $username = "yourUSERNAME";
    $password = "yourPASS";
    $dbname = "yourDBNAME";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
         exit();}

        $id = $_GET['mode']; // Id get from URL
            //echo $id;
            $query = "SELECT `job_title` FROM `job` WHERE `job_id`=".$id."";
            $result = $conn->query($sql);
            while($row=$result->fetch_assoc()){ 
           //do something...
            }
     $conn ->close(); 
?>

tip: put your connection on another connection.php file.

like:

require('Connect.php');
Adi Azarya
  • 4,015
  • 3
  • 18
  • 26
-1
$cars = array
  (
  array("Volvo",22,18),

  array("BMW",15,13),

  array("Saab",5,2),

  array("Land Rover",17,15)

  );

for ($row = 0; $row < 4; $row++) {

  echo "<p><b>Row number $row</b></p>";

  echo "<ul>";

  for ($col = 0; $col < 3; $col++) {

    echo "<li>".$cars[$row][$col]."</li>";

  }

  echo "</ul>";

}
gre_gor
  • 6,669
  • 9
  • 47
  • 52