-2
<pre>
$query = "select * from user WHERE username = '$username'";
                    $query_run = mysqli_query($con,$query);
</pre>

When I execute the program it show me this message. Undefined variable: con in

Ali
  • 1
  • 2
  • 3
    so where is your $con variable? – Viktar Pryshchepa Mar 04 '19 at 14:35
  • 3
    Please add the whole code because in your snippet we can't see any tentative of variable initialization – ka_lin Mar 04 '19 at 14:36
  • 1
    We need a LOT more information here. Firstly, the snippet you have posted is not valid PHP because it does not contain the PHP delimiters . Second, we need to verify that your connection is indeed set up correctly. Finally, I highly advise you to do some research on PDO and SQL injection as using Mysqli_query is no longer considered best practice. – Russ J Mar 04 '19 at 15:06

1 Answers1

0

I do not know what your trying to query but based on your code snippet, you are using mysqli and you are making selection based on username. The code below will automatically connect you to database if database credential entered are okay and you can get results based on the queried username if the username info exist on your database

<html>
   <head>
      <title></title>
   </head>

   <body>
      <?php
         $dbhost = 'localhost:3306';
         $dbuser = 'root';
         $dbpass = '';
         $dbname = 'your db';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
         echo 'Connected successfully<br>';
         $sql = "select * from user WHERE username = '$username'";
         $result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
               echo "UserName: " . $row["username"]. "<br>";
            }
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
      ?>
   </body>
</html>
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38