0

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");
}
?>

Edited by gcarvalho: n/a

4
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by diafol
1

Have you tried:

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

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();
}
?>

Edited by pritaeas: n/a

0

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();
}
?>
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.