mysql_num_rows PHP - php

elseif(isset($_POST['submit']))
{
// Look for their user
$lookuser = mysql_query("SELECT * FROM `users` WHERE username='". mysql_escape_string($_POST['username']) ."'");
// If we find a row
if(mysql_num_rows($lookuser) > 0)
But my else for that, echos: An error has occured. <br> If you are sure you entered your username correctly, please contact an administrator.
I've tried to echo $_POST['username']; all works out fine. I've made sure my user exists, that works out fine.
The PHP error I get:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in password.php on line 23

Php.net says this about the return value of mysql_query:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
If you're getting a boolean, it's because there's a mysql error. Use mysql_error() to print the error, it'll help you diagnose the issue.

You probably have an error in your sql query. To be sure of that, echo after your mysql_query mysql_error().

Related

php and mysql error for registration form

when i press register on my php form im getting this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean
given in /home/changj/public_html/register.php on line 26 No database
selected
Below is the register.php script line 26 but unsure how to fix the error
if(mysql_num_rows($sql)> 0 ) {
any ideas thanks.
I guess $sql is a string. Try this,
$result = mysql_query($sql);
if(mysql_num_rows($result)> 0 ) { .... }
but mysql_query() will be deprecated as of PHP 5.5.0. An alternative is to use, mysqli_query() or PDO::query()
You don't have a database selected: "No database selected"
Make sure that you have a mysql_connect() and a mysql_select_db() anywhere before your statement.
As answered by me here
From mysql_query() documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
The query is wrong. Change it.
Did you set the database?
mysql_select_db("databaseName");
Or, if you're using "raw queries", you may be missing this line:
mysql_query("USE databaseName");

I am getting error in mysql_fetch_assoc() [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am connected with my database and there seems to be an error appearing on this line of code:
while($br = mysql_fetch_assoc($brand))
and on my query I put this:
$brand = mysql_query("Select * from genratb");
The error says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\videoshop\index.php on line 166
The first command is actually my line 166.
It looks like your query failed. The mysql_query call probably returned false, instead of the result resource.
$brand = mysql_query("Select * from genratb");
if (!$brand)
{
//error, query failed
}
else
{
while($br = mysql_fetch_assoc($brand))
{
//use row
}
}
If they query fails, then mysql_query() will return false. In that case, you need to look at mysql_error() to find out why the query failed.
The PHP documentation states:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Most likely your query is failing for some reason, and setting $brand to FALSE. Are you sure your database connection is working, and your table exists?
You can add after your query line:
if (FALSE===$brand) { die(mysql_error()); }
This should tell you what is going wrong.

PHP error mysql_num_rows returns expects boolean error. is my query wrong?

mysql_num_rows() expects parameter 1 to be resource, boolean given in line 33
if($password!=$password_again){
echo'password did not match';
}else{
$query="SELECT username FROM users WHERE 'username'='$username'";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)==1){
echo'the username'.$username.'already exists';
}else{
echo'ok';
}
}
'username'='$username' Change this to username='$username'
Getting a boolean back from mysql_query means there was an error - either there's a syntax error in the query (which yours has), or something else blew up (e.g. failed to connect first).
Change the query call to
$query_run=mysql_query($query) or die(mysql_error());
The syntax error I can see is due to bad quotes
$query="SELECT username FROM users WHERE 'username'='$username'";
^--------^----
when you quote a field name like that, it's no longer a field name - it's just a string. However, this isn't an SQL syntax error, it's a logic error as you're doing an invalid comparison, and not comparing the real username against the username field in the database.
Your SELECT query looks incorrect:
$query="SELECT username FROM users WHERE 'username'='$username'";
Should probably be something like
$query="SELECT username FROM users WHERE username='$username'";
i.e. do not put the column name in single quotes. But even that is dangerous - it opens your code to SQL injection attack.
You're not doing a check to see if mysql_query is returning a resource or FALSE prior to sending it to the mysql_num_rows. Please take a look at the documentation here for proper usage:
http://php.net/manual/en/function.mysql-query.php
In your case you're passing a FALSE to the mysql_num_rows function and that is why you're getting that error.
mysql_num_rows expects a MySQL resource. $query_run is being set to false because your query is erroring, due to the single quotes around 'username' in your WHERE clause.

Unknown warning while updating field

What sort of error is this, I mean while updating field if I use '', I am getting this error.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\xampp\htdocs\shadaab\aaction.php on line 87
ON 86-87 I have this query
$result = mysql_query("SELECT name FROM album WHERE name='$name'");
if(mysql_num_rows($result) != 0)
Pretty popular question on SO. It means your query failed, so $result is false.
Add
if (!$result){
echo mysql_error();
}
To see what exactly error was. Most probably you have some illegal characters in $name variable. So, before embedding it into query string, you need to mysql_escape_string() it.
$name= mysql_escape_string($name);
Btw, your code is subject to SQL-injection. So, you'd better use prepared_statements
As the documentation says:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
You should check that $result !== false first.
mysql_query returns false on errors. Replace the first line with this (if you want to know the error):
mysql_query("SELECT name FROM album WHERE name='$name'") or die(mysql_error());
For SELECT statements mysql_query() returns a resource on success and FALSE on failure. Check if FALSE === $result

if mysql_query() fails, what to do?

Sometimes so happens that mysql_query() fails to INSERT data and I am unaware of it. So, the question is how do I know when it happens?
Quoting the documentation page of mysql_query :
For SELECT, SHOW, DESCRIBE, EXPLAIN
and other statements returning
resultset, mysql_query() returns a
resource on success, or FALSE on
error.
For other type of SQL statements,
INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on
success or FALSE on error.
So, to detect whether there's been an error or not, you have to test the return value of your call to mysql_query :
$result = mysql_query('...');
if ($result === false) {
// And error has occured while executing
// the SQL query
}
Then, what can you do ?
Well, first, you can log the error to a file, that you can analyse later
For than, you can use mysql_error and mysql_errno.
And you can display a nice error message the user
i.e. some kind of "oops, an error occured page".
Remember, though : the user doesn't need to know (and will not understand) the technical error message -- so don't display it.
You can use mysql_error php.net/mysql_error to get a better explanation that you then either display or log it in a file.
One check that I usually do is like in the example
$result = mysql_query( $query );
if ( !empty( $error = mysql_error() ) )
{
echo 'Mysql error '. $error ."<br />\n";
}
else
{
// the query ran successfully
}
This is a good check for any kind of queries
suggest making a wrapper for mysql_query that detects failure and logs the query plus mysql_error somewhere
If you're using a sensible client library, it will throw an exception when a SQL command fails, and you can examine the error code programmatically to know what to do.
If you have no code handling that specific error path, you should probably run a standard error handler which logs the error, stack trace and some other debug info into a log.
you have to analyze your sql. did you escape your parameters? sounds like this could be the problem. you may paste your sql so we can help you.
for showing the error, you can eg.
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
.
If the cause of the error is deadlock, you might want to try again.
You can check how many rows MySQL has inserted/updated/deleted by doing this query right after your insert/update/delete query.
SELECT ROW_COUNT() as rows_affected
If this returns 0 your insert failed. If it returns 1 or more you've succeeded.
See: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count
Probably you can check LAST_INSERT_ID() (mysql_insert_id() in PHP). Provided that your table has auto_increment column.

Categories