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

MySQL ServerPHP

Avatar of undefined
Last Comment
Marco Gasi

8/22/2022 - Mon
Marco Gasi

Something is wrong in your query so the $result fariable is 'false' instead of a mysqli_result... Let me take a closer look
Marco Gasi

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

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));
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Marco Gasi

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

ASKER
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
Marco Gasi

Ok, this is a stupid question, I know, but... are you sure the record i present in the database?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Member_2_6479049

ASKER
Yes it exist, actually it displays one record
Member_2_6479049

ASKER
And there are 5 records that must be displayed but displays only one and the warning line
ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_6479049

ASKER
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Marco Gasi

Lol, glad to help you. On to hte next.