Jump to content

Loop is not working. Help


wobbie

Recommended Posts

Guys, I can’t find how to make this loop to count all records I’ve within $id

 

What am I doing wrong.

 

Thanks

 

 

$conn = mysql_connect("localhost", "root", "");

$sql = mysql_select_db( 'hillh' );

$sql2 = "SELECT id FROM admin WHERE id='$id'";

 

if (!$conn)

  {

  die('Could not connect: ' . mysql_error());

  }

 

$result = mysql_query($sql2);

$num = mysql_query($result);

 

echo "<table border=\"1\" align=\"center\">";

echo "<tr><th>Records</th>";

 

$i=0;

while ($i < $num) {

$out=mysql_result($i);

mysql_close();

++$i;

 

echo "<tr><td>";

echo $out;

echo "</td></tr>";

}

echo "</table>";

Link to comment
https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/
Share on other sites

You are doing most things wrong here m8, and really need to read up on the basics some more.

I'll help you get a bit further, but ^

 

First of all: what are you trying to do here? Are you just trying to count the records?

They you could do :

$sql ='select count(id) as number_of records from admin';

$result = mysql_fetch_array(mysql_query($sql));

 

echo $result[0]; // this will now print number of records total in your table

 

You can also do

$result = mysql_fetch_assoc(mysql_query($sql));

echo $result[number_of_records]; // this will now print number of records total in your table

 

Or you can do

$result = mysql_fetch_object(mysql_query($sql));

echo $result->number_of_records; // this will now print number of records total in your table

 

Say you want to print everything in your table :

$sql ='select * from admin';

$result = mysql_query($sql);

$number_of_rows = mysql_num_rows($result);

 

echo 'Number of rows total : '.$number_of_rows.'<br>';

while($row = mysql_fetch_assoc($result)) {

  echo 'id='.$row[id].'<br>';

  echo 'name='.$row[name].'<br>'; //if there is a coloumn named 'name'

  etc.

}

 

Good luck :-)

 

 

Thanks guys, Here and There I have managed.

 

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

  printf("ID: %s <br>", $row[0]);

}

 

$print=mysql_free_result($result);

 

But now I have to subtract 2 from max(id) because mysql indexes start from 0, not 1. and echo; that single record.

what about :

 

SELECT id FROM admin ORDER BY id DESC LIMIT COUNT(id)-2

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.