Hey, DaniWeb friends!
I've a problem with my login.php: When an error occurred, the

mysql_error();

doesn't appear the error. Tested the

mysql_errno()

also, it appears as 0, saying it doesn't have any errors. Take a look at the script:

<?php

$username = $_POST['username'];
$pass = $_POST['password'];

require "conf/connection.php";
require "layout/nav.php";

$result = mysql_query("SELECT * FROM users WHERE uname = '$username' AND password = '$pass'");
$total = mysql_num_rows($result);
$r = mysql_fetch_array($result);
if ($total == 0){
   echo 'An error occurred: ' . mysql_error() . 'The error code: ' . mysql_errno();
} else {
     session_start();
    $_SESSION['id'] = $r['id'];
    $_SESSION['uname'] = $r['uname'];
    $_SESSION['password'] = $r['password']; 
    header("Location: logged.php");
}
?>

Have you tried:

$result = mysql_query("SELECT * FROM users WHERE uname = '$username' AND password = '$pass'") or die (mysql_error());

phorce is right. This is the quote from the manual: "mysql_error() Returns the text of the error message from previous MySQL operation". The previous being mysql_fetch_array .

This logic would make more sense:

$result = mysql_query("SELECT * FROM users WHERE uname = '$username' AND password = '$pass'");
if ($result)
{
  $total = mysql_num_rows($result);
  if ($total == 0) 
  {
    echo 'No results for your query.';
  } 
  else 
  {
    session_start();
    $r = mysql_fetch_array($result);
    $_SESSION['id'] = $r['id'];
    $_SESSION['uname'] = $r['uname'];
    $_SESSION['password'] = $r['password']; 
    header("Location: logged.php");
  }
  mysql_free_result($result);
}
else
{
  echo mysql_error();
}
?>

Priteas, but for what exactly serves the mysql_free_result i don't understand by the manual, it's confusing!

phorce is right. This is the quote from the manual: "mysql_error() Returns the text of the error message from previous MySQL operation". The previous being mysql_fetch_array .

This logic would make more sense:

$result = mysql_query("SELECT * FROM users WHERE uname = '$username' AND password = '$pass'");
if ($result)
{
  $total = mysql_num_rows($result);
  if ($total == 0) 
  {
    echo 'No results for your query.';
  } 
  else 
  {
    session_start();
    $r = mysql_fetch_array($result);
    $_SESSION['id'] = $r['id'];
    $_SESSION['uname'] = $r['uname'];
    $_SESSION['password'] = $r['password']; 
    header("Location: logged.php");
  }
  mysql_free_result($result);
}
else
{
  echo mysql_error();
}
?>
Member Avatar for diafol

It clears the $result resource from memory.

@Pritaeas
Is this necessary for tiny resources? I've read conflicting stuff about this. Some say use it in everything, while others claim it uses up more memory if you use it on small resources.

I've learned PHP3 from the start with putting it in, because in some cases it overflowed my mysql connection count without it. Ever since then, I put it in. Never looked back to see if it is still required.

I'd love to know how much memory they are talking about. Personally I prefer decent code over performance, until it proves necessary.

Member Avatar for diafol

Fair point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.