php and mysql error for registration form - php

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");

Related

Couldn't Fetch Mysqli

I have a problem fixing this code it says that
Warning: mysqli_query(): Couldn't fetch mysqli in
D:\Websites\movie-site\watch.php on line 56.php Warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null
given in D:\Websites\movie-site\watch.php on line 59
I got this issue when i close the mysqli connection and after closing the mysqli i am performing some more queries that time i got this error. so please check that your connection to your mysqli server is working.
From the sounds of the error the query you provided in mysqli_query() may have a syntax error in it which would explain why mysqli_query() wont work.
mysqli_fetch_array() will not work unless you have a returned result from mysqli_query(), good programming practice is before using mysqli_fetch_array() to check what has been returned by mysqli_query(), since it's saying that it's getting NULL in the argument an example check would be
if(empty(mysqli_query($conn,$query)))
{
echo "can not execute query"; // can use anything as a friendly error
}
From the errors you are getting, I can say that you're not passing any parameters to the mysqli_fetch_array() function, while it expects one argument.
You should run the mysqli_query() function with your SQL query as an argument and the returned result is stored to a result variable.
For example:
$result = mysqli_query($connection,"SELECT * FROM Peson");
Then you should use the $result variable with msqli_fetch_query() to fetch the result to a row array.
For example:
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}

mysql_free_result() expects parameter 1 to be resource

im sending data from my android application to mySQL in localhost and I receive warning on (Warning: mysql_free_result() expects parameter 1 to be resource, Boolean given in C:\xampp\htdocs\datatest.php on line 17)
despite the warning i'm still able insert the data into the database.
i'm wondering is it ok to ignore this or how can i solve this problem?
i tried various forums and website by none solve my problems.
<?php
$dbcnx = mysql_connect("localhost", "root", "");
$db = "agentdatabase";
mysql_select_db($db, $dbcnx);
$user_id=$_POST['username'];
$passwd=$_POST['password'];
$query = "INSERT INTO agentable (username,password) VALUES ('".$user_id."','".$passwd."')";
echo $query;
$result = mysql_query($query) or die ("<b>Query failed:</b> " . mysql_error());
if($result){
echo '<br />','pass';
}
else echo mysql_error();
mysql_free_result($result);
mysql_close($dbcnx);
?>
You can't free the result of an INSERT query, since you can't free a boolean.
Side note, the MySQL PHP extension is deprecated. It's better to use MySQLi or PDO.
mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets.
mysql extension is deprecated, instead use the MySQLi or PDO_MySQL.
mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.
Here's another possible reason that I tested and was able to reproduce this error in two other different ways which have not been mentioned here.
1) Your SQL has the wrong syntax and looks like this (notice the incorrect extra comma after field3:
"select field1, field2, field3, from myTable";
2) Your SQL contains a duplicate field and looks like this:
"select field1, field2, field2 from myTable";
In both cases I got the message and it was displayed in different functions:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given"
"Warning: mysql_free_result() expects parameter 1 to be resource, boolean given"
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given"
use this:
if (is_bool($result) === false) {
mysql_free_result($result);
}

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.

mysql_num_rows 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().

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

Categories