Trying to get specific data

I have a database setup something like this

someid | age | title | zipcode

2101 35 Agent 12345
3507 46 Broker 23456
7889 21 Intern 34567

What I am trying to do is get the information from a specific row, if the input from a webpage matched the ID of one in the database. Fromthe table above if I search for ID 3507, then I want to see all of the info on that row: ID: 3507, Age: 46, Title: Broker, Zipcode 23456.
I can get the info for the entire database (“SELECT * FROM mydatabase”) but can someone point me in the direction of being able to compare the form input to the first column ID and if it matches echo the complete row?
A copy of what I have coded is below.

Thanks for any help in advance.
(Yep, I’m a noob at MySql)

Jim

<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydatabase";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

$findthis = mysqli_real_escape_string($conn, $_REQUEST['key']);

$sql = "SELECT someid FROM mydatabase";
if ($sql = $findthis)
{
$sqla = "SELECT someid, age, title, zipcode FROM mydatabase WHERE someid = $findthis"; echo "ID: " . $row["someid"]. "\nAge: " . $row["age"]. "\nTitle: " . $row["title"]. "\nZip: " . $row["zipcode"]. "\n\n";

}
else
{
echo "0 results";
}

$conn->close();
?>

OK, there are multiple problems:

  • $findthis comes from nowhere, or at least you are not showing what that could be
  • you are using mysqli_real_escape_string what is better than nothing, but i would recommend using Prepared Statements so you don’t have to use that over and over again, and more readability leads to more understandable programs: MySQLI Prepared Statements
  • a minor enhancement: set your MySQLi to throw errors so you have to use less conditions and may later use a configuration value: mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  • $sql = $findthis this is an assignment, you are not checking anything real, check the manual: comparison operators
  • at least you haven’t said what your problem is, you have a proper WHERE condition in your statement. What you you tried?
Sponsor our Newsletter | Privacy Policy | Terms of Service