EvilZone

Programming and Scripting => Web Oriented Coding => : gabry1234 July 04, 2014, 04:27:20 PM

: Problem with printing sql values
: gabry1234 July 04, 2014, 04:27:20 PM
I have a problem to printing sql values with this code:

:
....
$con = mysqli_connect($server,$us,$pass,$database);

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL";
}

$result = $con->query("SELECT ID,Nome,Cognome FROM Contatti WHERE ID>=0 and ID<=10 ORDER BY ID");
$row = $result->fetch_array(MYSQLI_NUM);

while($row = $result->fetch_array())
{
$rows[] = $row;
}


echo '<table border=0 cellspacing=2 cellpadding=2>';
echo ' <tr>
   <th><font size=3 face="Arial, Helvetica, sans-serif">Nome</font></th>
   <th><font size=3  face="Arial, Helvetica, sans-serif">Cognome</font></th>
 </tr>';

foreach($rows as $row)
{
echo' <tr>';
echo '<td><font size=2  face="Arial, Helvetica, sans-serif">'.$row['Nome'] . "</font></td>" ;
echo '<td><font size=2  face="Arial, Helvetica, sans-serif">'.$row['Cognome']  . "</font></td>" ;
echo' </tr>';
}
....

The problem is that the first value (in the sql extraction) is not printed

Someone can help me?
: Re: Problem with printing sql values
: Schalla July 04, 2014, 05:20:17 PM
Hit me when I am wrong, but why don't use use limit instead?

:
SELECT ID,Nome,Cognome FROM Contatti ORDER BY ID LIMIT 0,10
However, you are pulling the first array before the while loop kicks in, I marked that red for you.
Remove that line and you are fine.

$row = $result->fetch_array(MYSQLI_NUM);

while($row = $result->fetch_array())
{
$rows[] = $row;
}

I still may recommend to switch to PDO, which is tbh the better implementation of a mySQL Wrapper.
: Re: Problem with printing sql values
: gabry1234 July 08, 2014, 09:52:47 AM
thank you very much!! now it work!!