Forum Moderators: open

Message Too Old, No Replies

Newbie to MySQL help debugging error

         

cstickman

7:41 pm on Mar 7, 2009 (gmt 0)

10+ Year Member



I have a new account with lunarpages and I am having problems with the database. It runs great on my server at home that I use to test, but not when I make it live with them.

I have 3 php files one named index.php (which is the login page)
2nd file - checklogin.php
3rd file - login_success.php

I believe the index.php is fine I have a form that its action is set to checklogin.php

Here is what I have for the checklogin.php page


<?php
$host="localhost"; // Host name
$username="lpusername_username"; // Mysql username
$password="password"; // Mysql password
$db_name="lpusername_siteusers"; // Database name
$tbl_name="web_members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
?>
then my html stuff

I have set up a user and when I click on submit it goes to the checklogin.php and I get this error at the top of the page:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mylpusername/public_html/checklogin.php on line 26

So what am I doing wrong?

phranque

12:24 am on Mar 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], cstickman!

you should first test if $result is FALSE and if so check the error that was returned.

cstickman

6:13 pm on Mar 9, 2009 (gmt 0)

10+ Year Member



Okay I was able to solve the problem with your help thank you very much. I had my database user permissions set wrong. I only had him be able to update, delete and insert I didnt have the select permission on. I thought that was a security risk if you gave your database user that many permissions? Am I wrong in thinking this? Also how can I take that login script and make it more secure? I see some posts talk about using sha1 or md5 and I have no idea how to implement that. Any suggestions would be helpful. Thanks