How to cancel a select statement when a WHERE parameter is empty or does not exist? [duplicate]












-1
















This question already has an answer here:




  • check if row exists with mysql

    3 answers



  • “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

    27 answers




I hope someone can help me on this? Here's my select statement based on a session variable.



$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';


I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?



Any help on how this is done would be very much appreciated.



Thank you.
Steve










share|improve this question















marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 22:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    -1
















    This question already has an answer here:




    • check if row exists with mysql

      3 answers



    • “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

      27 answers




    I hope someone can help me on this? Here's my select statement based on a session variable.



    $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';


    I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?



    Any help on how this is done would be very much appreciated.



    Thank you.
    Steve










    share|improve this question















    marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner php
    Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 1 at 22:29


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -1












      -1








      -1









      This question already has an answer here:




      • check if row exists with mysql

        3 answers



      • “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

        27 answers




      I hope someone can help me on this? Here's my select statement based on a session variable.



      $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';


      I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?



      Any help on how this is done would be very much appreciated.



      Thank you.
      Steve










      share|improve this question

















      This question already has an answer here:




      • check if row exists with mysql

        3 answers



      • “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

        27 answers




      I hope someone can help me on this? Here's my select statement based on a session variable.



      $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';


      I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?



      Any help on how this is done would be very much appreciated.



      Thank you.
      Steve





      This question already has an answer here:




      • check if row exists with mysql

        3 answers



      • “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

        27 answers








      php database if-statement session






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 21:52









      Funk Forty Niner

      1




      1










      asked Jan 1 at 21:39









      Steven CheshireSteven Cheshire

      105




      105




      marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner php
      Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 1 at 22:29


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner php
      Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 1 at 22:29


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.



          $users = ;

          if (isset($_SESSION['user'])) {
          $sql = "SELECT * FROM wbcusers WHERE username = ?";
          $stmt = $pdo->prepare($sql);
          $stmt->execute([$_SESSION['user']]);
          $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
          }


          P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.






          share|improve this answer


























          • Perfect, thanks Bill, worked a treat. Much appreciated. Steve

            – Steven Cheshire
            Jan 2 at 21:49



















          -1














          if (isset($_SESSION['user'])) {
          $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
          ....
          }
          else
          {
          //something else
          }





          share|improve this answer
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.



            $users = ;

            if (isset($_SESSION['user'])) {
            $sql = "SELECT * FROM wbcusers WHERE username = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$_SESSION['user']]);
            $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
            }


            P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.






            share|improve this answer


























            • Perfect, thanks Bill, worked a treat. Much appreciated. Steve

              – Steven Cheshire
              Jan 2 at 21:49
















            0














            Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.



            $users = ;

            if (isset($_SESSION['user'])) {
            $sql = "SELECT * FROM wbcusers WHERE username = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$_SESSION['user']]);
            $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
            }


            P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.






            share|improve this answer


























            • Perfect, thanks Bill, worked a treat. Much appreciated. Steve

              – Steven Cheshire
              Jan 2 at 21:49














            0












            0








            0







            Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.



            $users = ;

            if (isset($_SESSION['user'])) {
            $sql = "SELECT * FROM wbcusers WHERE username = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$_SESSION['user']]);
            $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
            }


            P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.






            share|improve this answer















            Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.



            $users = ;

            if (isset($_SESSION['user'])) {
            $sql = "SELECT * FROM wbcusers WHERE username = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$_SESSION['user']]);
            $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
            }


            P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 2 at 21:52

























            answered Jan 1 at 21:43









            Bill KarwinBill Karwin

            380k64518672




            380k64518672













            • Perfect, thanks Bill, worked a treat. Much appreciated. Steve

              – Steven Cheshire
              Jan 2 at 21:49



















            • Perfect, thanks Bill, worked a treat. Much appreciated. Steve

              – Steven Cheshire
              Jan 2 at 21:49

















            Perfect, thanks Bill, worked a treat. Much appreciated. Steve

            – Steven Cheshire
            Jan 2 at 21:49





            Perfect, thanks Bill, worked a treat. Much appreciated. Steve

            – Steven Cheshire
            Jan 2 at 21:49













            -1














            if (isset($_SESSION['user'])) {
            $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
            ....
            }
            else
            {
            //something else
            }





            share|improve this answer






























              -1














              if (isset($_SESSION['user'])) {
              $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
              ....
              }
              else
              {
              //something else
              }





              share|improve this answer




























                -1












                -1








                -1







                if (isset($_SESSION['user'])) {
                $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
                ....
                }
                else
                {
                //something else
                }





                share|improve this answer















                if (isset($_SESSION['user'])) {
                $users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
                ....
                }
                else
                {
                //something else
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 1 at 21:53

























                answered Jan 1 at 21:43









                SimonareSimonare

                14.8k11840




                14.8k11840















                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas