I've problem with my code, the reason, when run query un PHP FILE, this retrieve all records from SELECT * FROM material
, then it pass to JS FILE for process and store all records in an array json format, but display this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result
resource in C:\AppServ\www\biblioteca\include\doLogin.php on line 31 []
_
PHP FILE WITH FUNCTIONS
.........
public function searchMat($tipoBusqueda,$terminoBuscar){
$query = " SELECT * FROM material ";
$result = mysql_query($query) or die (mysql_error());
$resultArray = mysql_fetch_assoc($result);
return $resultArray;
}
OTHER PHP FILE with functions
$results = $db->searchMat($tipoBusqueda, $terminoBuscar);
$jsonSearchResults = array();
if ($results != false) {
while($row = mysql_fetch_assoc($results)) {
$jsonSearchResults = array (
'clavemat' => $row['cve_mat'],
'tipomat' => $row['tipo_mat'],
'titulomat' => $row['titulo_mat'],
'autormat' => $row['autor_mat'],
'editmat' => $row['edit_mat'],
'success' => 'success',
);
}
echo json_encode($jsonSearchResults);
}
EDIT:
You're calling mysql_fetch_assoc
two times. The first time on the resource you got from mysql_query
, the second time on an array - invalid.
Also, when you would loop over all rows, you need to add to $jsonSearchResults
. As @RezaSanaie already said, it gets overwritten each iteration in your code. It should be:
public function searchMat() {
/* returns the material table as a resource */
$query = "SELECT * FROM material";
$result = mysql_query($query) or die (mysql_error());
return $result;
}
$results = $db->searchMat();
$jsonSearchResults = array();
while ($results!=false && $row = mysql_fetch_assoc($results)) {
array_push($jsonSearchResults, array(
'clavemat' => $row['cve_mat'],
'tipomat' => $row['tipo_mat'],
'titulomat' => $row['titulo_mat'],
'autormat' => $row['autor_mat'],
'editmat' => $row['edit_mat'],
'success' => 'success'
));
}
echo json_encode($jsonSearchResults);
Your searchmat function should return $result not the first row.
in this line:
$results = $db->searchMat($tipoBusqueda, $terminoBuscar);
$results
is an array, not a MySQL Result Resource. So you don't need to pass it into mysql_fetch_assoc
. You have already done that in searchMat
.
There is a problem here though. searchMat
only gets one row from the result and returns it. After that, you no longer have any way to get the rest of the rows. You should either have searchMat
return the resource or move your while
loop into searchMat
so that it can return an array containing all results from the query.
Try it like this:
public function searchMat($tipoBusqueda,$terminoBuscar){
$query = "
SELECT
cve_mat AS clavemat,
tipo_mat AS tipomat,
titulo_mat AS titulomat,
autor_mat AS autormat,
edit_mat AS editmat,
success
FROM material
";
$result = mysql_query($query) or die (mysql_error());
$out = array();
while ($row = mysql_fetch_assoc($result))
{
$out[] = $row;
}
return $out;
}
$results = $db->searchMat($tipoBusqueda, $terminoBuscar);
if (!empty($results)) {
echo json_encode($results);
}
I've also changed your query so that columns are already named as you want them. Then you don't have to go to the trouble of copying one array into another.