Jump to content

mysql_fetch_array in a while loop not working


nasir1123

Recommended Posts

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.";
	}	

}

Link to comment
Share on other sites

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.";
}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.