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!!

5
  • 3
    $key is an array, echo won't work to print arrays. Commented Jun 29, 2016 at 12:44
  • Very dangerous query! Commented Jun 29, 2016 at 12:46
  • 1
    Create a variable outside (for example: $myArray) the foreach loop, than array_push() the $key1) Commented Jun 29, 2016 at 12:46
  • If you'd like to see what's in the $key array, you can use print_r($key) Commented Jun 29, 2016 at 12:47
  • Yes I have tried with print_r it shows the last loop value only ? Commented Jun 29, 2016 at 12:55

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is is possible to display more than one values from the same column?
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
}

Comments

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(); 

Comments

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');

1 Comment

Is is possible to display more than one values from the same column?
-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>";

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.