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
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 :-)

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

I need to learn how select one specific record into a table.

 

Ej. This line would give me the last record of id:

SELECT id FROM admin ORDER BY id DESC LIMIT COUNT id

 

I need to select the previous record to the last or in other words. (-1)

 

Please help ???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.