0

I'm trying to get the mysqli_fetch_assoc() to work with my $stmt, but couldn't manage it to find any working method. The base code that I'm trying to do is to get the latitudine and longitudine from the table, 1 row at a time so I have implemented Haversine Formula to calculate the distances:

$stmt = $conn->prepare("SELECT latitudine, longitudine FROM alerte WHERE tip_problema = ?");
                $stmt->bind_param("s", $problema);
                $stmt->execute();   
                $stmt->store_result();                  
                if($stmt->num_rows>0){  
                    while($row = $stmt->mysqli_fetch_assoc()) {
                        $row[latitudine]= $latitudine1;
                        $row[longitudine]= $longitudine2;
                        ...(rest of while);
                    }

but using this code, I got the error that says the mysqli_fetch_assoc() has to be used with mysqli_result , not mysqli_stmt, so I started to change the code to achieve the mysqli_result :

$stmt = $conn->prepare("SELECT latitudine, longitudine FROM alerte WHERE tip_problema = ?");
                $stmt->bind_param("s", $problema);
                $stmt->execute();   
                $stmt->store_result();                  
                if($stmt->num_rows>0){
                    $result = $stmt->get_result();  
                    while($row = $result->mysqli_fetch_assoc()) {
                        $row[latitudine]= $latitudine1;
                        $row[longitudine]= $longitudine2;
                        ......
                    }

but once again, I get an error :

Fatal error: Uncaught Error: Call to a member function mysqli_fetch_assoc() on bool in ...

I tried to get out the $stmt->store_result; that's before the if statement but it generates this error:

Fatal error: Uncaught Error: Call to a member function execute() on bool in ..

The last major try was this:

$stmt = $conn->prepare("SELECT latitudine, longitudine FROM alerte WHERE tip_problema = ?");
                $stmt->bind_param("s", $problema);
                $stmt->execute();   
                $result = $stmt->get_result();          
                if($stmt->num_rows>0){
                    while($row = $result->mysqli_fetch_assoc()) {
                        $row[latitudine]= $latitudine1;
                        $row[longitudine]= $longitudine2;
                        ....
                    }

And here, I do not receive any error, but the code gets past if($stmt->num_rows>0) , thus not retrieving the rows 1 by 1.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Costin
  • 194
  • 1
  • 2
  • 16
  • what is the value of `$problema`? – jibsteroos Jan 10 '20 at 08:48
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Jan 10 '20 at 08:49
  • See here https://stackoverflow.com/a/59619050/1839439 – Dharman Jan 10 '20 at 08:50
  • 3
    Why do you even bother with `num_rows`? Use `fetch_all` – Dharman Jan 10 '20 at 08:51
  • 1
    Now you need to explain why `if($stmt->num_rows>0){` is even there. Just remove this useless condition and everything would work – Your Common Sense Jan 10 '20 at 08:52
  • @your Everything except `mysqli_fetch_assoc`. The function is called `fetch_assoc` – Dharman Jan 10 '20 at 08:53
  • @jibsteroos value of $problema it's used to retrieve only some rows. It's a string – Costin Jan 10 '20 at 08:55
  • @Dharman , @your I need the `num_rows` to make the code do something else if there are no entries in the table, returned by the `SELECT` statement. I have also tried the `fetch_assoc` function, but still getting the same errors – Costin Jan 10 '20 at 08:57
  • you need to figure out why `$stmt->execute();` returns `false` – jibsteroos Jan 10 '20 at 08:58
  • Which is why I recommend to fetch all records into an array instead. – Dharman Jan 10 '20 at 08:58
  • `"I need the num_rows to make the code do something else"` - you have $row for the purpose – Your Common Sense Jan 10 '20 at 09:06
  • @YourCommonSense I deleted that if and i got this error: `Fatal error: Uncaught Error: Call to undefined method mysqli_result::mysqli_fetch_assoc()` – Costin Jan 10 '20 at 09:13
  • 2
    Like @Dharman already [said](https://stackoverflow.com/questions/59678034/php-statement-with-mysqli-fetch-assoc-not-working?noredirect=1#comment105514127_59678034), **the function is called `fetch_assoc()`**, not mysqli_fetch_assoc() – Your Common Sense Jan 10 '20 at 09:15
  • 1
    @Costin I think you are getting confused between the "procedural" syntax and the "object-oriented" syntax. The [documentation](https://www.php.net/manual/en/mysqli-result.fetch-assoc.php) gives clear examples of each style. In your code you are using the object-oriented style, therefore the function name is simply "fetch_assoc()" because you are calling it from the $result object, not as a global function. – ADyson Jan 10 '20 at 09:40
  • @YourCommonSense Right, my bad. Can anyone tell me other way to test if a variable is different from `NULL` ? I tried `!is_null($variable)` but it's not working – Costin Jan 10 '20 at 09:45
  • @ADyson Thanks for the link, it really helped me. Yes, as a beginner in `PHP` I got confused. – Costin Jan 10 '20 at 09:46
  • 2
    is_null() is one of the ways and *of course* it works. And now you need to explain why do you think some variable is null. – Your Common Sense Jan 10 '20 at 09:47
  • @Costin No problem. FYI As a beginner especially the PHP documentation should really be the first place you are looking to get clarity on usage of functions etc! – ADyson Jan 10 '20 at 09:54

0 Answers0