-1

I have two table.

  1. newuser (id,office_id,username)

  2. offices (office_id,office_name)

When user login I got his office_id

  • I want to fetch his office_name from offices table

The session is working :

if(isset($_SESSION['office']))
{
    $id_office = $_SESSION['office'];
}

I wrote this code to fetch office_name but dose not work

$query =("SELECT office_name FROM offices WHERE office_id = '$id_office'");
            $result=mysqli_query($con,$query);
            if($result->num_rows > 0)
{
        $office_name = $_POST['office_name'];
}
Sehdev
  • 5,486
  • 3
  • 11
  • 34
med med
  • 13
  • 5
  • 1
    I'm quite sure this is the problematic part: `$office_name = $_POST['office_name'];`. I don't know how to do it in `mysqli`, but `$_POST` does not contain the result of your query – Carl Binalla Nov 07 '18 at 03:09
  • What happens with the code? `$result` would need to be fetched (http://php.net/manual/en/mysqli-result.fetch-array.php) to get a result. You also should use parameterized queries. – user3783243 Nov 07 '18 at 03:11
  • I suggest finding some video tutorials, they will help you more then a few way word comments on SO, Not that my fellow posters aren't great (I have the utmost respect for them). Just you can only convey so much info in the format that SO provides. – ArtisticPhoenix Nov 07 '18 at 03:20
  • 2
    Your code is open to [SQL injection](https://stackoverflow.com/q/332365/2469308) related attacks. Even [`real_escape_string`](https://stackoverflow.com/a/12118602/2469308) cannot secure it completely. Please learn to use [Prepared Statements](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Madhur Bhaiya Nov 07 '18 at 04:16

1 Answers1

1

You're not actually fetching the result. Try changing the if block to fetch the result using fetch_assoc():

if($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $office_name = $row['office_name'];
}

Note that $_POST contains data submitted to the PHP page by a form, not the results of MySQL queries.

Nick
  • 138,499
  • 22
  • 57
  • 95