wobbie Posted February 1, 2007 Share Posted February 1, 2007 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 More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 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 https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-174530 Share on other sites More sharing options...
artacus Posted February 1, 2007 Share Posted February 1, 2007 $sql2 = "SELECT id FROM admin WHERE id='$id'"; I've been told this week that my sense of humor went beyond sarcastic to the level of sardonic... So without saying anything further, welcome to PHP Freaks! Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-174736 Share on other sites More sharing options...
fenway Posted February 1, 2007 Share Posted February 1, 2007 LOL.l Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-174768 Share on other sites More sharing options...
janroald Posted February 1, 2007 Share Posted February 1, 2007 hehe, I chose to look away from that one, even though it was very tempting :-) Guess I'm just a bigger man ;-) Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-174820 Share on other sites More sharing options...
wobbie Posted February 2, 2007 Author Share Posted February 2, 2007 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 https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-175118 Share on other sites More sharing options...
janroald Posted February 2, 2007 Share Posted February 2, 2007 Are you French? Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-175283 Share on other sites More sharing options...
wobbie Posted February 5, 2007 Author Share Posted February 5, 2007 No, Spanish. Madrid Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-177292 Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 But now I have to subtract 2 from max(id) because mysql indexes start from 0, not 1. and echo; that single record. Please explain what you think you mean by this. Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-177303 Share on other sites More sharing options...
wobbie Posted February 5, 2007 Author Share Posted February 5, 2007 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 https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-177688 Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 I don't know what you mean... you mean the record you just inserted? If so, you need to use SELECT LAST_INSERT_ID() to get the ID. Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-177732 Share on other sites More sharing options...
wobbie Posted February 6, 2007 Author Share Posted February 6, 2007 What I mean is: Penultimate row. Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-178587 Share on other sites More sharing options...
fenway Posted February 7, 2007 Share Posted February 7, 2007 What I mean is: Penultimate row. I still don't understand... you have the id, and you're asking for the id back? Link to comment https://forums.phpfreaks.com/topic/36622-loop-is-not-working-help/#findComment-179201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.