I am fairly new to php and mysql as I have been trying to teach myself. I am used to Classic ASP and am adjusting to php but I am struggling with doing a Query within a Query.
<?php
$con = mysqli_connect("localhost","username","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sel_query="Select * from accounts ORDER BY name asc;";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result))
{
$refname = intval($row["referrer"]);
$sql = "Select name from accounts where userid = '$refname' LIMIT 1";
$username = mysqli_query($con,$sql)
$row2 = mysql_fetch_assoc($username);
echo $row2;
}
I am trying to perform a query of the entire table and then as I go though each row, I query the same table again to pull a name based on a referrer column.
How do I properly do this in php/mysql? Thank you
2
Answers
Using mysql_fetch_assoc was the problem. I didn't realize it was there and now have php error reporting turned on and that helps greatly.
You’re missing the
;
at the end of your$username = mysqli_query($con,$sql)
line.Corrected Version:
$username = mysqli_query($con,$sql);