Jump to content

MySQL with PHP...


JP128

Recommended Posts

PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource...


<?php
include "config.php";
checkConfirm(); //Confirms passwords match.
checkEmpty(); //Checks for empty forms.
dbConnect(); //Connects to DB
$username = $_POST['username'];
$email = $_POST['email'];
$sql = "select * from registry where user='$username'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);[b] // <=-=-=-=- This is where I get the error...
[/b]
if ($num_rows = 0) {

mysql_query("insert into registry values ('$firstname', '$lastname', '$username', MD5('$password'), '$email'");

} //end of if
Link to comment
Share on other sites

[!--quoteo(post=387356:date=Jun 23 2006, 09:49 PM:name=hackerkts)--][div class=\'quotetop\']QUOTE(hackerkts @ Jun 23 2006, 09:49 PM) [snapback]387356[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]$sql = "select * from registry where user='$username'";[/code]

You need to specify which column.
[code]$sql = "select firstname from registry where user='$username'";[/code]
[/quote]
no you don't. * selects all columns. firstname just selects firstname.

why don't you add this red part here:

$result = mysql_query($sql)[!--coloro:red--][span style=\"color:red\"][!--/coloro--] or die(mysql_error());[!--colorc--][/span][!--/colorc--]

i bet it might give you some useful hints as to what's wrong.
Link to comment
Share on other sites

so you get no error from that? okay, then add this:

$sql = "select * from registry where user='$username'";
[!--coloro:red--][span style=\"color:red\"][!--/coloro--]echo $sql;[!--colorc--][/span][!--/colorc--]

this will echo the actual query string being sent to the db. maybe $username is empty, or not the value you expect.
Link to comment
Share on other sites

Going back to your original post, you said:
[code]<?php
$sql = "select * from registry where user='$username'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result); // <=-=-=-=- This is where I get the error...
?>[/code]
That mean that the mysql_query did not return a meaningful result. Change the query line to be:
[code]<? $result = mysql_query($sql) or die('Problem with the query: ' . $sql . '<br>' . mysql_error()); ?>[/code]
You script will then stop if there is an error and display both the error message and your query.

Ken
Link to comment
Share on other sites

I re-created the entire script. I dont get an error. I know my script is running, and will tell the errors, because I changed the table name, and it says it doesnt exist. So then I turned it back. I connect to the DB, and select the tables. But it wont do anything more than that.
Link to comment
Share on other sites

you must be doing something wrong, as in, not following our instructions. you say that you know it connects to the db fine, cuz you change the parameters to some random thing and you get error messages. so obviously your script is being run. therefore i see no reason why doing

$sql = 'select * ...';
echo $sql;

does not echo out anything. that does not make sense, unless you just aren't doing it, or somehow doing like echo $someothervarname; or something.
Link to comment
Share on other sites

This is in CONFIG.PHP

<?php
function dbConnect() { //Start of dbConnect
mysql_connect("localhost", "jp128", "****");
mysql_select_db("jp128");
} //End of dbConnect

function registerCheck(){
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$email = $_REQUEST['email'];
dbConnect();
$output = mysql_query("SELECT * FROM registry WHERE username=$username OR email=$email");
$output_rows = mysql_num_rows($output);
if($output_rows = 0){
mysql_query("INSERT INTO registry VALUES('$firstname','$lastname','$username','$password','$email'");
} elseif ($output_rows > 1){
echo "MYSQL ERROR. IS MORE THAN ONE OF THESE USERNAMES. PLEASE CONTACT webmaster@jp128.mooo.com!";
} elseif ($output_rows = 1){
echo"I am sorry, but that username or email address already exists in the database. If this is an error, please contact webmaster@jp128.mooo.com.";
}
}
?>


This is REGISTERCHECK.php

<?php
include "config.php";
registerCheck();
?>
Link to comment
Share on other sites

omg where to begin...
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
This is in CONFIG.PHP

<?php
function dbConnect() { //Start of dbConnect
mysql_connect("localhost", "jp128", "****");
mysql_select_db("jp128");
} //End of dbConnect

function registerCheck(){
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$email = $_REQUEST['email'];
dbConnect();
$output = mysql_query("SELECT * FROM registry WHERE username=[!--coloro:red--][span style=\"color:red\"][!--/coloro--]'[!--colorc--][/span][!--/colorc--]$username[!--coloro:red--][span style=\"color:red\"][!--/coloro--]'[!--colorc--][/span][!--/colorc--] OR email=[!--coloro:red--][span style=\"color:red\"][!--/coloro--]'[!--colorc--][/span][!--/colorc--]$email[!--coloro:red--][span style=\"color:red\"][!--/coloro--]'[!--colorc--][/span][!--/colorc--]");
$output_rows = mysql_num_rows($output);
if($output_rows =[!--coloro:red--][span style=\"color:red\"][!--/coloro--]=[!--colorc--][/span][!--/colorc--] 0){
//assuming these are the right column names
mysql_query("INSERT INTO registry [!--coloro:red--][span style=\"color:red\"][!--/coloro--](firstname, lastname, username, password, email) [!--colorc--][/span][!--/colorc--]VALUES('$firstname','$lastname','$username','$password','$email'");
[!--coloro:red--][span style=\"color:red\"][!--/coloro--]//strictly speaking, you shouldn't be scripting this elseif > 1
// in the first place. you should have it setup like,
// if (blah > 0) { 'name already taken' } else { insert info } [!--colorc--][/span][!--/colorc--]
} elseif ($output_rows > 1){
echo "MYSQL ERROR. IS MORE THAN ONE OF THESE USERNAMES. PLEASE CONTACT webmaster@jp128.mooo.com!";
[!--coloro:red--][span style=\"color:red\"][!--/coloro--]should be an else, not an elseif[!--colorc--][/span][!--/colorc--]
} [!--coloro:red--][span style=\"color:red\"][!--/coloro--]else[!--colorc--][/span][!--/colorc--] ($output_rows =[!--coloro:red--][span style=\"color:red\"][!--/coloro--]=[!--colorc--][/span][!--/colorc--] 1){
echo"I am sorry, but that username or email address already exists in the database. If this is an error, please contact webmaster@jp128.mooo.com.";
}
}
?>


This is REGISTERCHECK.php

<?php
include "config.php";
registerCheck();
?>
[/quote]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.