title: Is this PDO code safe for the public to look up information on a database?
body: <p>I'm trying to make a very simple lookup page for the members at my studyspace. Every member has a personal ID number, their registered name (and other information) and the number of points/credits they have left on their account.</p>
  
  <p>I wanted to make a public page with two search fields where the user can lookup how many points/credits are remaining on their account if they input their user ID and some bit of personal information like their last name.</p>
  
  <p>This would query a database and show their credits.</p>
  
  <p>My main concern is security of course, since it will be a public website available to anyone. I got the sample code from a tutorial because I am a very beginner and just know enough to change the code from the tutorial to suit my database.</p>
  
  <p>Here's the search form page code:</p>
  
  <pre><code>&lt;form action="search.php" method="post"&gt;
  ID: &lt;input type="text" name="searchid" placeholder=" Search here ... "/&gt;&lt;br&gt;
  Name: &lt;input type="text" name="searchname" placeholder=" Search here ... "/&gt;
  &lt;input type="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And then the code to query and display results:</p>
  
  <pre><code>&lt;?php 
  //load database connection
  $host = "localhost";
  $user = "root";
  $password = "passwordhere";
  $database_name = "test";
  $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
  PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION
  ));
  // Search from MySQL database table
  $searchid=$_POST['searchid'];
  $searchname=$_POST['searchname'];
  $query = $pdo-&gt;prepare("select * from members where name LIKE '$searchname' &amp;&amp; id LIKE '$searchid'  LIMIT 0 , 10");
  $query-&gt;bindValue(1, "%$searchid%", PDO::PARAM_STR);
  $query-&gt;execute();
  // Display search result
       if (!$query-&gt;rowCount() == 0) {
              echo "Search found :&lt;br/&gt;";
              echo "&lt;table style=\"font-family:arial;color:#333333;\"&gt;";  
              echo "&lt;tr&gt;&lt;td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\"&gt;name&lt;/td&gt;&lt;td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\"&gt;id&lt;/td&gt;&lt;td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\"&gt;last visit&lt;/td&gt;&lt;/tr&gt;";               
          while ($results = $query-&gt;fetch()) {
              echo "&lt;tr&gt;&lt;td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\"&gt;";            
              echo $results['name'];
              echo "&lt;/td&gt;&lt;td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\"&gt;";
              echo $results['id'];
              echo "&lt;/td&gt;&lt;td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\"&gt;";
              echo $results['lastvisit'];
              echo "&lt;/td&gt;&lt;/tr&gt;";              
          }
              echo "&lt;/table&gt;";        
      } else {
          echo 'Nothing found';
      }
  ?&gt;
  </code></pre>
  
  <p>The column named last visit is just for testing, this will be the part with the point information.</p>
  
  <p>The code seems to work fairly well with some dummy data and simple testing. I changed the code to require both fields to match otherwise they return no result so people cannot just try to enter in easy to guess data to try to get results. If one field is empty it will return nothing, if the data is slightly wrong it will also return nothing. However, entering in the user ID with name perfectly returns all that information for that line.</p>
  
  <p>So naturally the first questions is if this code is safe from mySQL attacks?
  Or if there are any other glaring problems with it?</p>
  
  <p>Also, is it common to actually connect to a database with the user root and the password in plain text like that? That doesn't seem so secure to me but every example I have seen with using PHP to access databases has that code.</p>
  
  <p>Another question I had, for my own learning experience, is the purpose of this code:</p>
  
  <pre><code>$query-&gt;bindValue(1, "%$searchid%", PDO::PARAM_STR);
  </code></pre>
  
  <p>I couldn't quite figure it out, yet.</p>
  
  <p>Thanks for any help.</p>