Jump to content

[SOLVED] Help: fetch_assoc returns nothing


clown[NOR]

Recommended Posts

I've been working on this one for quite a while now, but I cant figure out why nothing is returned from the fetch_assoc().. All the places I've placed an echo to check where it is in my code returns fine...

 

<?php

include('../config/db.php');

$username = $_POST['brukernavn'];
$password = $_POST['passord'];

$username = mysql_real_escape_string(trim($username));
$password = mysql_real_escape_string(trim(md5($password)));

global $dbHost, $dbUser, $dbPass, $dbName;
if (!mysql_connect($dbHost, $dbUser, $dbPass)) { die("Unable to connect to database"); }

echo "Connection OK<br>";

if (!mysql_select_db($dbName)) { die("Unable to select database"); }

echo "Selection OK<br>";

$query = "SELECT * FROM users WHERE username = '" . $username . "'";
$result = mysql_query($query);

if (!$result) { die("Could not run query from database"); }

echo "Query OK<br>";

$dbField = mysql_fetch_assoc($result);

echo "Fetch_Assoc OK<br>";

print_r($dbField);

/*$user = $dbField['username'];
$pass = $dbField['password'];

if ($password === $pass) {
	echo "Du er logga inn";
} else { echo "Feil passord<br><br>Username: ".$user."<br>Password: ".$pass; } */

?>

Link to comment
https://forums.phpfreaks.com/topic/47416-solved-help-fetch_assoc-returns-nothing/
Share on other sites

Just because the query executes ok, doesn't mean that any rows are returned. You can check that by using the mysql_num_rows() function after the query:

<?php
$query = "SELECT * FROM users WHERE username = '" . $username . "'";
$result = mysql_query($query) or die("Could not run query from database, query:<pre>$query</pre><br>" . mysql_error());
echo "Query OK<br>";
        if (mysql_num_rows($result) > 0) {
  	      $dbField = mysql_fetch_assoc($result);
      echo "Fetch_Assoc OK<br>";
      echo '<pre>' . print_r($dbField,true) . '</pre>';
        } else
              echo 'No records retrieved from the database';
?>

 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.