Gacha Posted January 28, 2004 Report Share Posted January 28, 2004 Nu es papildinaaju savu news.php ar viena turtoriala palidzibu, lai varetu noteikt cik postiem raadiitos vienaa lapaa, bet man raada erroru Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in d:\usr\www\db\news.php on line 52 Un te ir news.php <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=windows-1257"> </head> <body> <a href="post_news.php" target="_self">Ievietot postu</a> <? include_once("../protected/mysql_config.php"); $tablename = "news"; if (!mysql_connect($hostname,$dbuser,$dbpass)) {echo "Nevar pievienoties DB";} //izvadu informaciju no DB if (mysql_select_db ($dbname)) { $result = mysql_query ("Select * from $tablename order by id desc"); if (mysql_affected_rows() != 0) { while ($row = mysql_fetch_array ($result)) { echo "<table width='300' border='1' cellpadding='0' cellspacing='0' bordercolor='#FFFFFF'> <tr> <td bgcolor='#003366'> <div align='center'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'><strong>".$row["tema"]."</strong></font></div></td> </tr> <tr> <td height='71' valign='top' bgcolor='#CCCCCC'>".$row["texts"]."</td> </tr> <tr> <td align='right' bgcolor='#003366'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'>".$row["datums"]."</font></td> </tr> </table> <a href=\"delete.php?news_id=".$row["id"]."\"> Dzēst</a> "; }}} else echo "Neizdevaas nolasiit informaciju no DB."; mysql_close(); if(!isset($start)) $start = 0; $query = "SELECT * FROM $tablename LIMIT " . $start . ", 10"; //do database connection $result = mysql_query($query); //you should do error checking //display data //this code was wrong, I did not have the second query. Thanks to Plaggy Pig. // need another query to get the total amount of rows in our table $self=$_SERVER['PHP_SELF']; $query = "SELECT count(*) as count FROM $tablename"; $result = mysql_query($query); $row = mysql_fetch_array($result); $numrows = $row['count']; if($start > 0) echo "<a href=\$self?start=" . ($start - 10) . "\">Previous</a><BR>\n"; if($numrows > ($start + 10)) echo "<a href=\$self?start=" . ($start + 10) . "\">Next</a><BR>\n"; ?> </body> </html> Megginaaju visaadi, bet man nesanaak, nevaru atrast klluudu, bet man liekas, ka tur viss ir viena liela klluuda :D Link to comment Share on other sites More sharing options...
Aleksejs Posted January 28, 2004 Report Share Posted January 28, 2004 Paldies, ka vismaz rindas numuru pateici !!! :( Problēma, manuprāt, ir te: $query = "SELECT count(*) as count FROM $tablename"; //50 rindiņa Vajadzētu būt kaut kam līdzīgam: SELECT count(id) as count FROM $tablename Pie noteikuma, ka ir tāda id kolonna, protams. Link to comment Share on other sites More sharing options...
Gacha Posted January 29, 2004 Author Report Share Posted January 29, 2004 Tu domaa tur kur man ir tas echo, kursh izvada postu es to postu uztaisu piem kaa $posts un tad to ar echo izvadu. Un taas zvaigzniites vietaa ielieku $posts taa? Link to comment Share on other sites More sharing options...
jb4 Posted January 29, 2004 Report Share Posted January 29, 2004 Pieprasījumam "SELECT COUNT(*) as count FROM $tablename" nav ne vainas, ja ir definēts mainīgais $tablename; Kad parādās ar MySQL saistītās kļūdas, tad vislabāk uzreiz pēc kveriju izpildes (pēc mysql_query()) izsaukt echo mysql_error(); Ar šo Tu dabūsi MySQL kļūdas paziņojumu (ja kļūda būs pieprasījumā). Vari arī izdrukāt pieprasījumu, lai zinātu kāds tas izskatās pēc $tablename iestādīšanas: echo $tablename; Bet vispār, lai nākotnē viegli atrastu kļūdas savā kodā, iesaku turpmāk rakstīt tā: # visu skriptu sākumā ieliec kļūdu paziņojumu ieslēdzēju: error_reporting(E_ALL); ... # savienojamies ar DB serveri if ( !mysql_connect(...) ) { trigger_error('Nevaru savienoties ar DB serveri, jo nepareizs lietotajs/parole',E_USER_ERROR); } if ( !mysql_selectdb(...) ) { trigger_error('Nevaru izveleties datubazi',E_USER_ERROR); } ... # izpildām pieprasījumu if ( !$result = mysql_query($query) ) { trigger_error(mysql_error().'<br />SQL: '.$query,E_USER_ERROR); } // trigger_error('Kludas pazinojums',E_USER_ERROR) - komanda, ar kuras palīdzību tu izraisi FATAL error - programma šajā brīdī pārstās darboties un izdrukās kļūdas paziņojumu. Ļoti noderīga programmēšanas laikā. Link to comment Share on other sites More sharing options...
Gacha Posted January 29, 2004 Author Report Share Posted January 29, 2004 Nu es pamegginaju tos tiger errorus, bet tad man ir veel lielaaka putra. Tos salikshu peec tam. Nu tad taa - shis ir tas mans mega sasniegums news.php , kursh straadaa normaali <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=windows-1257"> </head> <body> <a href="post_news.php" target="_self">Ievietot postu</a> <? error_reporting(E_ALL); include_once("../protected/mysql_config.php"); $tablename = "news"; if (!mysql_connect($hostname,$dbuser,$dbpass)) {echo "Nevar pievienoties DB";} //izvadu informaciju no DB if (mysql_select_db ($dbname)) { $result = mysql_query ("Select * from $tablename order by id desc"); if (mysql_affected_rows() != 0) { while ($row = mysql_fetch_array ($result)) { echo "<table width='300' border='1' cellpadding='0' cellspacing='0' bordercolor='#FFFFFF'> <tr> <td bgcolor='#003366'> <div align='center'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'><strong>".$row["tema"]."</strong></font></div></td> </tr> <tr> <td height='71' valign='top' bgcolor='#CCCCCC'>".$row["texts"]."</td> </tr> <tr> <td align='right' bgcolor='#003366'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'>".$row["datums"]."</font></td> </tr> </table> <a href=\"delete.php?news_id=".$row["id"]."\"> Dzēst</a> "; }}} else echo "Neizdevaas nolasiit informaciju no DB."; ?> </body> </html> (Tur droshi vien veel pietrukst paaris error cheki, bet man pagaidaam pietiek) Un shis ir no viena turtoriala ar kuru vajadzetu uztaisiit Previous un next pogas. if(!isset($start)) $start = 0; $query = "SELECT * FROM $tablename LIMIT " . $start . ", 10"; $result = mysql_query($query); $self=$_SERVER['PHP_SELF']; $query = "SELECT count(*) as count FROM $tablename"; $result = mysql_query($query); $row = mysql_fetch_array($result); $numrows = $row['count']; if($start > 0) echo "<a href=\$self?start=" . ($start - 10) . "\">Previous</a><BR>\n"; if($numrows > ($start + 10)) echo "<a href=\$self?start=" . ($start + 10) . "\">Next</a><BR>\n"; Man shkjiet, ka vajadzetu nomainiit te $query pret $query1 un parejos taa pat, jo vinji pilda citas funkcijas atshkkiriibaa no taa tur aushaa kur ir taadi pashi. Vai arii taa nevajag dariit? Es taa apmeeram saprotu ko kura rindinja dara, bet ko dara shii $row = mysql_fetch_array($result); tieshi tas fech array? Un ja pielieku sho kodu zem news.php tad tas galiigi nestraadaa. Link to comment Share on other sites More sharing options...
persix Posted January 30, 2004 Report Share Posted January 30, 2004 if(!isset($start)) $start = 0; if (mysql_select_db ($dbname)) { $query = "SELECT * FROM $tablename LIMIT " . $start . ", 10"; $result = mysql_query($query); $self=$_SERVER['PHP_SELF']; $query = "SELECT count(*) as count FROM $tablename"; $result = mysql_query($query); $row = mysql_fetch_array($result); $numrows = $row['count']; while ($numrows) { echo "<table width='300' border='1' cellpadding='0' cellspacing='0' bordercolor='#FFFFFF'> <tr> <td bgcolor='#003366'> <div align='center'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'><strong>".$row["tema"]."</strong></font></div></td> </tr> <tr> <td height='71' valign='top' bgcolor='#CCCCCC'>".$row["texts"]."</td> </tr> <tr> <td align='right' bgcolor='#003366'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'>".$row["datums"]."</font></td> </tr> </table> <a href=\"delete.php?news_id=".$row["id"]."\"> Dzēst</a> "; }}} if($start > 0) echo "<a href=\$self?start=" . ($start - 10) . "\">Previous</a><BR>\n"; if($numrows > ($start + 10)) echo "<a href=\$self?start=" . ($start + 10) . "\">Next</a><BR>\n"; Pameigini, bet apsoluti neesu parliecinats! :D Sodien mana galva apsoluti nedoma! Link to comment Share on other sites More sharing options...
Gacha Posted January 30, 2004 Author Report Share Posted January 30, 2004 Tagad esmu skolaa man te nav nekas lai to pameginatu. Cik saprotu tad ar sho man ir jaizvieto viss tas mans news.php un saakumaa ir jaieraksta lai piekonektejas DB. ja? Link to comment Share on other sites More sharing options...
jb4 Posted January 30, 2004 Report Share Posted January 30, 2004 voila persix, tu tiešām šodien nedomā. gadījienā, ja SQL pieprasījums būs uzrakstīts pareizi un mysql_numrows() atgriezīs kaut ko vairāk par 0, tas varenais while ($numrows) iedzīs skriptu bezgalībā. tā vietā rakstām: while ( $row = mysql_fetch_assoc($result) ) ... pārējo nelasīju. turpmāk, kad šādus skriptus postējam, varētu izvairīties no tabulu ģenerēšanas html. tā vietā izlīdzēsimies ar vienkāršu html, kurš attēlo pašu svarīgāko attiecīgajā fragmentā. šajā reizē tie būtu paši linki. tā tabulas ģenerēšana traucē skripta lasīšanu un palīdzēšanu forumā. Link to comment Share on other sites More sharing options...
Gacha Posted January 30, 2004 Author Report Share Posted January 30, 2004 jb4 , bet ja es ierakstu tur while ( $row = mysql_fetch_assoc($result) ) , tad vinjsh vispaar neko neraada, tikai linkus un ja nospiezhu uz linku next tad paraadaas www.manalapa/db/news.php?id$self vai kaut kaa taa. Par to next es saprotu, ka tur kaut kas nav ar to linku iisti, kapeec vinjsh nejem pretii, ja $self=$_SERVER['PHP_SELF']; ir nodefineets? Link to comment Share on other sites More sharing options...
persix Posted January 30, 2004 Report Share Posted January 30, 2004 if (!mysql_connect($hostname,$dbuser,$dbpass)) {echo "Nevar pievienoties DB";} if(!isset($start)) $start = 0; //izvadu informaciju no DB if (mysql_select_db ($dbname)) { $result = mysql_query ("Select * from $tablename order by id desc LIMIT ".$start.", 10"); if (mysql_affected_rows() != 0) { while ($row = mysql_fetch_array ($result)) { echo "<table width='300' border='1' cellpadding='0' cellspacing='0' bordercolor='#FFFFFF'> <tr> <td bgcolor='#003366'> <div align='center'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'><strong>".$row["tema"]."</strong></font></div></td> </tr> <tr> <td height='71' valign='top' bgcolor='#CCCCCC'>".$row["texts"]."</td> </tr> <tr> <td align='right' bgcolor='#003366'><font color='#CCCCCC' size='-1' face='Geneva, Arial, Helvetica, sans-serif'>".$row["datums"]."</font></td> </tr> </table> <a href=\"delete.php?news_id=".$row["id"]."\"> Dzēst</a> "; }}} else echo "Neizdevaas nolasiit informaciju no DB."; $self= $_SERVER['PHP_SELF']; $query = "SELECT count(*) as count FROM $tablename"; $result = mysql_query($query); $row = mysql_fetch_array($result); $numrows = $row['count']; if($start > 0) echo "<a href=".$self."?start=".($start - 10).">Previous</a><BR>\n"; if($numrows > ($start + 10)) echo "<a href=".$self."?start=".($start + 10).">Next</a><BR>\n"; ?> </body> </html> Apskati sito un varbut tev kaut kas izdodass, vismaz man aizgaja. Link to comment Share on other sites More sharing options...
Vistu Zaglis Posted January 30, 2004 Report Share Posted January 30, 2004 (edited) $row = mysql_fetch_assoc($result) izveido masīvu (array), kas satur visus no datubāzes atgrieztos ierakstus. $row["texts"] tev neatgriezīs tekstu, jo masīvs $row tev satur pieprasījuma COUNT(*) rezultātus. Secība, vecīt... vai variabļu nosaukumi!!! Edited January 30, 2004 by Vistu Zaglis Link to comment Share on other sites More sharing options...
persix Posted January 30, 2004 Report Share Posted January 30, 2004 Sini gadijuma! Runa iet par to ka pariet pie nakosa teksta, nevis par kartosanu un visu pareju. :ph34r: Link to comment Share on other sites More sharing options...
jb4 Posted January 30, 2004 Report Share Posted January 30, 2004 (edited) Vistu Zagli, man šķiet, ka tu pats ļoti labi zini, ka mysql_fetch_assoc() neatgriež visus no DB atlasītos ierakstus, bet gan tikai tekošo asociatīvā masīva formā. Bez tam, $row['teksts'] satur tekstu. Jā, vēlāk tas satur ierakstu skaitu. Lapu linku ģenerēšanas loģika: http://paste.php.lv/725 (es nevarēju caurskatīt citu piedāvātos variantus, jo tajos autori pacentās iebliezt pretīgo html) Papildinu: Tur skriptā ir kļūda. Pie <$pagecount vajadzēja būt <=$pagecount Edited January 30, 2004 by jb4 Link to comment Share on other sites More sharing options...
Vistu Zaglis Posted January 30, 2004 Report Share Posted January 30, 2004 jb4 -> Es par šito koda daļu: $query = "SELECT * FROM $tablename LIMIT " . $start . ", 10"; $result = mysql_query($query); $self=$_SERVER['PHP_SELF']; $query = "SELECT count(*) as count FROM $tablename"; $result = mysql_query($query); $row = mysql_fetch_array($result); $numrows = $row['count']; Tavuprāt, kas šobrīd sēž masīvā $row? :) Link to comment Share on other sites More sharing options...
jb4 Posted January 30, 2004 Report Share Posted January 30, 2004 a es skatījos uz persixa otro kodu. pirmajā ir tā kā Tu raksti, bet otrajā šis jau pamanījies lietas izlabot ;] Link to comment Share on other sites More sharing options...
Recommended Posts