Problem with php query to pull value

Here is my code.

<?php $user = $_GET["user"]; $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "web"; $conn = new mysqli($servername, $username, $password, $dbname); $sql = "SELECT `password` FROM `accounts` WHERE user='$user'"; $result = mysql_query($conn($sql)); while($row = mysqli_fetch_array($result)) echo $row['user']; $conn->close(); ?>

Trying to pull a value from a table with a variable, but no matter what I try it doesn’t seem to work.

Two issues.

  1. You start with mysqli then switch to mysql_ function. DON’T use mysql_.

  2. You are not using prepared statements. It is very important that you do.

Your issue is actually easily remedied, but because it coincides with something that needs to be changed anyway, I’ll leave you to correct the major problem, before the minor.

Like stated above use either mysqli or PDO, but NOT mysql.

Second turn errors by either putting this on top of the page, putting this in a configuration file of some sort, or editing php.ini file.

[php]<?php
/* Turn on error reporting */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(-1);[/php]

This should help you out and also look at the user’s comments they’ll should give examples of scripts that will show you how to do what you want to do.
http://php.net/manual/en/mysqli-result.fetch-array.php

Sponsor our Newsletter | Privacy Policy | Terms of Service