0

I am trying to create a script that gets the varible in the browser URL p and querys the colums playername for anything matching varible p but it still doesnt work anyone know what im doing wrong ive been fiddling with this for hours..

<!DOCTYPE html>
<html lang="en">
 <head>
    <meta charset="utf-8">
    <title>Admin Panel</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link href="http://example.com/assets/css/bootstrap.css" rel="stylesheet">
    <link href="http://example.com/assets/css/docs.css" rel="stylesheet">
    <link href="http://example.com/assets/js/google-code-prettify/prettify.css" rel="stylesheet">

<center>

<?php

$con = mysql_connect("","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("log", $con);

$plyr=$_GET["p"];

$result = mysql_query('SELECT * FROM logs_chat WHERE playername="$plyr"');

echo '
            <table class="table">
              <thead>
                <tr>
                  <th>Time</th>
                  <th>Player</th>
                  <th>Message</th>
                </tr>
              </thead>
              <tbody>
';

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['time'] . "</td>";
  echo "<td>" . $row['playername'] . "</td>";
  echo "<td>" . $row['text'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

</center>
    <script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
    <script src="http://example.com/assets/js/jquery.js"></script>
    <script src="http://example.com/assets/js/google-code-prettify/prettify.js"></script>
    <script src="http://example.com/assets/js/bootstrap-transition.js"></script>
    <script src="http://example.com/assets/js/bootstrap-alert.js"></script>
    <script src="http://example.com/assets/js/bootstrap-modal.js"></script>
    <script src="http://example.com/assets/js/bootstrap-dropdown.js"></script>
    <script src="http://example.com/assets/js/bootstrap-scrollspy.js"></script>
    <script src="http://example.com/assets/js/bootstrap-tab.js"></script>
    <script src="http://example.com/assets/js/bootstrap-tooltip.js"></script>
    <script src="http://example.com/assets/js/bootstrap-popover.js"></script>
    <script src="http://example.com/assets/js/bootstrap-button.js"></script>
    <script src="http://example.com/assets/js/bootstrap-collapse.js"></script>
    <script src="http://example.com/assets/js/bootstrap-carousel.js"></script>
    <script src="http://example.com/assets/js/bootstrap-typeahead.js"></script>
    <script src="http://example.com/assets/js/bootstrap-affix.js"></script>
    <script src="http://example.com/assets/js/application.js"></script>
</body>
</html>
3
  • Is it normal that you don't close your head tag and don't open your body tag? Commented Dec 10, 2012 at 2:50
  • Heads up! Future versions of PHP are deprecating and removing the mysql_ family of functions. It looks like you're still learning PHP, meaning now would be a great time to switch to PDO or mysqli. Commented Dec 10, 2012 at 3:03
  • Please learn to use parametrized queries. What you have now leaves you open to SQL injection. bobby-tables.com/php.html has examples. Commented Dec 10, 2012 at 3:08

2 Answers 2

1

Problem is on here,

$result = mysql_query('SELECT * FROM logs_chat WHERE playername="$plyr"');

$plyr is considered as string not variable, PHP will not parse anything which is single quoted ' '. You need to concat the variable.

Change it to,

$result = mysql_query('SELECT * FROM logs_chat WHERE playername="'.$plyr.'"');

Update:

Don't use mysql_* function, they will be deprecated soon. Use PDO or mysqli for connecting database. Beware of SQL INJECTIONS, $_GET["p"] is not validated and possible weakness in your code. Try prepared statements or mysqli_real_escape_string or PDO::quote.

Sign up to request clarification or add additional context in comments.

6 Comments

also, there is no check here for SQL injection
What else could I possibly use for that function then?
@cegfault I was updating. Check now.
@MuthuKumaran: perpared statements work, are not as fast as regular queries if they are only being used once (ie, not in a loop). mysqli_real_escape_string or PDO::quote would probably be better
@cegfault: Far better to take whatever microsecond speed hit you claim than to use tainted data when building SQL statements. Parametrized queries are the only way to go.
|
1

Have you tried to put single quotes around the names of the tables?

form: $result = mysql_query('SELECT * FROM logs_chat WHERE playername="$plyr"');

to: $result = mysql_query("SELECT * FROM logs_chat WHERE playername='$plyr'");

also dont forget to use mysql_real_escape_string when getting $_GET variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.