nasir1123 Posted September 16, 2011 Share Posted September 16, 2011 Hello all, I am having trouble with a piece of code. I have used the same piece of code on a different server and I had no problems. Suddenly I do. - I use this code to load info into a Flash website to browse through all rows in a table; - Inside the table currently there is one record; - NumLow is being passed through the url; - echo "&aantalEvenementen=".$aantalEvenementen; shows 1 record just fine; - the record doesn't get printed though... the error is somewhere in the while loop. Hope someone can point me in the right direction! Cheers, // Connect to mySQL Server $DBConn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Error bij het verbinden met de MYSQL server: " . mysql_error()); // Select mySQL Database mysql_select_db($DBName, $DBConn) or die("Error bij het verbinden met de MYSQL database: " . mysql_error()); //amount of rows that are shown at once $numComments = 1; //select the right table $sql = "SELECT * FROM $table"; //loading events $nieuweEvenementen = mysql_query($sql, $DBConn) or die("Error bij het ophalen van de concerten: " . mysql_error()); //amount of rows $aantalEvenementen = mysql_num_rows($nieuweEvenementen); //order rows $sql .= ' ORDER BY `concertDatum` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments; //loaded row $geladenEvenement = mysql_query($sql, $DBConn) or die("Error bij het laden van het concert: " . mysql_error()); //echo amount of rows echo "&aantalEvenementen=".$aantalEvenementen; //if no records, display message, otherwise print row if($aantalEvenementen == 0) { echo "&concertOmschrijving=Er zijn geen concerten. U kunt nieuwe concerten aanmaken via de concertenkalender."; } else { while ($array = mysql_fetch_array($geladenEvenement)) { $concertNaam = mysql_result($geladenEvenement, $i, 'concertNaam'); $concertArtiest = mysql_result($geladenEvenement, $i, 'concertArtiest'); $concertDatum = mysql_result($geladenEvenement, $i, 'concertDatum'); $concertTijd = mysql_result($geladenEvenement, $i, 'concertTijd'); $concertOpen = mysql_result($geladenEvenement, $i, 'concertOpen'); $concertOmschrijving = mysql_result($geladenEvenement, $i, 'concertOmschrijving'); echo "&concertNaam=".$concertNaam; echo "&concertArtiest=".$concertArtiest; echo "&concertDatum=".$concertDatum; echo "&concertTijd=".$concertTijd; echo "&concertOpen=".$concertOpen; echo "&concertOmschrijving=".$concertOmschrijving; $i++; } } // if there are no more records (all are loaded), show message if($_GET['NumLow'] > $aantalEvenementen) { echo "$eventOmschrijving=Alle concerten zijn reeds geladen."; } } Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/ Share on other sites More sharing options...
gristoi Posted September 16, 2011 Share Posted September 16, 2011 where are you declaring $i? $concertNaam = mysql_result($geladenEvenement, $i, 'concertNaam'); Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269862 Share on other sites More sharing options...
WebStyles Posted September 16, 2011 Share Posted September 16, 2011 you're also doing this: $sql .= ' ORDER BY `concertDatum` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments; without first checking if $_GET['NumLow'] exists or not. and again, at the end, this piece of code will throw a warning and fail if $_GET['NumLow'] is not present and where does $eventOmschrijving come from? if($_GET['NumLow'] > $aantalEvenementen) { echo "$eventOmschrijving=Alle concerten zijn reeds geladen."; } Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269872 Share on other sites More sharing options...
web_craftsman Posted September 16, 2011 Share Posted September 16, 2011 sorry for offtop but I often see that people here use mysql_fetch_array. I have been taught that this is bad style and you need to use mysql_fetch_assoc instead(if you do not need nubmer keys why ask php to create them). Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269933 Share on other sites More sharing options...
nasir1123 Posted September 16, 2011 Author Share Posted September 16, 2011 you're also doing this: $sql .= ' ORDER BY `concertDatum` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments; without first checking if $_GET['NumLow'] exists or not. and again, at the end, this piece of code will throw a warning and fail if $_GET['NumLow'] is not present and where does $eventOmschrijving come from? if($_GET['NumLow'] > $aantalEvenementen) { echo "$eventOmschrijving=Alle concerten zijn reeds geladen."; } Ok well I understand what you are saying, but $_GET will always exist. Without it, the script won't be launched (there is a check in Flash for that). $eventOmschrijving should've been &concertOmschrijving , a silly mistake. But this won't solve my problem unfortunately. Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269963 Share on other sites More sharing options...
nasir1123 Posted September 16, 2011 Author Share Posted September 16, 2011 where are you declaring $i? $concertNaam = mysql_result($geladenEvenement, $i, 'concertNaam'); That's a good question! This is an older piece of code I have used many times and it has always worked. But on this different webserver, it doesn't. I checked my previous php scripts and I have never declared $i before. So maybe it depends on the PHP configuration of the server, whether it's accepted or not. $i is supposed to stand for the rownumber that is currently loaded. I guess I always thought that mysql_fetch_array declared $i . So how can I declare the $i ? The purpose of this script is to select all rows, but only load and display one at the same time. So when the script is loaded, it's supposed to show the first (1) row of those that are loaded. In my flash movie there are two buttons, next and prev, that will add a -1 or +1 to the $_GET['NumLow'] variabele. That way you control which row number of the rows that are loaded, is displayed. Which is why I used mysql_fetch_array by the way, because I need to control the rownumber as well (in response to web_craftsman). Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269970 Share on other sites More sharing options...
gristoi Posted September 16, 2011 Share Posted September 16, 2011 instead of $concertNaam = mysql_result($geladenEvenement, $i, 'concertNaam'); use $concertNaam =$array['concertNaam']; and change while ($array = mysql_fetch_array($geladenEvenement)) { to while ($array = mysql_fetch_assoc($geladenEvenement)) { Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269986 Share on other sites More sharing options...
WebStyles Posted September 16, 2011 Share Posted September 16, 2011 Ok well I understand what you are saying, but $_GET will always exist. Without it, the script won't be launched (there is a check in Flash for that). $eventOmschrijving should've been &concertOmschrijving , a silly mistake. But this won't solve my problem unfortunately. the probelm isn't exactly the fact that you're sure it will always exist... what if a malicious user tries to access the script directly without that variable? it will make your server throw an error and possible release some information about your database... You should make sure your scripts all work AND are protected from whatever everyone else might try. Quote Link to comment https://forums.phpfreaks.com/topic/247253-mysql_fetch_array-in-a-while-loop-not-working/#findComment-1269999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.