-3

Hello I am trying to look after content of Selected query... but however the error appears:

mysqli_num_rows() expects parameter 1 to be mysqli_result ...

      $stmt= $this->conn->prepare("SELECT * FROM table") or die(mysql_error());
        $result = $stmt->execute();


        // check for empty result
        if (mysqli_num_rows($result) > 0) {

    ..
    }

EDIT NEW ERROR mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in...

    $stmt= $this->conn->prepare("SELECT * FROM table") or die(mysqli_error());

$stmt->execute();

        $stmt->store_result();

// check for empty result
if ($stmt->num_rows > 0) {
    // looping through all results

    $response["array"] = array();

    while ($row = mysqli_fetch_array($stmt)) { 

... }

I think I got it:

    while ($row =  $stmt->fetchAll()) {
        // temp user array
        $array= array();
        $array["bla"] = $row["bla"];
... }

but I get only "null" as value...?

EDIT

Now i got it:

$stmt->bind_result($column1, $column2...)

then

$array["bla"] = $column1; $array["bla2"] = $column2;

but isn't it possible to bind_result all colums without to put every single one into a variable? or isnt it possible to use this :

$array["bla"] = $row["bla"];

so it puts the value from the row with the column "bla" into $array[bla]?

because the way i solved it, seems to be veyr difficult

Steve
  • 127
  • 1
  • 10

1 Answers1

-1

Correct is

   $stmt= $this->conn->prepare("SELECT * FROM table") or die(mysql_error());
        $result = $stmt->execute();
        $result->store_result();

        // check for empty result
        if ($result->num_rows > 0) {

    ..
    }
Carca
  • 564
  • 1
  • 6
  • 16
  • can you explain me what exactly $result = $stmt->execute(); $result->store_result(); does? the first result get's the content from SELECT query right? But what does the second line mean? – Steve Dec 10 '14 at 19:23
  • You can do the following: `$stmt = $this->conn->query("SELECT * FROM table"); if($stmt->num_rows >= 1) { return true; } else { return false; }` Or if you want to use a prepared statement (which isn't necessary for this query) do the following: `$stmt = $this->conn->prepare("SELECT * FROM table"); // No need to bind_params $stmt->execute(); $stmt->store_result(); if($stmt->num_rows >= 1) { return true; } else { return false; }` – SuperDJ Dec 10 '14 at 19:24
  • Transfers the result set from the last query on the database connection => https://php.net/manual/ro/mysqli.store-result.php – Carca Dec 10 '14 at 19:24
  • @SuperDJ okay - how to deal with mysqli_fetch_array($stmt){ } this also does not work :S Error: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in... – Steve Dec 10 '14 at 19:44
  • @Steve this depends on how you called the query. With standard `mysqli_query()` or `prepared` or OOP `query`? – SuperDJ Dec 10 '14 at 19:52
  • @SuperDJ I've did it like Look Edit in table.. thx u – Steve Dec 10 '14 at 22:28
  • @Steve I tought it should be `$stmt->fetch_array()`. Have a look at: http://php.net/manual/en/mysqli-result.fetch-array.php for more information. I have bad experience with `prepared` statements and `$stmt->fetch_*`. I almost always tend to go with `$stmt->query()` and then the result of that is easilly used with `$stmt->fetch_*`. Note: `$stmt->_fetch_assoc()` or `$stmt->fetch_object()` are a bit easier to use since you can just call a result by column name. – SuperDJ Dec 11 '14 at 09:25
  • hmm ok i will try it with fetch_array. I changed to prepared statements, because some guys told me here it would be more secure to use this^^ – Steve Dec 11 '14 at 16:54
  • You have an error. [`mysql_error()`](https://www.php.net/manual/en/function.mysql-error.php) worked only for the old API. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 31 '19 at 23:14