Jump to content

mysql_fetch_array loop not working


phenner

Recommended Posts

I am attempting to print out the values from the database into a list but with no success.

 

For some reason it just won't go into the loop.

 

Any ideas guys?

 


$username="root";
$password="";
$database="cricket";
mysql_connect(localhost,$username,$password);

mysql_select_db($database) or die( "Unable to select database");

$scoresq = mysql_query("SELECT * FROM `scores`") or die(mysql_error());

echo '<ul>';

while($scoresb = mysql_fetch_array($scoresq)){

echo 'hello?';

$x = $scoresb['ID'];

echo '<li><a href="index.php?id='.$x.'">'.$scoresb['matchname'].'-'.$scoresb['date'].'</a></li>';

}

echo '</ul>';

 

Thanks!

Link to comment
Share on other sites

Thanks for your reply!

 

The database definitely has values and when I use index.php?id=1 for example, I get the printed results.

 

It is just a small test project running on a XAMPP server so that is why it is localhost, but having tried your suggestion I am faced with the same blank screen as before :(

Link to comment
Share on other sites

web4 is right about using localhost with no quotes, but if this is used then it will default to 'localhost:3306', however still needs correcting.

 

Add a check to see if mysql could successfully connect, and add error reporting incase there is a fatal error somewhere, which is usually what a blank screen means.

 

// show errors for debugging
error_reporting(E_ALL);
ini_set('display_errors', '1');

$username="root";
$password="";
$database="cricket";
$conn = mysql_connect('localhost',$username,$password);

if(!$conn)
{
  echo 'could not connect to the database';
}

Link to comment
Share on other sites

Thanks for your replies guys.

 

It is defintately connecting to the database as $conn is true.

 

It does however produce this notice when I added the errors thing at the top.

 

Notice: Undefined index: id in C:\xampp\htdocs\cricket\index.php on line 51

 

This line is:

 

$id = $_GET['id'];

 

My code then checks if $id is set or not:

 

if(!isset($id)){

 

If $id is set then it produces the table of results from the database with the ID number $id.

 

If $id is not set then it proceeds to the while loop section:

 

echo '<ul>';

while($scoresb = mysql_fetch_array($scoresq)){

echo 'hello?';

$x = $scoresb['ID'];

echo '<li><a href="index.php?id='.$x.'">'.$scoresb['matchname'].'-'.$scoresb['date'].'</a></li>';

}

echo '</ul>';

}

 

It goes into the IF statement, but does not ever go into the while-loop.

 

Hope that helps you help?!

 

Link to comment
Share on other sites

Thanks for your replies guys.

 

It is defintately connecting to the database as $conn is true.

 

It does however produce this notice when I added the errors thing at the top.

 

Notice: Undefined index: id in C:\xampp\htdocs\cricket\index.php on line 51

 

This line is:

 

$id = $_GET['id'];

 

My code then checks if $id is set or not:

 

if(!isset($id)){

 

If $id is set then it produces the table of results from the database with the ID number $id.

 

If $id is not set then it proceeds to the while loop section:

 

echo '<ul>';

while($scoresb = mysql_fetch_array($scoresq)){

echo 'hello?';

$x = $scoresb['ID'];

echo '<li><a href="index.php?id='.$x.'">'.$scoresb['matchname'].'-'.$scoresb['date'].'</a></li>';

}

echo '</ul>';

}

 

It goes into the IF statement, but does not ever go into the while-loop.

 

Hope that helps you help?!

 

That's the problem then....

 

if(!isset($id))  can never be true because you set $id = $_GET['id']

 

Even if $_GET['id'] is not set, $id will still end up being set to blank. So isset($id) will always be true. Try using

 

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0; // if $_GET['id'] is set then set to that (casted as int), if not set to 0.

if($id > 0)
{

Link to comment
Share on other sites

I think you are missing the point of what I am trying to achieve here.

 

If the URL is: index.php then output a list of records in the database.

 

If the URL is: index.php?id=# (where # is a number(id))  then output the data for that record.

 

Your method does however remove the NOTICE error which came up before, but now the system is trying to display a record but has no data for it as the ID = 0.

 

The main problem is that the code is NEVER going into the while loop to fetch the data for the list.

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cricket Scorer - Live Scores</title>
<style type="text/css">
<!--
body p {
font-family: Verdana, Geneva, sans-serif;
}
.results {
font-family: Verdana, Geneva, sans-serif;
}
.bold {
font-family: Verdana, Geneva, sans-serif;
font-weight: bold;
}
-->
</style>
</head>
<body>

<?php
// show errors for debugging
error_reporting(E_ALL);
ini_set('display_errors', '1');

$username="root";
$password="";
$database="cricket";
$conn = mysql_connect('localhost',$username,$password);

if(!$conn)
{
  echo 'could not connect to the database';
}else{

mysql_select_db($database) or die( "Unable to select database");

$scoresq = mysql_query("SELECT * FROM `scores`") or die(mysql_error());

var_dump($scoresq);

$scoresa = mysql_fetch_array($scoresq);

$teams = $scoresa['matchname'];

$spliteams = explode("_", $teams);

$team1name = $spliteams[0];
$team2name = $spliteams[1];

$id = $_GET['id'];

if(!isset($id))
{

echo '<ul>';

while($scoresb = mysql_fetch_array($scoresq)){

echo 'hello?';

$x = $scoresb['id'];

echo '<li><a href="index.php?id='.$x.'">'.$scoresb['matchname'].'-'.$scoresb['date'].'</a></li>';

}

echo '</ul>';

}

else{

$scoresqa = mysql_query("SELECT * FROM `scores` WHERE id = '$id'") or die(mysql_error());

$scoresc = mysql_fetch_array($scoresqa);

?>

<div class="bold"><?php echo $team1name; ?></div>


<p>SCORE: <?php echo $scoresc['team1runs']; ?> for <?php echo $scoresc['team1wkts']; ?></p>
<p>OVERS: <?php echo $scoresc['team1overs']; ?></p><br />

<table border="1">

<th class="results">No.</th>
  <th class="results">Name</th>
  <th class="results">How Out</th>
  <th class="results">Runs</th>
  <th class="results">Balls</th>

<tr><td class="results">1</td>
  <td class="results"><?php echo $scoresc['team1bat1name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat1howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat1runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat1balls']; ?></td>
</tr>
<tr><td class="results">2</td>
  <td class="results"><?php echo $scoresc['team1bat2name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat2howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat2runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat2balls']; ?></td>
</tr>
<tr><td class="results">3</td>
  <td class="results"><?php echo $scoresc['team1bat3name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat3howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat3runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat3balls']; ?></td>
</tr>
<tr><td class="results">4</td>
  <td class="results"><?php echo $scoresc['team1bat4name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat4howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat4runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat4balls']; ?></td>
</tr>
<tr><td class="results">5</td>
  <td class="results"><?php echo $scoresc['team1bat5name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat5howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat5runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat5balls']; ?></td>
</tr>
<tr><td class="results">6</td>
  <td class="results"><?php echo $scoresc['team1bat6name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat6howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat6runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat6balls']; ?></td>
</tr>
<tr><td class="results">7</td>
  <td class="results"><?php echo $scoresc['team1bat7name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat7howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat7runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat7balls']; ?></td>
</tr>
<tr><td class="results">8</td>
  <td class="results"><?php echo $scoresc['team1bat8name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat8howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat8runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat8balls']; ?></td>
</tr>
<tr><td class="results">9</td>
  <td class="results"><?php echo $scoresc['team1bat9name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat9howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat9runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat9balls']; ?></td>
</tr>
<tr><td class="results">10</td>
  <td class="results"><?php echo $scoresc['team1bat10name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat10howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat10runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat10balls']; ?></td>
</tr>
<tr><td class="results">11</td>
  <td class="results"><?php echo $scoresc['team1bat11name']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat11howout']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat11runs']; ?></td>
  <td class="results"><?php echo $scoresc['team1bat11balls']; ?></td>
</tr>

</table>

<br /><br />

<div class="bold"><?php echo $team2name; ?></div>


<p>SCORE: <?php echo $scoresc['team2runs']; ?> for <?php echo $scoresc['team2wkts']; ?></p>
<p>OVERS: <?php echo $scoresc['team2overs']; ?></p><br />

<table border="1">

<th class="results">No.</th>
  <th class="results">Name</th>
  <th class="results">How Out</th>
  <th class="results">Runs</th>
  <th class="results">Balls</th>

<tr><td class="results">1</td>
  <td class="results"><?php echo $scoresc['team2bat1name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat1howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat1runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat1balls']; ?></td>
</tr>
<tr><td class="results">2</td>
  <td class="results"><?php echo $scoresc['team2bat2name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat2howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat2runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat2balls']; ?></td>
</tr>
<tr><td class="results">3</td>
  <td class="results"><?php echo $scoresc['team2bat3name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat3howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat3runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat3balls']; ?></td>
</tr>
<tr><td class="results">4</td>
  <td class="results"><?php echo $scoresc['team2bat4name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat4howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat4runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat4balls']; ?></td>
</tr>
<tr><td class="results">5</td>
  <td class="results"><?php echo $scoresc['team2bat5name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat5howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat5runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat5balls']; ?></td>
</tr>
<tr><td class="results">6</td>
  <td class="results"><?php echo $scoresc['team2bat6name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat6howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat6runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat6balls']; ?></td>
</tr>
<tr><td class="results">7</td>
  <td class="results"><?php echo $scoresc['team2bat7name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat7howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat7runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat7balls']; ?></td>
</tr>
<tr><td class="results">8</td>
  <td class="results"><?php echo $scoresc['team2bat8name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat8howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat8runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat8balls']; ?></td>
</tr>
<tr><td class="results">9</td>
  <td class="results"><?php echo $scoresc['team2bat9name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat9howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat9runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat9balls']; ?></td>
</tr>
<tr><td class="results">10</td>
  <td class="results"><?php echo $scoresc['team2bat10name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat10howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat10runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat10balls']; ?></td>
</tr>
<tr><td class="results">11</td>
  <td class="results"><?php echo $scoresc['team2bat11name']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat11howout']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat11runs']; ?></td>
  <td class="results"><?php echo $scoresc['team2bat11balls']; ?></td>
</tr>

</table>

<?php
}
}
?>

</body>

</html>

Link to comment
Share on other sites

The code inside the IF will never be executed (ie the while loop), like I said in my other post, because of this part:

 

$id = $_GET['id']; // you have now set $id so isset($id) will always return true not matter what the value is

if(!isset($id)) // can't possibly enter this if statement because $id has been set.
{
    // never executed

 

It's the same as doing

 

if(false)
{
   // will this code here run? No never.
}

 

Link to comment
Share on other sites

But it is going into the IF statement as it prints out the <ul> and </ul> tags as shown below in the source:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Cricket Scorer - Live Scores</title> 
<style type="text/css"> 
<!--
body p {
font-family: Verdana, Geneva, sans-serif;
}
.results {
font-family: Verdana, Geneva, sans-serif;
}
.bold {
font-family: Verdana, Geneva, sans-serif;
font-weight: bold;
}
-->
</style> 
</head> 
<body> 

resource(3) of type (mysql result)
<br /> 
<ul></ul> 
</body> 

</html>

 

Link to comment
Share on other sites

How many records are there in the table?

 

If there is only 1 record then the while loop won't execute because you have already called mysql_fetch_array() once before the while loop, which will have moved the internal result pointer onto the next record when the while loop runs, so if there is no second record it won't be executed.

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.