I'm trying to get email validation working. So my PHP code is firing off a query that that should return if it has any rows with the matching email. If 1 row is returned it then returns an error message, that I have else where in my code and sets my valid boolean to false.
However, I'm getting this error "Warning: mysql_num_rows() expects parameter 1 to be resource, object given in"
I think I'm using mysql_num_rows() wrong, but I'm new to php/mysql :( I can't seem to figure it out.
Here's the relevant sections of my code.
$conn = #mysqli_connect("server","user","pass","database");
if (!$conn) {
// Displays an error message
echo "<p>Database connection failure</p>";
////
$sql_table="Customer";
$query="SELECT * FROM $sql_table WHERE EMAIL = '$email'";
$result = mysqli_query($conn, $query);
if (mysql_num_rows($result) >= 1) { //<-- Offending line
$takenErr = "Email already taken ";
$valid = false; }
if (!$result) {
echo "<p> something is wrong with ", $query, "<p>";
}
Thanks guys/gals! :).
You can use
$row_cnt = $result->num_rows;
or
mysqli_num_rows($result);
you are using mysqli driver so use it at all times.
You are mixing mysqli_* and mysql_* function.
Try this :
if (mysqli_num_rows($result) > 0) {
$takenErr = "Email already taken ";
$valid = false;
}
For more information please read this mysqli_*
Related
I have a problem, so I want to do that if by chance the user searched on and the code for it was no longer activated. In this way, I made a simple code:
$loginu = $_SESSION['login'];
$query = "SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode = $numer AND usrlogin = $loginu ";
$result2 = mysql_query($query);
echo mysql_error();
if (mysql_num_rows($result2) == 0) {
not found
}
else {
found
}
and crashes me the following errors
Unknown column 'Kamil' in 'where clause'
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\kod.php on line 56
Where in this case I made a mistake?
Please help,
greet.
Just make sure that those strings are wrapped with quotes if they are not integers:
WHERE idcode = $numer AND usrlogin = '$loginu'
And make sure that this variable $numer is indeed defined, since in your question there is no definition of it.
Obligatory note:
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Here's what it would look like when used in mysqli with prepared statements:
$loginu = $_SESSION['login'];
$numer = // make sure this is defined!
$db = new mysqli('localhost', 'username', 'password', 'database_name');
$query = "SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode = ? AND usrlogin = ?";
$select = $db->prepare($query);
// binding them
$select->bind_param('is', $numer, $loginu);
$select->execute();
if($select->num_rows > 0) {
// found
} else {
// not found
}
check this code and if condtion check to not blank a session data.
and check a query value is yes to found and else to not found print.
$loginu = $_SESSION['login'];
if(isset($loginu))
{
$query="SELECT `usrlogin`,`idcode` FROM `aktywacja` WHERE idcode='".$numer."' AND usrlogin ='".$loginu."'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
{
echo "found";
}
else
{
echo "not found";
}
}
Try changing this line in you query
WHERE idcode='".$numer."' AND usrlogin ='".$loginu."'
I wrote this line of code, but i do not know what happened. I have looked all around the internet for the solution, but none of them seem to fix my issue. I get:
Warning: mysql_query() expects parameter 1 to be string, resource given in /home/mylittle/public_html/style1.php on line 12
yes
When i enter the page. It does not update the style thing in my database. Please help me. I am desperate!
$dbewds = mysql_connect("localhost","mylittle_pony","lol123", "mylittle_pony") or die("Couldn't connect!");
if ($_SESSION['username']) {
$unw = $_SESSION['username'];
$style = 1;
mysql_query($dbewds,"UPDATE `users` SET `style` = '".$style."' WHERE `username` = '".$unw."'");
echo "yes";
} else {
echo "no";
}
?>
$dbewds = mysql_connect("localhost","mylittle_pony","lol123") or die("Couldn't connect!");
mysql_select_db("mylittle_pony");
if (isset($_SESSION['username'])) {
$unw = $_SESSION['username'];
$style = 1;
$query=mysql_query("UPDATE `users` SET `style` = '".$style."' WHERE `username` = '".$unw."'",$dbewds);
if(!$query){
die("query failed".mysql_error());
}
echo "yes";
} else {
echo "no";
}
the connection should be the second variable
I would advise the steps you debug the likely problems next time:
1. try to understand the warning/error message
for example: "mysql_query() expects parameter 1 to be string, resource give", so figure what is a string, what is a resource, according to your code
2. read the manual
go to http://us2.php.net/manual/en/ and search "mysql_query", you can get http://us2.php.net/manual/en/function.mysql-query.php, so figure out how to use the function,
pay attention to the parameters and return, and run the examples under the function intro
3. check your code
btw, mysql_query() will be deprecated as of PHP 5.5.0, MySQLi or PDO_MySQL is better.
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
warning:mysql_fetch_array() expects parameter 1 to be resource, object given [duplicate]
(3 answers)
Closed last year.
I cant seem to figure out what I'am doing wrong. So when I submit my form I get Warning error and the
Notice: Undefined variable: dbusername in /Library/WebServer/Documents/ArturoLuna_Final/loginCheck.php on line 30
$username = $_POST['username'];
$password = $_POST['password'];
if($username&&$password)
{
require 'conn.php';
$query = "SELECT * FROM users WHERE username='$username'";
$result = $mysql->query($query) or die(mysqli_error($mysql));
$numrows = $result->num_rows;
if ($numrows!=0)
{
while($row = mysql_fetch_assoc($result))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
//check to see if they match!
if($username==$dbusername&&$password==$dbpassword)
{
echo "youre In!";
}
else
echo "incorrect password!";
}
else
die("that user is dead");
//echo $numrows;
}
else
echo ("Please Enter Username")
what can I be possibly doing wrong?
Change
while($row = mysql_fetch_assoc($result))
to
while($row = $result->fetch_assoc())
You missed an i off the function name, and mixed up OO and procedural style code, so you are mixing up mysql_* result resources and mysqli_result objects.
You're mixing traditional PHP MySQL functions mysql_* with the MySQLi interface. In this case, mysql_fetch_assoc() expects a resource handle created with mysql_query().
To retrieve the correct result, you'll need to use MySQLi's method:
$row = $result->fetch_assoc();
And while you're at it, you might want to consider making the code less susceptible to MySQL injection. Properly escape any user provided input before interpolating it into a query string, or better yet, use MySQLi parameter binding facilities.
I'm receiving the following error:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource..
and I can't figure out what is going on. Here is my code. I did create a database with a table named notes. Also, the database connection is working correctly. Any idea on what is going on?
$query = mysql_query("SELECT * FROM notes ORDER BY id DESC");
mysql_fetch_assoc($query)
Thanks
var_dump($query);
echo mysql_error();
Did mysql_query fail and return FALSE? As the documentation explains:
For SELECT, SHOW, DESCRIBE, EXPLAIN
and other statements returning
resultset, mysql_query() returns a
resource on success, or FALSE on
error.
FALSE would not be a valid resource.
On the PHP page, it gives you a perfect example of how to use it and do error checking...
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
How about you go and implement the entire way making sure rows are returned, and its a valid resource, and if result is truthy?
You should always debug and never assume something executed rightfully.
In the future please read through the entire page of the function on php.net, in this case it's http://php.net/manual/en/function.mysql-fetch-assoc.php
I am using a simple PHP script for the activation part of one of my applications. The applications posts one variable to the page (http://validate.zbrowntechnology.info/WebLock.php?method=validate). The variable is the serial number, posted as 'Serial'. Each time I post to this page, it returns Invalid. Here is the code:
<?php
$serial = $_POST['Serial'];
$method = $_GET['method'];
$con = mysql_connect("HOSTHERE", "USERHERE", "PASSHERE");
if(!$con) {
die('Unable to connect to MySQL: ' . mysql_error());
}
if($method == "validate") {
mysql_select_db("zach_WebLock", $con);
$query = "SELECT Key, Status FROM Validation WHERE Key='".mysql_real_escape_string($serial)."'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
echo "Valid";
} else {
echo "Invalid";
}
} else {
echo "Unkown Method";
}
?>
Here Is The Error From PHP,
PHP Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given
Right after the query use mysql_error() to see what happened. And Key is a bad choice for a column name because it's a reserved word in SQL. You can enclose it in `` to tell MySQL it's an identifier. Do some more debugging like this:
...
if (!mysql_select_db("zach_WebLock", $con)) die('mysql_select_db failed');
$query = "SELECT `Key`, Status FROM Validation WHERE `Key`='".mysql_real_escape_string($serial)."'";
print "query=$query<br>\n";
$result = mysql_query($query, $con);
print "error=" . mysql_error($con);
...
You're missing a closing parenthesis on this line:
if(mysql_num_rows($result) > 0 {
Is that missing in your code or just your question?
You may also want to add
if (!$result) {
print mysql_error();
}
after your query
Try Like This
$query = "SELECT Key, Status FROM Validation WHERE Key='".$serial."'";
What happens if at the last line you add this?
else echo 'Unknown method';
What may be happening is that $_POST and $_GET are not getting populated, this is a setting in php.ini, if I remember correctly (search for "superglobals" in the php docs).
edit: also, you have a very bad security risk there, google "sql injection". Basically the problem is that you could get any SQL directly into your database, and if the php user has enough permissions it could mean that anyone can, for example, delete all the data from your Validation table. You should at least do something like this:
$query = "SELECT Key, Status FROM Validation WHERE Key='".addslashes($serial)."'";
It could be a typo but you are missing a closing parenthesis here:
if(mysql_num_rows($result) > 0 {
^
And you might have turned off you error reporting, in which case you get a blank page.
Try echoing $serial:
echo $serial;
And is it what you typed in form?