I want to check my database to see if any user has a confirmcode identical to one a registering user enters on their registration form. However I keep getting the error:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/elight5/public_html/test/student_register.php on line 15"
From what I can make out, that means there is an undefined variable but I know the values in the db are named appropriately.....Is there something wrong with my syntax? I have been using the same syntax across the site with no issues....
Any suggestions or errors about/in my code?
function confirmcode_exists($confirmcode){
$confirmcode = sanitize($confirmcode);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `confirmcode` = '$confirmcode'"), 0) == 1) ? true : false;
}
The error for mysql_result() is stating that your MySQL query is failing.
Your actual SQL query does not look syntatically incorrect, which most likely means that either the table, or a column, is mispelled, or your connection doesn't have access to that table, or possibly that the connection is invalid altogether.
Try updating your code to the following; this should show you what your actual error is:
$result = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `confirmcode` = '$confirmcode'");
if (!$result) {
// query error
die('Error: ' . mysql_error());
}
return (mysql_result($result, 0) == 1);
Related
i had the problem when i want to check my valid key on my database and showing this error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\ravaaa\indexx.php on line 20
the db is login, tables is users (5) id,name,key,login
this the code
$error='';
$key="123";
$con = mysqli_connect("localhost","root","","login");
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
echo "sukses\n\n";
$query = mysqli_query($con, "SELECT * FROM users WHERE key='$key'");
$rows = mysqli_num_rows($query);
if($rows == 1){
echo "key is valid";
}
else
{
$error = "key is Invalid";
echo $error;
}
mysqli_close($con);
Are you sure your table have an column named "key"? Key is a reserved MySQL Keyword, and when i tried to create a table with this column to test i got an error, and after change the name "key" to another name (on the table and on the .php) i got no errors, just the message "key is Invalid".
Try to change the column name "key" and change on the php too, and check if you still get errors.
The column name key is a reserved word and therefore must be escaped using backticks:
select * from users WHERE `key` = '$key'
Without the escape, the query will fail and return FALSE per the mysqli_query docs:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will return a mysqli_result object. For
other successful queries mysqli_query() will return TRUE.
You can exit early with an error using the or die pattern:
mysqli_query($con, "SELECT * FROM users WHERE `key` = '$key'")
or die(mysqli_error($con));
I'm creating a few lines to check if a user exist within the database. To do this, I was going to just find the username in the DB and if there IS a user with that name in the database use num_rows to make it show that their is a user with that name.
The error is:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given in /home/bluef/public_html/SMNS/register.php on line 36
Code:
$usernamef = mysqli_query($link, "SELECT * FROM Users
WHERE Username =".$Username."");
$usernamefound = mysqli_num_rows($usernamef);
if($usernamefound != 0){
echo "Username in use, try another username?";
}
Always have this line before mysqli_connect
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Always format your queries correctly.
Always use prepared statements when you need to insert a variable into query.
Always check out the "Related questions" section on the page (Or suggested questions while writing your own).
Try this with your query
$usernamef = mysqli_query($link, "SELECT * FROM Users
WHERE Username =".$Username."") or die(mysqli_error());
to see the error. Also you can try this
$usernamef = mysqli_query($link, "SELECT COUNT(*) AS myCount FROM Users
WHERE Username =".$Username."") or die(mysqli_error());
$row = mysqli_fetch_array($usernamef)
if( $row['myCount '] > 0 )
{
echo "Username in use, try another username?";
}
Check this link http://www.w3schools.com/php/func_mysqli_error.asp
Also you can try with mysql_query and mysql_num_rows() and not with mysqli_query()
I've searched this issue high and low. I've also seen it asked in many places but never to the point in which i am having the issue. Nor I have found a solution that has fixed mine. I've spent a very long time trying to fix this and I can not move forward until it has been resolved any help would be GREATLY appreciated.
It's a login & register tutorial by phpacademy.
My error is as follows:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\lr\core\functions\users.php on line 5
The code I have for my users.php file is as follows and is 100% the same as is on the tutorial. Mine fails to work his does not.
<?php
function user_exists ($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
return (mysql_result($query, 0) == 1) ? true : false;
}
?>
The following is line 5 from the above code where the error appears to be resulting from.
return (mysql_result($query, 0) == 1) ? true : false;
Thanks again in advance.
This error indicates that your previous statement mysql_query
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
has returned false, if you check the documentation it states that on failure mysql_query will return false. Therefore we need to come to a conclusion that your query is failing. To find out more information on why your query is failing. Include mysql_error() at the end of the statement.
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'") or die(mysql_error());
I have created a registration program which insert username password into mysql database. I am now trying to check the username exixts or not. I wrote the following program. its not working. it showing "mysql_num_rows() expects parameter 1 to be resource."
I really need your expert suggestion
registration.php
<?php
require 'jcon.php';
if(isset($_POST["username"], $_POST["firstname"],$_POST["password"])){
$username=$_POST["username"];
$firstname=$_POST["firstname"];
$password=$_POST["password"];
}
$query = mysql_query("SELECT * FROM member WHERE username='$username'");
if(mysql_num_rows($query) != 0)
{
echo "Username already exists";
}
else{
$sql="INSERT INTO member (username, firstname, password)
VALUES ('$username', '$firstname','$password')";}
if(!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
echo "Dear {$firstname} ! you have been successfully registered. "
?>
it showing "mysql_num_rows() expects parameter 1 to be resource
This is the typical case where mysql_query returns false upon failure therefore triggering the infamous:
mysql_num_rows() expects parameter 1 to be resource
This can be caused by multiple factors. Try running the query in phpMyAdmin or directly to the database and see the error or fetch the last mysql error via mysql_error.
It's good practice to always check if the returned value of mysql_query is false or if the mysql_error string is not empty:
if ($result and empty(mysql_error()))
// everything ok
Note: Never ever mix mysql_ and mysqli_ functions. If you have to choose I'd go with mysqli since mysql_* functions are considered deprecated.
You can use this query:
SELECT COUNT(*) as cnt FROM member WHERE username = '$username'`.
If he's not registered, cnt column will return 0.
Therefore, you can avoid the needed parameter as it is possible for mysql_query to return FALSE if the query doesn't reproduce result and it's not possible for mysql_fetch and friends to make it as parameter.
Everything in my new website is working just fine, except that I can't get this piece of code to work:
$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'");
$row = mysql_fetch_assoc($query);
$activation = $row['activation'];
if ($activation == '0') {
die('Your account is not yet active. Please check your email!');
exit();
}
In the database, the type is enum('0', '1') and the field name is activation.
Here is the error message I am getting:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result
Could someone please give me a hint?
Thank you.
The error message suggests that your query is invalid:
$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'");
Are you doing any input sanitization for $usermail?
Are you sure your database contains that table and that the table contains that column.
Try doing a little debugging:
$query = "SELECT * FROM members WHERE useremail = '$useremail'";
echo $query;
and try running the content of $query directly in your database (from phpMyAdmin or something).
Everything in my new website is working just fine
...until something goes wrong.
You have to learn how to handle errors.
run all your queries at least this way.
$query = "SELECT * FROM members WHERE useremail = '$useremail'"
$result = mysql_query() or trigger_error(mysql_error()." ".$query);
and you always be notified of any error and it's reason
or implement any other way of error notifications, employing mysql_error() function which is the only thing in the world that can tell you where the actual problem is.
Change your first line to:
$query = mysql_query("SELECT * FROM members WHERE useremail = '$useremail'") or die(mysql_error());
And it will spit out the mysql error that is causing the Warning =)
You should not be presuming the call to mysql_query returns a valid result; It's good practice to always test the result first, e.g.
$r=mysql_query("SELECT * YADYADA");
if($r && mysql_num_rows($r)>0){
$row=mysql_fetch_assoc($r);
}else{
//Uh oh, something's not right~~~ throw an exception maybe?
}