2

All this section is supposed to do is collect an int from active and use it in the if statement to continue with the code. Can somebody please tell me why this is not working?

$act_qry = "Select active FROM user_m WHERE username = '$username' and password = '$Menrypted_password'";
$result_act = mysqli_query ( $connMS, $act_qry );
$value_act = mysqli_fetch_object($result_act);

if($value_act == 1)
{
    //Do php stuff.
}
wogsland
  • 8,299
  • 18
  • 54
  • 83
GenGen
  • 103
  • 1
  • 12
  • 3
    Change like:-`if($value_act->active == 1) { //Do php stuff. }` – Anant Kumar Singh Jan 23 '16 at 02:43
  • Use **var_dump($value_act);** to see what is being returned – user2182349 Jan 23 '16 at 02:51
  • var_dump($value_act) = object(stdClass)#5 (1) { ["active"]=> string(1) "1"} – GenGen Jan 23 '16 at 03:11
  • @GenGen DID YOU TRIED WHAT I SUGGESTED TO YOU – Anant Kumar Singh Jan 23 '16 at 03:19
  • @A-2-A Can you show me that code in procedural style, oriented style doesnt click with me. – GenGen Jan 23 '16 at 03:43
  • If you don't want object oriented then don't fetch as an object...`mysqli_fetch_object`.. Also use prepared statements in the future, that could be an issue for you in the future.. – chris85 Jan 23 '16 at 04:28
  • @chris85 http://stackoverflow.com/questions/1530868/simple-explanation-php-oop-vs-procedural You can use procedural for mysqli_fetch_object http://php.net/manual/en/mysqli-result.fetch-object.php – GenGen Jan 23 '16 at 04:31
  • @GenGen What you linked shows the usage `$value_act->active`... so what is the question then? Note the usage of `$obj` in example #1 and #2 on the manual's page. – chris85 Jan 23 '16 at 04:35

1 Answers1

2

I am having this table in my database:-http://prntscr.com/9tnalx

Check this code:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
//connect to database
$link = mysqli_connect('localhost','root','','stack');
$act_qry = "Select product_id FROM bom WHERE bom_description = 'Table Tops'";
$result_act = mysqli_query ( $link, $act_qry ) or die(mysqli_error($link));

$value_act = mysqli_fetch_object($result_act);

if($value_act->product_id == 1)
{
    echo $value_act->product_id;
}
// or you can do this 

$value_act = mysqli_fetch_assoc($result_act);

    if($value_act['product_id'] == 1)
    {
        echo $value_act->product_id;
    }
    mysqli_close($link);
    ?>

Output on my browser:- http://prntscr.com/9tnazj

Note:- I hope you can understand the code by checking my screenshot of table.Thanks

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
  • if($value_act->product_id == 1) How would I write that using procedural style? – GenGen Jan 23 '16 at 04:17
  • Although I dont fully understand the $value_act->product_id it does work what I needed it to. Thank you @A-2-A. – GenGen Jan 23 '16 at 04:35
  • @GenGen Note the `mysqli_fetch_assoc` is returning an array. When you ask for an object, `mysqli_fetch_object`, you get an object. – chris85 Jan 23 '16 at 04:38
  • 1
    Thanks @GenGen. Since `$value_act` is an object so if you want to get any property of that object then you have to do `$object name->property name`. I added another one in my code also – Anant Kumar Singh Jan 23 '16 at 04:39