I run this query to check if cat_name already exists it the mysql database... but it's show a warning messag.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs......"
<?php
include("db.php");
if(isset($_POST['cat_name']))
{
$cat_name = mysql_real_escape_string(htmlentities(trim($_POST['cat_name'])));
$err = array();
$ch = mysql_query("SELECT cat_name FROM categories WHERE cat_name = '$cat_name' ");
$num = mysql_num_rows($ch);
if(empty($cat_name))
$err[] = "Category field empty";
elseif(is_numeric($cat_name))
$err[] = "Category name should be string, ex: category name";
elseif($num > 0)
$err[] = "Category name already exits, please choose another name";
else
{
if(strlen($cat_name) < 3)
$err[] = "Category name at least 3 or more
characters";
}
if(!empty($err))
{
foreach($err as $er)
{
echo "<font color=red>$er.</font>";
}
}
else
{
$sql_insert = mysql_query("INSERT INTO categoires VALUES('',
'$cat_name')");
if($sql_insert)
{
echo "Successfully inserted your category name";
}
else
{
echo "Something is wrong to add your cateogory name";
mysql_error();
}
}
}
?>
Any idea?
Your query is failing (and returning false). Check mysql_error or the mysql/php error logs.
From the docs:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
mysql_query() will also fail and return FALSE if the user does not
have permission to access the table(s) referenced by the query.
My best guess is that you have an error in your query.
Related
Every time i am trying to run the following PHP code on 000Webhost, i keep getting this error
-- mysqli_num_rows() expects parameter 1 to be mysqli_result.
The same code had been run successfully without errors on my localhost, XAMPP, i have looked through many examples and only found out that this error is caused by an error in the query, but as mentioned, the query works perfectly on my localhost.
The error is indicated in the code.
Any help would be appreciated.
<?php
session_start();
//decalre variables
$DeviceID ="";
$productID ="";
//connect to database
$db = mysqli_connect('localhost','id5655845_grocerywatch1234','123456','id5655845_grocerywatch1234');
//validate product id and device id are avaliable
if(isset($_POST['validate_user'])){
$DeviceID = mysqli_real_escape_string($db,$_POST['DeviceID']);
$productID = mysqli_real_escape_string($db,$_POST['productID']);
$query = "SELECT * FROM configuration WHERE DeviceID='$DeviceID' AND productID='$productID'";
$result1 = mysqli_query($db,$query);
echo $query;
//error indicated on the following line.
if(mysqli_num_rows($result1) == 1){
$_SESSION['DeviceID'] = $DeviceID;
$_SESSION['success'] = "You are now logged in";
header('location: register.php');
}
else{
echo "Device Not registered";
echo "Product Doesnt Exist";
}
}
I think your query is likely failing. The return value for mysqli_query is False on failure, otherwise it is mysqli_result. See docs here
Fix by properly formatting string:
...
$query = "SELECT * FROM configuration WHERE DeviceID='".$DeviceID."' AND productID='".$productID."'";
$result1 = mysqli_query($db,$query);
echo $query;
if ($result1 == false){
echo "Error has occurred!";
}
elseif (mysqli_num_rows($result1) == 1){
$_SESSION['DeviceID'] = $DeviceID;
$_SESSION['success'] = "You are now logged in";
header('Location: register.php');
}
else{
echo "Device Not registered";
echo "Product Doesnt Exist";
}
The query either returned no rows or is erroneus, thus FALSE is returned. Change it to
if (!$dbc || mysqli_num_rows($dbc) == 0)
Return Values
Returns TRUE on success or FALSE on failure. For SELECT, SHOW,
DESCRIBE or EXPLAIN mysqli_query() will return a result object.
include("lib/config.php");
include("lib/mysql.php");
if ($_GET['action'] == "loginsignup") {
$error = "";
if(!$_POST['email']) {
$error = "an email address is needed";
} else if(!$_POST['password']) {
$error = "a password is needed";
} else if (filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) === false) {
$error = "please enter a valid email address";
}
if ($error != "") {
echo $error;
exit();
}
if($_POST['loginactive'] == "0") {
$query = "SELECT * FROM 'users' WHERE email = '".mysql_real_escape_string($link, $_POST['email'])."' LIMIT 1 ";
$result = mysql_query($link,$query);
if(mysql_num_rows($result) == 0) $error = "that email address is already taken.";
}
if ($error != "") {
echo $error;
exit();
}
}
so here is the code im trying to verify if there are any errors and if there is an email in my database that has the same one thats being signed up so im getting these errors
mysql_real_escape_string() expects parameter 1 to be string, resource given
mysql_query() expects parameter 1 to be string, resource given in
mysql_num_rows() expects parameter 1 to be resource null given in
and i really i tried going back and forth with mysqli and mysql and they both have the same errors
You're passing as argument a variable which is either declared or initialized to mysql_real_escape_string(), which is causing it to return an unexpected result, causing mysql_query() & mysql_num_rows() not receiving the expected parameters.
Hope it helps!
I have this line of code, which checks the result of the query. I want to have a STATUS ERROR if one of the columns contains a value= '0'.
If the condition founds a zero value it will echo: Status: ERROR,
Status
PHP CODE:
<?php
$query = "SELECT * FROM `inventorybill_tbl`";
$result = mysqli_query($link, $query) or die("Failed".mysqli_error());
//Here it will check the result if there is a value contains zero
if (empty($result)) {
echo 'Status: ERROR';
} else {
echo 'Status: OK';
}
?>
Is there any way to achieve this kind of method? Thanks.
mysqli_query() Return values
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.
Check if empty($result->num_rows) - then there's no data.
bool zeroExist = false;
$finfo = $result->fetch_fields();
foreach ($finfo as $val)
{
while($row = $result->fetch_assoc())
{
if ($row[$val->name] === 0)
{
zeroExist = true;
break;
}
}
if(zeroExist){
break;
}
}
if (!zeroExist){
echo 'Status: OK';
}else {
echo 'Status: ERROR';
}
try this
try this.
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
//$row["your column name"]
if (empty($row["twohundred"]) || $row["twohundred"]=='0' || $row["twohundred"]==0) {
echo 'Status: ERROR';
} else {
echo 'Status: OK';
}
}
}
You should check condition in your query instead of PHP code.
Rewrite your query as below:-
SELECT * FROM table WHERE NOT columnA = 0 OR NOT columnB = 0;
OR
SELECT * FROM table WHERE columnA != 0 OR columnB != 0;
Check this Original Answer Link
I want to check if my table is empty I've tried this "which I think that is the solution"
$test_empty="SELECT *FROM objectif where 1 ";
if(empty($test_empty))
{
echo "I m here";
}
But it seems that it doesn't work.
Depending on how you are connecting to your database (for example, using mysqli):
$db = new mysqli("localhost","username","password","dbname");
$check = $db->query("SELECT COUNT(*) FROM objectif");
if ($check->num_rows == 0 || $check->fetch_field() == 0){
echo "table is empty";
}else{
echo "table is not empty";
}
Currently, your code isn't actually connecting to the database or querying the table - you are essentially just checking if the variable $query is empty (which it never will be, as it contains a string!
Running a query to fetch the number of records and checking that as per the code above is one way to do this.
Use this
$mysqli = new mysqli("localhost","root","","db");
if ($result = $mysqli->query("SELECT * FROM `table` LIMIT 1"))
{
if ($obj = $result->fetch_object())
{
echo "NOT EMPTY";
}
else
{
echo "empty";
}
$result->close();
}
$mysqli->close();
Please try below code :
$test_empty="SELECT * FROM objectif";
$query = mysql_query($test_empty);
if(mysql_affected_rows() > 0)
{
echo "It is Empty";
}
i've got a problem with my php script. I want to check my script if there is anything in array and if there's no to echo a message but when array is empty it just don't echo nothing.
That's my code:
include("mysql_connect.php");
$name = $_POST['playerName'];
$users = mysql_query('SELECT * FROM playerdata WHERE username LIKE "'.$name.'%"');
if($name==""){
echo 'Type in player name!';
}else{
while($usersList= mysql_fetch_assoc($users)){
if(!array_count_values($usersList)){
echo 'Player not found';
}else{
echo $usersList['username'].', ';
}
}//While end.
}//If name is empty end.
As mentioned, you have to check for rows before the loop.
$rows = mysql_num_rows($users);
if($rows==0) echo "No rows..";
else {
foreach() {
The problem seems is that, if a user enters a invalid name, the while loop does not execute, as the query simply cannot find any rows, the code should be as follows
include("mysql_connect.php");
$name = $_POST['playerName'];
if( $name === "" ){
echo 'Type in player name!';
} else {
$users = mysql_query('SELECT * FROM playerdata WHERE username LIKE "'.$name.'%"');
if ( mysql_num_rows($users) == 0) {
echo "Invalid Player name provided";
} else {
while($usersList= mysql_fetch_assoc($users)){
{
echo $usersList['username'].', ';
} //While end.
}
}//empty name
Notes:
I have re-formatted the code as it looked ugly
The query is executed only if a user provides some text for name, why execute the query when the name is empty !
instead of
if(!array_count_values($usersList)){
use
if(empty($usersList)){
Your while loop wont be executed if $users is empty, so the condition:
if(!array_count_values($usersList)){
echo 'Player not found';
}
will never be met because it is contained in a loop which will never run.