clown[NOR] Posted April 17, 2007 Share Posted April 17, 2007 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 More sharing options...
clown[NOR] Posted April 17, 2007 Author Share Posted April 17, 2007 wow... i'm a really smart person =) haha.. placing the mysql_real_escape_string() outsice the mysql connection? *punching himself* Link to comment https://forums.phpfreaks.com/topic/47416-solved-help-fetch_assoc-returns-nothing/#findComment-231374 Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 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 Link to comment https://forums.phpfreaks.com/topic/47416-solved-help-fetch_assoc-returns-nothing/#findComment-231375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.