How to handle result from mysqli_query

Just wondering what I’m doing wrong with this code, I’m sure it’s the mysqli_fetch_row line but I’m lost. if I change the $id variable in the $sql query to an actual user id, the query works… so obviously my first query isn’t handling the result correctly… tips anyone? $username is declared earlier


$id = mysqli_query($link, "SELECT id FROM users WHERE username = '$username'");
$id = mysqli_fetch_row($id);
if (!$id)
		{
			$error = 'Error!';
			include 'error.php';
			exit();
		}
else
$sql = mysqli_query($link, "SELECT moviename FROM movies INNER JOIN usermovies ON movie_id = usermovies.movieid WHERE usermovie.userid = '$id'");
if (!$sql)
		{
			$error = 'Error!';
			include 'error.php';
			exit();
		}
while ($row = mysqli_fetch_array($sql))
		{
			$movies[] = array('movie_name' => $row['movie_name']);
		}
?>

$result = mysqli_query($link, “SELECT id FROM users WHERE username = ‘$username’”);
$row = mysqli_fetch_row($result);
if (!$row)
{
$error = ‘Error - user does not exist’;
include ‘error.php’;
exit();
}
$id = $row[‘id’];

changed it to that, but now getting ‘Notice: Undefined index: id’

hmm

Try changing mysqli_fetch_row to mysqli_fetch_assoc

woo, it’s working! thanks so much. :slight_smile: