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

elm9999Asked:
Who is Participating?
 
Marco GasiFreelancerCommented:
Oh, yes, found it! In lines 55 / 57 you use $result as a string variable! This is the reason wy the first record is displayed, then $result becomes a string and the second loop raises the error!
Just change the string variable name, for instance $result_str and it will work.

Cheers
0
 
Marco GasiFreelancerCommented:
Something is wrong in your query so the $result fariable is 'false' instead of a mysqli_result... Let me take a closer look
0
 
Marco GasiFreelancerCommented:
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

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
elm9999Author Commented:
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));
1
 
Marco GasiFreelancerCommented:
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;
0
 
elm9999Author Commented:
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
0
 
Marco GasiFreelancerCommented:
Ok, this is a stupid question, I know, but... are you sure the record i present in the database?
0
 
elm9999Author Commented:
Yes it exist, actually it displays one record
0
 
elm9999Author Commented:
And there are 5 records that must be displayed but displays only one and the warning line
0
 
elm9999Author Commented:
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.
0
 
Marco GasiFreelancerCommented:
Lol, glad to help you. On to hte next.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.