0

I used to make $sql and execute it directly with no control and now I'm swapping to prepared statements, I've made one on my localhost but the moment I tried it on a live server. This is what it used to look like before

<?php 
                    $sql = "SELECT * FROM tbl_front WHERE active='Yes'";
                    $res = mysqli_query($conn, $sql);
                    if($res==TRUE)
                    {
                        $count = mysqli_num_rows($res); 
                        if($count>0)
                        {
                            while($row=mysqli_fetch_assoc($res))
                            {
                                $day1=$row['day1'];
                                $day2=$row['day2'];
                                $time1=$row['time1'];
                                $time2=$row['time2'];
                                
                   echo "
                   <h2 class='hours'>RADNO VRIJEME:</h2>
                   <h2 class='hours'>$day1 - $day2 <br>$time1:00 - $time2:00</h2>";
                            }}
                        else
                        {
                            echo "
                   <h2 class='hours'>Privremeno zatvoreno</h2>";
                        }}
                   ?>

Works perfect, and now I've changed it to this

           $sql = "SELECT * FROM tbl_front WHERE active=?";
                    $stmt = mysqli_stmt_init($conn);
                    if(!mysqli_stmt_prepare($stmt, $sql)){
                        echo 'lol';
                    }
                    else{
                        mysqli_stmt_bind_param($stmt, "s", "Yes");
                        mysqli_stmt_execute($stmt);
                        $res = mysqli_stmt_get_result($stmt);
                        $count = mysqli_num_rows($res); 
                        if($count>0)
                        {
                            while($row=mysqli_fetch_assoc($res))
                            {
                                $day1=$row['day1'];
                                $day2=$row['day2'];
                                $time1=$row['time1'];
                                $time2=$row['time2'];
                    
                   echo "
                   <h2 class='hours'>RADNO VRIJEME:</h2>
                   <h2 class='hours'>$day1 - $day2 <br>$time1:00 - $time2:00</h2>";
                            }}
                        else
                        {
                            echo "
                   <h2 class='hours'>Trenutno zatvoreno</h2>";
                        }}
                   ?>

I don't get crashes but I can't get output, I tried without the $count or changing it to $count=mysqli_stmt_num_rows($res); but i simply can't draw data out of the database, not even with print_r($res); Any ideas on how I might be able to solve this?

aynber
  • 20,647
  • 8
  • 49
  • 57
MonkeyDPhp
  • 31
  • 2
  • I would have thought the call to `mysqli_stmt_bind_param()` would generate some message as you should be passing in variables and not constants. – Nigel Ren Dec 29 '21 at 15:37
  • @NigelRen - that's assuming they have error reporting tuned on... I prefer PDO over mysqli anyway, it's very ugly to me. – ArtisticPhoenix Dec 29 '21 at 15:39
  • @ArtisticPhoenix does anyone prefer mysqli? I think all the questions using it are just copy/pasting 10 year old tutorials. – miken32 Dec 29 '21 at 17:28

1 Answers1

-1

In order to get more verbose output from mysqli, you can add this to your code before making the connection ($conn).

   mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

This will report errors to the error log more verbose than the default setting and display what is causing the error on the live server.

tomjack
  • 37
  • 3