Link to home
Create AccountLog in
Avatar of Member_2_6479049
Member_2_6479049

asked on

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given in C:\....... on line 42

Hi guys,

I have this warning but I have not found what causes this error, please guys can you light me?
The error is in line 42 ---> while($row = mysqli_fetch_assoc($result)) {
$conn = mysqli_connect($DBhost, $DBuser, $DBpass, $DBname);
if (!$conn) {
	die("No fue posible conectarse a la base de datos: " . mysqli_connect_error());
	exit();
}else{
	$sql = "SELECT * FROM documentos WHERE documentos.key = '$clavenum' or nombre = '$snombre' ORDER BY fecha DESC, hora DESC";
	$result = mysqli_query($conn, $sql);
	if (mysqli_num_rows($result) > 0) {
		?> <table width="1000" border="0" align="center">
  		<tr>
    	<td>&nbsp;</td>
	  	</tr>
  		<tr>
    	<td><span class="style2"></span></td>
  		</tr>
		</table>
		<table width="1000" border="0" align="center">
  		<tr>
		<td width="150"><span class="style27 style2">Nombre de archivo:</span></td>
		<td width="850"><span class="style37"><?php echo "$snombre"; ?></span></td>
		</tr>
		<tr>
	    <td>&nbsp;</td>
	    <td>&nbsp;</td>
	    </tr>
		</table>
		<table width="1000" border="0" align="center">
		<tr bgcolor="#006A00"> 
		<td width="9%"><div align="left" class="style35 style38">
      	<div align="center"><span class="style2">Fecha</span></div>
    	</div></td>
    	<td width="18%"><div align="left" class="style39">
      	<div align="center"><span class="style2">Usuario</span></div>
    	</div></td>
	    <td width="65%"><div align="left" class="style39">
	    <div align="center"><span class="style2">Descripción</span></div>
	    </div></td>
		<td width="8%"><div align="left" class="style39">
		<div align="center"><span class="style2">Anexo</span></div>
		</div></td>
		</tr> <?php
	    while($row = mysqli_fetch_assoc($result)) {
    	    if ("-odd" == $odd) {
				$odd = "";
				echo "<tr bgcolor = #E6E6E6>";
			}else{
				$odd="-odd";
				echo "<tr bgcolor = #ffffff>";
			}
			$cantidadcaracteres = strlen($row["rutaorigen"]);
			if ($cantidadcaracteres > '0') {
				$caracteres = ($cantidadcaracteres - 4);
				$cualversion = substr($row["rutaorigen"], $caracteres, 1);
				if ($cualversion == ".") {
					$result = substr($row["rutaorigen"], -4);
				}else{
					$result = substr($row["rutaorigen"], -5);
				}
		  		echo ('<td><div class="style10"><font name="arial">' . $row["fecha"] . '</div></font></td>');
			   	echo ('<td><div class="style10"><font name="arial">' . $row["usuario"] . '</div></font></td>');
			   	echo ('<td><div class="style10"><font name="arial">' . $row["descripcion"] . '</div></font></td>');
			  	echo ('<td><div class="style2" align="center"><font name="arial"><a href="\deptos\Uploads/' .  $row["key"] . "$result" . '">' . "Anexo" . '</a></div></font></td>');
	  			echo "</tr>";
			}else{
			  	echo ('<td><div class="style10"><font name="arial">' . $row["fecha"] . '</div></font></td>');
	   			echo ('<td><div class="style10"><font name="arial">' . $row["usuario"] . '</div></font></td>');
		     	echo ('<td><div class="style10"><font name="arial">' . $row["descripcion"] . '</div></font></td>');
   			  	echo ('<td><div class="style10"><font name="arial">' . "." . '</div></font></td>');
			  	echo "</tr>";
			}
    	}
		?> </table> <?php
	}else{
    	echo "No existen registros";
		exit();
	}
	mysqli_close($conn);
}
?>

Open in new window

Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Something is wrong in your query so the $result fariable is 'false' instead of a mysqli_result... Let me take a closer look
Try to get the error:

$sql = "SELECT * FROM documentos WHERE documentos.key = '$clavenum' or nombre = '$snombre' ORDER BY fecha DESC, hora DESC";
$result = mysqli_query($conn, $sql);
if (!$result)
{
  echo("Error description: " . mysqli_error($con));
}
else
{
...

Open in new window

Avatar of Member_2_6479049
Member_2_6479049

ASKER

I tried but displays the same error only on line 42

$sql = "SELECT * FROM documentos WHERE documentos.key = '$clavenum' or nombre = '$snombre' ORDER BY fecha DESC, hora DESC";
$result = mysqli_query($conn, $sql);
if (!$result)
{
  echo("Error description: " . mysqli_error($con));
Try to echo the sql query to see it is what you expect it to be:

$sql = "SELECT * FROM documentos WHERE documentos.key = '$clavenum' or nombre = '$snombre' ORDER BY fecha DESC, hora DESC";
echo $ql;
exit;
The echo displays this:

SELECT * FROM documentos WHERE documentos.key = '05115' or nombre = 'Solicitud de Apoyo logistico' ORDER BY fecha DESC, hora DESC

and yes, it is what i'm looking for
Ok, this is a stupid question, I know, but... are you sure the record i present in the database?
Yes it exist, actually it displays one record
And there are 5 records that must be displayed but displays only one and the warning line
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
wow, wow, wow

Oh my Goood Marco, you got it.

I'll take note for the next one :)

Thank you so much, you made may day.
Lol, glad to help you. On to hte next.