|
Posted by Jerry Stuckle on 06/05/07 10:46
mypetprogrammer@gmail.com wrote:
> On Jun 4, 5:53 pm, Denis Gerina <denisREMOVET...@cced.ba> wrote:
>> vinnie wrote:
>>> it only displays the name, but the last name and the address seems not
>>> to exist! why?
>>> <?php
>>> @ $db=mysql_pconnect('host', 'UserID', 'PWD');
>>> if (!$db)
>>> {
>>> echo 'conneciton eror';
>>> exit;
>>> }
>>> else
>>> {
>>> echo 'connection on!';
>>> }
>>> mysql_select_db('list');
>>> $query="select * from index";
>>> $result=mysql_query($query);
>>> $num_results=mysql_num_rows($result);
>>> if ($num_results == 0)
>>> {
>>> echo'<br><br>nothing to dispaly';
>>> }
>>> else
>>> {
>>> echo'<br><br>here are the results: '.$num_results;
>>> }
>>> for ($i<0; $i<$num_results; $i++)
>>> {
>>> $row=mysql_fetch_array($result);
>>> echo '<br>name: ';
>>> echo htmlspecialchars(stripslashes($row['name']));
>>> echo '<br>last name: ';
>>> echo htmlspecialchars(stripslashes($row['last']));
>>> }
>>> ?>
>> You don't seem to be printing address anywhere. As for last name, check
>> the name of the column in your table (index).
>>
>> And, to avoid notices, the code
>>
>> for ($i<0; $i<$num_results; $i++)
>>
>> should be
>>
>> for ($i=0; $i<$num_results; $i++)
>>
>> Although I would probably write something like
>>
>> while ($row = mysql_fetch_array($result))
>> {
>> ...
>>
>> }
>
> See, and I would avoid the inevitable out of bounds on that with:
> while( ($row = mysql_fetch_array($result)) != null)
> {
>
> So as not to be operating on a null on the last iteration of the loop.
> Agree with the while, though.
>
> ~A!
>
Denis's code is correct. There will be no out of bounds error in Denis's
code.
mysql_fetch_array() is a function which returns either an array or
false. It does not return null. Denis's code will continue to fetch
rows as long as the exist; after the last row has been fetched, the next
call to mysql_fetch_array() returns false, stopping the loop.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|