Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
C:\xampp\htdocs\sites\admin_panel\admin\base\load_data.php

<prev>this load_data.php file </prev>

What I have tried:

<pre> <?php
//ADD lost _ item...

$query = "Select Max(item_id) From lost_items_table";
$returnD = mysql_query($query);
$result = mysql_fetch_assoc($returnD);
$maxRows = $result['Max(item_id)'];
if(empty($maxRows)){
$lastRow = $maxRows = 1001;
}else{
$lastRow = $maxRows + 1 ;
}

if(isset($_REQUEST['additem'])){

$item_id = $_REQUEST['item_id'];
$item_name = $_REQUEST['item_name'];
$description = $_REQUEST['description'];
$item_category = $_REQUEST['item_category'];
$pic = $_REQUEST['pic'];

if(!empty($item_id) && !empty($item_name) && !empty($item_category)){

if($maxRows){

$query = "Insert Into lost_items_table(item_id,item_name,description,item_category,pic) Values('$item_id','$item_name','$description','$item_category','$pic')";
mysql_query($query);
$errorMsg = "data Sucessfully Added.";

$query = "Select Max(item_id) From lost_items_table";
$returnD = mysql_query($query);
$result = mysql_fetch_assoc($returnD);
$maxRows = $result['Max(item_id)'];
if(empty($maxRows)){
$lastRow = $maxRows = 1001;
}else{
$lastRow = $maxRows + 1 ;
}
}
else{
$errorMsg = "Table is Empty.";
}

}
else{
$errorMsg = "Please! Enter in Empty Field.";
}

include("add_lostitem.php");
}
?>
Posted
Updated 20-Jun-18 9:27am

This a FAQ (Frequentyl Asked Question) which has been asked and answered multiple times and can be also solved by reading the documentation:
Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

You have to check the return value of mysql_query() as shown in the examples at the above link before passing it to other functions like mysql_fetch_assoc().

Upon failures report the error message to know what went wrong. Usual error sources are invalid SQL commands and the message provided by mysql_error() will tell you what was wrong.

Because I don't know about your database, I can only guess here. The SQL query selecting the max. ID looks fine provided that column and table exist. If that query fails, I would expect that the previous INSERT query has failed too.
 
Share this answer
 
PHP
$query = "Insert Into lost_items_table(item_id,item_name,description,item_category,pic) Values('$item_id','$item_name','$description','$item_category','$pic')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900