Author Topic: Problem with printing sql values  (Read 469 times)

0 Members and 1 Guest are viewing this topic.

Offline gabry1234

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Problem with printing sql values
« on: July 04, 2014, 04:27:20 pm »
I have a problem to printing sql values with this code:

Code: [Select]
....
$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?

Offline Schalla

  • VIP
  • Peasant
  • *
  • Posts: 81
  • Cookies: 29
    • View Profile
Re: Problem with printing sql values
« Reply #1 on: July 04, 2014, 05:20:17 pm »
Hit me when I am wrong, but why don't use use limit instead?

Code: [Select]
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.

Quote
$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.

Offline gabry1234

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: Problem with printing sql values
« Reply #2 on: July 08, 2014, 09:52:47 am »
thank you very much!! now it work!!