phenner Posted April 5, 2010 Share Posted April 5, 2010 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! Quote Link to comment Share on other sites More sharing options...
web4 Posted April 5, 2010 Share Posted April 5, 2010 mysql_connect(localhost,$username,$password); --> i guess how about "localhost" ??? inside the quotes? or check the database if it has some values...that's only my guess.. Hope that helps Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 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 Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 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'; } Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 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?! Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 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) { Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
web4 Posted April 5, 2010 Share Posted April 5, 2010 why not trying the source first?? var_dump($scoresq); ?? then tell us what the var_dump tells about the resource...copy and paste it here Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 resource(3) of type (mysql result) Quote Link to comment Share on other sites More sharing options...
web4 Posted April 5, 2010 Share Posted April 5, 2010 then that's seems okay....now my next guess is your $scoresb['ID']; <--- ID is not a valid field of your table scores.... Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 It is in fact 'id' rather than 'ID', but that doesn't make any difference..... Quote Link to comment Share on other sites More sharing options...
web4 Posted April 5, 2010 Share Posted April 5, 2010 but first change it to id sometimes php is "case sensitive" then tell us what's wrong when you already changed it Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 Can you include all of your for the page that is having trouble so we can see where the problem is. Sounds like you are still using if(isset($id)) Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 <!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> Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 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. } Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 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> Quote Link to comment Share on other sites More sharing options...
web4 Posted April 5, 2010 Share Posted April 5, 2010 Yes, 182guy is correct you can change the code in his other post.. Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 It is going into the IF statement, just not into the while-loop or the while-loop is outputting nothing. It is effectively missing out the while-loop entirely for some reason........ Quote Link to comment Share on other sites More sharing options...
web4 Posted April 5, 2010 Share Posted April 5, 2010 Okay then try this...before the loop... $scoresb = mysql_fetch_array($scoresq); then... var_dump($scoresb); if it says something bool(false) then you know what is wrong... Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
phenner Posted April 5, 2010 Author Share Posted April 5, 2010 Solved! I only had one record and thus was receiving a bool(false) error. I have now added another record and it works great! Thanks for your help guys! Quote Link to comment 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.