PHP Select from MYSQL with Variable


Hi, my below select statement is not outputting anything. I have used the same syntax on other statements that are working.

The issue is with the variable $driver1 as when i replace this with test data, the statement outputs.

I have also echoed out $driver1, outside of the SQL statement and thats also fine. Thanks in advance.

$sql9 = “SELECT impedance FROM drivers WHERE drivername = ‘$driver1’”;
$result9 = (mysqli_query($link, $sql9));

while ($imp1 = $result9->fetch_assoc()) {
$impedance1 = $imp1[‘impedance’]."


The variable probably contains some non-printing or white-space characters. Use var_dump($driver1); to see what length the value is in addition to what it appears like.

Next, your code contains some practices that are insecure or are causing unnecessary typing on your part.

You should NOT put external/unknown data directly into an sql query. Use a prepared query, with a ? place-holder for each value, then supply the data when the query gets executed. Unfortunately, the php mysqli extension is overly complicated and inconsistent when dealing with prepared queries and you should switch to the much simpler php PDO extension. A prepared query, using the PDO extension, actually simplifies the sql query statement (the variable, any single-quotes around the value, and any concatenation dots are all removed) and only adds one extra php statement.

For a query that will only match one row of data, don’t use a loop to fetch the data. Just directly call a single fetch statement. You should also detect if the query did match any data/fetch the row and output an appropriate message in the html document.

Putting () around the mysqli_query… statement is unnecessary and is just cluttering up your code.

Using variables that end in numbers indicates you are either writing too much code/queries to accomplish a task or you are not finishing with one task before going onto the next. Any time you find yourself creating variables that end in numbers, it is a sign you are doing something in the hardest way possible. Within the context of the code for one task, use simple/common variable names. You only need to assign the resultant fetched data to a uniquely named variable. Once you do this, you will begin to see that there is common code for the same tasks. You can then write functions/classes to handle the implementation of these tasks and further simplify your main application code.

Your variable assignment statement is concatenating a br tag onto the end of the value. Your code that is responsible for retrieving data should not contain any html markup. You should only have html markup in the code that’s responsible for producing the output from the data. Something like this is probably what is causing the $driver variable to not match a row in the query.

You are also mixing mysqli procedural with OOP notation. While this works, OOP notation is shorter, less typing. This problem will go away when you switch to the PDO extension, since it only uses OOP notation.

Making use of these practices, this is all the code/typing you need for the task shown in this thread -

$sql = "SELECT impedance FROM drivers WHERE drivername = ?";
$stmt = $pdo->prepare($sql); // PDO connection in $pdo
$impedance = $stmt->fetchColumn(); // this will either be a false value if there was no data to fetch or the impedance value from the query

Pull records by Year dropdown list

Thats very useful. Thanks so much!