-5

I am trying to show some text depending upon what a status is set to in a db table.

See my code below:

$result=mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1")or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row ['stage_name'];

if($stage_name['stage_name'] == 'Shortlisting') { echo"Shortlisting"; } else { echo"Not Shortlisting"; } ?>

However this doesnt seem to be working properly as it is showing as Not Shortlisting even when stage_name equals Shortlisting.

Any ideas why?

Shane
  • 705
  • 3
  • 7
  • 19
  • 1
    What have you done to debug this? What is the value of `$result`? What is the value of `$row`? What is the value of `$stage_name`? What is the value of `$stage_name['stage_name']`? `print` and `print_r` are your friends. – Quentin Jun 30 '16 at 17:59
  • 3
    **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the latest version of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Jun 30 '16 at 18:00
  • 1
    How is this not working? Have you checked to see what's actually in `$row['stage_name']`? You might want to check `$stage_name == 'Shortlisting'`, too – andrewsi Jun 30 '16 at 18:00
  • The value of stage_name is Shortlisting, so it should show as `Shortlisting` – Shane Jun 30 '16 at 18:00
  • 2
    fetch_assoc() will return an associative array. Try that instead of mysql_fetch_array() – Radmation Jun 30 '16 at 18:00
  • @user3092953 — No. Not what value *should* it be, what value is it actually? You need to inspect it (e.g. with `print_r`). – Quentin Jun 30 '16 at 18:00
  • Are you _assuming_ it has that value, or have you _verified_ it? – CBroe Jun 30 '16 at 18:00
  • 1
    Using `$stage_name['stage_name']` does not make sense. `$stage_name` should not be an array. – Don't Panic Jun 30 '16 at 18:01
  • [**How do I get PHP Errors to display?**](http://stackoverflow.com/q/1053424/4577762) – FirstOne Jun 30 '16 at 18:02
  • 1
    @Radmation, actually, [mysql_fetch_array](http://php.net/manual/en/function.mysql-fetch-array.php): _Fetch a result row as an associative array, a numeric array, or both_. _MYSQL_BOTH (default)_. It should work anyway... – FirstOne Jun 30 '16 at 18:03

1 Answers1

1

Its variable type mistake. Check your assigned variable, you assigned the Array Element not the entire array. so try like below.

<?php
  $result = mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1") or die('ERROR 315' );
  $row = mysql_fetch_array($result);
  $stage_name = $row['stage_name'];

  if($stage_name == 'Shortlisting') { 
    echo"Shortlisting"; 
  } else {
    echo"Not Shortlisting";
  }
?>

Refer this Article for PHP Array understanding.
http://php.net/manual/en/language.types.array.php

Venkat.R
  • 6,616
  • 5
  • 34
  • 57