I’m using PHP to access a MYSQL database and I’m needing to extract data from a table. The problem is that there is only one field to be fetched.
$mysqli = new mysqli($hostname, $username, $password, $db_name);
// Output error info if there was a connection problem
if ($mysqli->connect_errno)
die("Failed to connect to MySQL: ($mysqli->connect_errno) $mysqli->connect_error");
$db = "SELECT username1 FROM Friends WHERE username2 = '"."$username"."' UNION SELECT username2 FROM Friends WHERE username1 = '"."$username"."'";
$result = $mysqli->query($db) or
die ("Wasn't able to search the database");
while($result->fetch_field($row))
{
echo "<h1> $row </h1>";
}
I guarantee that I am connected to my database. I also used $result->fetch_field() instead of $row in the parentheses. My main problem is that fetch_field returns an object but in all of the manuals on PHP they don’t say how you extract the actual value that the field has.
Are you sure that your variable
$usernamein your query is what you expect it to be? YourSELECTcould be returning0rows.You are using
$usernametwice in your code.First in your
mysqli–And again in your
SELECT–Try adding