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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.