0

I have code below, how can echo (print) value result from query select. I would like echo my result where the text echo "HERE"???? is. I tried to use echo and print but neither working.

try
     {
       $polaczenie = new mysqli($host, $db_user, $db_password, $db_name);
      if ($polaczenie->connect_errno!=0) 
        {
          throw new Exception(mysql_connect_errno());
        }
        else
            {

              mysqli_set_charset($polaczenie,"utf8"); //wrzuca poprawnie polskie znaki
              mysqli_set_charset($polaczenie,"SET NAMES `utf8` COLLATE `utf8_polish_ci`");//wrzuca poprawnie polskie znaki
                      //wszystko ok -> polecenie select                           
                       if (($polaczenie->query("SELECT zdjecie_1 FROM profile_photos WHERE login='test' AND zdjecie_1_status=1")))
                       {
                          echo "HERE"????
                          }
                          //wszystko ok polecenie select sie udało
                       }
                      else
                      {
                        throw new Exception($polaczenie->error);
                      }
                }
                $polaczenie->close();
        }              
          catch(Exception $error_polaczenia)
          {
            $_SESSION['error_bazy_danych']="Błąd serwera!";
            echo '<br />Info developer: '.$error_polaczenia; //ZAWSZE UKRYTE! DLA DEVELOPERA
        }
Scath
  • 3,777
  • 10
  • 29
  • 40
Marko sean
  • 13
  • 3
  • 2
    Just to clarify, do you (A) want to know how to echo the results of an SQL query in PHP, or (B) think there's something wrong with the code above and want help debugging it? – user234461 May 03 '18 at 12:57
  • option A, code is ok but i dont know how can I echo result from SQL I tried print($polaczenie->query); and echo $polaczenie->query and $polaczenie; not working – Marko sean May 03 '18 at 13:00
  • 2
    Possible duplicate of [Display SQL query results in php](https://stackoverflow.com/questions/20300582/display-sql-query-results-in-php) – user234461 May 03 '18 at 13:03
  • 1
    You have extra curly braces after echo. please remove it. – Jinandra Gupta May 03 '18 at 13:05
  • i removed but it wasn't this – Marko sean May 03 '18 at 13:13
  • You want how you can print the result. Right? I guess modified code work for you. Please check. – Jinandra Gupta May 03 '18 at 13:25
  • Are you sure that the execution reaches that code block? Try adding more echo commands farther up and in different branches. – David Hempy May 03 '18 at 14:15
  • Also, please edit your question, and add what you *are* getting as output currently. Are any of the exceptions being raised? Can you simplify the example down to just 2 or 3 lines of code to demonstrate the problem in a smaller sample? – David Hempy May 03 '18 at 14:17

1 Answers1

0

You have one curly braces extra after echo. Please remove or check the code below.

try
{
    $polaczenie = new mysqli($host, $db_user, $db_password, $db_name);
    if ($polaczenie->connect_errno!=0) 
    {
        throw new Exception(mysql_connect_errno());
    }
    else
    {

        mysqli_set_charset($polaczenie,"utf8"); //wrzuca poprawnie polskie znaki
        mysqli_set_charset($polaczenie,"SET NAMES `utf8` COLLATE `utf8_polish_ci`");//wrzuca poprawnie polskie znaki


        //wszystko ok -> polecenie select
        $profileRes = $polaczenie->query("SELECT zdjecie_1 FROM profile_photos WHERE login='test' AND zdjecie_1_status=1");
        if ($profileRes)
        {
            echo "<pre>";
            print_r($profileRes);
            exit;
            //wszystko ok polecenie select sie udało
        }
        else
        {
            throw new Exception($polaczenie->error);    
        }
    }

    $polaczenie->close();
}
catch(Exception $error_polaczenia)
{
    $_SESSION['error_bazy_danych']="Błąd serwera!";
    echo '<br />Info developer: '.$error_polaczenia; //ZAWSZE UKRYTE! DLA DEVELOPERA
}
Jinandra Gupta
  • 546
  • 2
  • 9
  • my mistake with an extra } but this does not solve the problem, I'm stating whether using the try catch function I can use the select command and display the result using an echo – Marko sean May 03 '18 at 13:08
  • You want how you can print the result. Right? I guess modified code work for you. Please check. – Jinandra Gupta May 03 '18 at 13:19