Jump to content
xisto Community
lonebyrd

Wondering If I'm Having A Code Problem Or If Its My Database

Recommended Posts

I'm still trying to figure out my login/registration page. It's probably a problem with my database itself. I have come up with this error on my webpage:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/lonebyrd/public_html/registration.php on line 93

This is the code for that line:
if(mysql_num_rows(mysql_query($nameq)) > 0)

Before that is probably what matters so I will give that too. Here it is:

elseif(isset($_POST['register'])) {			$name = mysql_real_escape_string($_POST['ad_name']);			$email = mysql_real_escape_string($_POST['ad_email']);			$nickname = mysql_real_escape_string ($_POST['ad_nickname']);			$password = mysql_real_escape_string($_POST['ad_password']);			$location = mysql_real_escape_string($_POST['ad_location']);			$gender = mysql_real_escape_string ($_POST['ad_gender']);			$date_of_birth = mysql_real_escape_string ($_POST['ad_date_of_birth']);						$activation_code = generateCode(25);			$nameq = "SELECT ad_name FROM registration WHERE ad_name = $name LIMIT 1";			$emailq = "SELECT ad_email FROM registration WHERE ad_email = $email LIMIT 1";			$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = $nickname LIMIT 1";			}			//put errors into an array I need to change these if I change the db			if(empty($name)) {				$errors[] = "The name field was blank! <br />";			}			if(mysql_num_rows(mysql_query($nameq)) > 0) 			{	$errors[] = "The name given is already in use! Please try another one! <br />";

Like I said, it's probably a problem in the way my database is set-up but, I am very new to all this.

Share this post


Link to post
Share on other sites

It's a code problem. mysql_num_rows requires, like any mysql command, tat you be connected to a mysql server and have picked a database. So first you must use mysql_connect() and then mysql_select_db();~Viz

Share this post


Link to post
Share on other sites

and Viz saves the day again...lol. I was about to tell him the same thing but you beat me to it...But ya Viz is correct if you make/update those entries then you should be able to fix your problem. If you keep gettting errors let us know.

Share this post


Link to post
Share on other sites

I have my database info in different files. But at the top of the script, I have it try to locate it. Maybe I am doing it wrong. Here is what it says:

<?phprequire("config.php");include("login.php");require("functions.php");?>

Share this post


Link to post
Share on other sites

Without seeing the various scripts and the main page in its entirety this will be incredibly difficult to solve. Please, post all relevant code. Unless you are absolutely sure (would stake your programming career) that a piece of code is not the cause, include it. This includes any external files that are accessed by the script.~Viz

Share this post


Link to post
Share on other sites

Ok.. I'm a newbie at this so I'll give you all the coding. Here are the scripts for the files given at the beginning.

config.php

<?php$nickname = "lonebyrd";$password = "mypassword";$database = "lonebyrd_FTV";$server = "localhost";?>

login.php
<?phpmysql_connect($server, $nickname, $password) or die (mysql_error());mysql_select_db($database) or die (mysql_error());?>

functions.php
<?phpfunction generateCode($length = 10){   $password="";   $char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";   srand((double)microtime()*1000000);    for ($i=0; $i<$length; $i++)    {	  $password = $password . substr ($chars, rand() % strlen($chars), 1);    }    return $password; } ?>

registration.php
<?phprequire("config.php");include("login.php");require("functions.php");?><HTML><HEAD><TITLE>Registration---Fantasized TeleVision</TITLE><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><style type="text/css">BODY {	background:#808080;	text-align:center;	font-family:Verdana, Arial;	font-weight:bold;	font-size:12px;	color:#FF0000;	}	   register_box	{	border: 1px solid #0000A0;	background-color: #0000FF;	font-family: Verdana, Arial;	font-weight: bold;	font-size: 9px;	color: #FF0000;	top margin: 200px	left margin: 200px	}</style></head><body><?php// The following line verifies which action is requested by the user// by default is setting to new if nothing is requested$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "new";switch($action) {	//--------------------------------------	//	   [New Registration]	//--------------------------------------	  	  case "new":		if(!isset($_POST['register'])) {			echo "			<form action='registration.php' method='POST'>				Name: <br />				<input type='text' name='name' class='register_box'>				<br />				Email: <br />				<input type='text' name='email' class='register_box'>				<br />				Nickname: <br />				<input type='text' name='nickname' class='register_box'>				<br />				Password: <br />				<input type='password' name='password' class='register_box'>				<br />				Location: <br />				<input type='text' name='location' class='register_box'>				<br />				Gender: <br />				<input type='text' name='gender' class='register_box'>				<br />				Date_of_Birth: <br />				<input type='date' name='date_of_birth' class='register_box'>				<br />				<input type='submit' name='register' value='New Registration!' class='register_box'>				<input type='hidden' name='action' value='new'>			</form>			";		}		elseif(isset($_POST['register'])) {			$name = mysql_real_escape_string($_POST['ad_name']);			$email = mysql_real_escape_string($_POST['ad_email']);			$nickname = mysql_real_escape_string ($_POST['ad_nickname']);			$password = mysql_real_escape_string($_POST['ad_password']);			$location = mysql_real_escape_string($_POST['ad_location']);			$gender = mysql_real_escape_string ($_POST['ad_gender']);			$date_of_birth = mysql_real_escape_string ($_POST['ad_date_of_birth']);						$activation_code = generateCode(25);			$nameq = "SELECT ad_name FROM registration WHERE ad_name = $name LIMIT 1";			$emailq = "SELECT ad_email FROM registration WHERE ad_email = $email LIMIT 1";			$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = $nickname LIMIT 1";			}			//put errors into an array I need to change these if I change the db			if(empty($name)) {				$errors[] = "The name field was blank! <br />";			}			if(mysql_num_rows(mysql_query($nameq)) > 0) 			{	$errors[] = "The name given is already in use! Please try another one! <br />";			}			if(empty($email)) {					$errors[] = "The email field was blank! <br />";			}			if(mysql_num_rows(mysql_query($emailq)) > 0) {				$errors[] = "The email given is already in use! Please try another one! <br />";			}						if(empty($nickname)) {				$errors[] = "The nickname field was blank! <br />";			}			if(mysql_num_rows(mysql_query($nicknameq))>0) {				$error[] = "That nickname is already taken.  Please try another one.<br />";			}						if(empty($password)) {				$errors[] = "The password field was blank! <br />";			}			if(empty($location)) {				$errors[] = "The location field was blank! <br />";			}			if(empty($gender)) {				$errors[] = "The gender field was blank! <br />";			}			if(empty($date_of_birth)) {				$errors [] = "No date of birth was entered! <br />";			}			if(count($errors) > 0) {			  foreach($errors as $err) {				echo $err;			}			}			else {			  $sqlq = "INSERT INTO registration (ad_name, ad_email, ad_nickname, ad_password, ad_location, ad_gender, ad_date_of_birth, is_activated, activation_code)";			  $sqlq .= " VALUES ('$name', '$email', '$nickname', '".md5($password)."', '$location', '$gender', '$date_of_birth', '0', '$activation_code')";			  mysql_query($sqlq) or die(mysql_error());			  echo "Thanks for registering! You will recieve an email shortly containing your validation code, and a link to activate your account!";			  mail($email, "New Registration, ftv.astahost.com;, "Thanks for registering on FTV.\n\nHere are your login details:\n\nNickname: ".$nickname."\n\nPassword: ".$password."\n\nIn order to login and gain full access, you must validate your account.\n\nClick here to validate:\n\nhttp://forums.xisto.com/no_longer_exists/;;			  // \n is equal to a new line// in your original version you use the variable user to send the nickname data// to your activation file and in that file you check for a variable nickname, //so it never work.		}		}	 ?></body></html>

I also have an activation.php file for after the registration process. If you have any suggestions, or see anything wrong with this, please let me know.

Share this post


Link to post
Share on other sites

Perhaps I'm overlooking something, but mysql_num_rows is apparently not receiving the correct object. I'm guessing it's a problem with your query. I'm fairly 'noobish' at this as well but if you're using a text field in your database or charvar, then your statements field should be enclosed in quotes. Correct me if I'm wrong here...and I may be, but:

$nameq = "SELECT ad_name FROM registration WHERE ad_name = $name LIMIT 1";			$emailq = "SELECT ad_email FROM registration WHERE ad_email = $email LIMIT 1";			$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = $nickname LIMIT 1";
Would that work? or would this work?
$nameq = "SELECT ad_name FROM registration WHERE ad_name = '$name' LIMIT 1";$emailq = "SELECT ad_email FROM registration WHERE ad_email = '$email' LIMIT 1";$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = '$nickname' LIMIT 1";

I usually do this when working with variables, because I encounter errors when variables are in quotes. Some variables work, but larger more complex variables, and superglobals often times have trouble.

$nameq = "SELECT ad_name FROM registration WHERE ad_name = '" .  $name . "' LIMIT 1";$emailq = "SELECT ad_email FROM registration WHERE ad_email = '" . $email . "' LIMIT 1";$nicknameq = "SELECT ad_nickname FROM registration WHERE ad_nickname = '" . $nickname . "' LIMIT 1";

Put an 'OR DIE' at the end of your query, or just echo out your query and copy and paste it into your SQL command prompt in phpMyAdmin and see if it executes properly.

Share this post


Link to post
Share on other sites

copy and paste it into your SQL command prompt in phpMyAdmin and see if it executes properly.

The old good tricks which stil work well : that's usually named "debugging" ; write down the queries and see if they give a correct result, and then put them in a program. Oh, remembering the old good ancient times! :D

Share this post


Link to post
Share on other sites

Ok, I've tried the two different ways of placing the quotes, but it was still the same results. The other things, adding 'OR DIE' and echoing out the query and putting it into a sql command prompt, i just dont understand. But one thing I don't understand is, I went through and put in all the data into my registration page on my website, and it said thank you for registering, there was no errors there. I'm lost.

Share this post


Link to post
Share on other sites

It must the queries. Syntax-wise there's nothing wrong with them but it must be something. I recommend you print out errors generated by mysql_query, it might help to solve the issue or better yet do as the guys before proposed, run the queries directly in the database.You can run the queries easily from phpMyAdmin or from command line if you have SSH access. Or you could just create a small PHP test page that connect to the database and run the queries and print out errors. Just don't use any user input, just fixed values that will give some results. In this kind of situations debugging is usually the best option. Staring and analyzing your code tends to be quite hard. I know from experience that I tend to screw up with SQL and usually I create the query, run it on the database and then insert it into the code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.