1

I have the following query :

'SELECT Active FROM tbUsers WHERE Id=55'

The Id is unique and I need just to know the status of the user if he's active yes or no. The Column Active is set as boolean in Mysql

When I tried to return the result like the following (using another php function) :

$result = $this->selectRow($db,"tbClass","Active","Id='$Id'");
if($result) { return "ok" ; } else { return "nok" ;)

it returns 'ok' on both cases. Any idea what's wrong with it ?

Here is the other function :

public function selectRow($db,$tableName,$field,$where) {
        if($where == "") {
            $query = "SELECT $field FROM $tableName";
        }
        else
        { $query = "SELECT $field FROM $tableName WHERE $where"; }

        $result=$db->Qry($query);


        if ($result) {
            $no_of_rows =  $db->TotRows($result);
            if($no_of_rows == 1) {
                return $result;
            }
            if($no_of_rows == 0) {
                return '';
            }
            if($no_of_rows < 0) {
                die('Invalid query: ' . $sender ."(".$query  ."): ".mysql_errno().": ". mysql_error());
                return '';
            }
        } 
        else {
            die('Invalid query: ' . $sender .": " .$query.": ". mysql_errno().": ". mysql_error());
            return '';
        }
    }

Qry Function is the following :

function Qry($sql) {
    if($result = mysqli_query($this->con,$sql) ) {
        return $result;
    }
    else 
    {
        $err = "Error: ".$sql. " :: ". mysqli_error;
        die("$err");
    }
}
4
  • What is $db an instance of? Also, you said "OK" is being returned in both instances, but you've only given one instance, Id = 55. What's the other instance and which should return OK? Commented Jun 2, 2015 at 9:43
  • You have a class in $db that is not a standard class of PHP, it's somthing you developed, so I can't really know what Qry or ToRows does. If I need to guess, ToRows returns the array of the result, so you need to check if count($result) == 1 and not $result. Commented Jun 2, 2015 at 9:43
  • right i have a class db which does : Execute Query and Record Count Commented Jun 2, 2015 at 11:32
  • @Styphon : if Modify the Active Column to true(1) the result is the same if that column is set to False(0). Commented Jun 2, 2015 at 11:36

2 Answers 2

1

I think you need to change this condition .

 $result=$db->Qry($query);

always return you query object

Just remove this condition and

You need to count number of affected row

<?php

public function selectRow($db, $tableName, $field, $where) {
    if ($where == "") {
        $query = "SELECT $field FROM $tableName";
    } else {
        $query = "SELECT $field FROM $tableName WHERE $where";
    }

    $result = $db->Qry($query);



    $no_of_rows = $db->TotRows($result);
    if ($no_of_rows == 1) {
        return $result;
    }
    if ($no_of_rows == 0) {
        return FALSE;
    }
    if ($no_of_rows < 0) {
        die('Invalid query: ' . $sender . "(" . $query . "): " . mysql_errno() . ": " . mysql_error());
        return FALSE;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i agree with you that's an object but how to return the value of the object not the object itself
use $result->fetch_object() to get object
0

Alright, so $result is a mysqli_result object.
You might want to fetch the first row of that result and return the desired column.

Replace

return $result;

with

return $result->fetch_assoc()[$field];

2 Comments

I have added this : if(is_array($result)) echo "array"; And it does show anything so it's NOt an array.
please have a look in main block to see what the QRY function does

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.