Jump to content

code not working when hosted


mariam

Recommended Posts

initially i used this code on xampp and it worked just fine.

now when i have used a free web hosting site (000webhost.com) created the same database there the followin error occurs:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a3907930/public_html/checklogin.php on line 18

 

Wrong Username or Password

 

 

following is the code for my check login page: (with $username="root" and $password= "" this worked fine in xampp)

the username and password in the below code was generated when the database was created in www.000webhost.com

 

<?php
$host="xxxxxx"; // Host name 
$username="xxxxxx"; // Mysql username 
$password="xxxxxx"; // Mysql password 
$db_name="xxxxxx"; // Database name 
$tbl_name="xxxxxx"; // Table name

// Connect to server and select databse.
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'];
$sql="SELECT * FROM $tbl_name WHERE username='$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");
  }
  else {
   echo "Wrong Username or Password";
   }
?>

 

MOD EDIT: code tags added, DB credentials removed.

 

Link to comment
https://forums.phpfreaks.com/topic/237219-code-not-working-when-hosted/
Share on other sites

When posting code, enclose it within the forum's

 . . . 

BBCode tags.

 

You have no logic to report MySQL errors resulting from a failed query.

session_register() is deprecated.

You must call session_start() before anything is output to the browser if you will be using $_SESSION variables.

I've made some changes and added comments in the code below.

 

<?php
session_start();
$host="xxxxxx.webhost.com"; // Host name
$username="xxxxxx"; // Mysql username
$password="xxxxxx"; // Mysql password
$db_name="xxxxxx"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
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'];
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
if( $result=mysql_query($sql) ) { // if query executes successfully . . . 
   // 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"
      // DEPRECATED FUNCTION --> session_register("myusername");
      $_SESSION['myusername'] = $myusername;
      // DEPRECATED FUNCTION --> session_register("mypassword");
      $_SESSION['mypassword'] = $mypassword;
      header("location:login_success.php");
   } else {
      echo "Wrong Username or Password";
   }
} else {  // if query fails. (Change this to a generic "Sorry" error message for a live web site)
   echo "<br>Query string $sql<br>Failed with error: " . mysql_error() . '<br>';
}

[/code]

thanks for going through all the trouble of reading and correcting the code :) but it's still not working. The following error occurs.

 

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a3907930/public_html/checklogin.php on line 19

if($count==1){ $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("location:login_success.php"); } else { echo "Wrong Username or Password"; } else { echo "

Query string $sql

Failed with error: " . mysql_error() . '

'; } ?>

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.