Connecting Tech Pros Worldwide Help | Site Map

issue with displaying data with php from mysql

Newbie
 
Join Date: Oct 2009
Posts: 31
#1: Oct 14 '09
Hi All,

The following script will display the grid, with its headers but will not display any data. Im sure ive made a mistake in the syntax somewhere but i can see the wood for the trees.

Ive ran the sql statement directly in mysql and it gives me results.

any help is greatly appreciated

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.     <title>CG0119 mysql - studentList.php</title>
  7. </head>
  8.  
  9. <body>
  10. <h1>Student Listing</h1>
  11.  
  12. <?php
  13.  
  14. // Connect to the Oracle database
  15. $link = mysqli_connect("localhost","root","root","university")
  16.  or die("ERROR: Could not connect : " . mysql_error());
  17.  
  18. // Set up a query to select the required fields and records
  19. $result = mysqli_query($link, "SELECT studentCode, forename, surname, startYear, typeDesc FROM student, studyType WHERE student.studyTypeID = studyType.studyTypeID")
  20. or die("ERROR:query failed (Student) - " . mysql_error());
  21.  
  22. $studentResult = mysqli_query($link, $sql);
  23. ?>
  24.  
  25. <!-- Move out of PHP mode and start the table tags (note you
  26. could have stayed in PHP mode and achieved the same
  27. result by placing these tags within echo statements. -->
  28.  
  29. <table width="70%" border="1">
  30. <tr>
  31. <td width="15%"><strong>Student Code</strong></td>
  32. <td width="25%"><strong>Surname</strong></td>
  33. <td width="25%"><strong>Forename</strong></td>
  34. <td width="20%"><strong>Study Type</strong></td>
  35. <td width="15%"><strong>Start Year</strong></td>
  36. </tr>
  37.  
  38. <?php
  39.  
  40. // For every row that was returned by the query, do something.
  41. // In this example, display data from each row within an XHTML table.
  42.  
  43. // NOTE: The array keys (the field names are the array keys, the values are the field values returned by the SQL query) 
  44.  
  45.                echo "<tr>\n";
  46.                echo "<td>&nbsp;</td>\n";
  47.                echo "<td>".$studentResult['studentCode']."</td>\n";
  48.                echo "<td>".$studentResult['surname']."</td>\n";
  49.                echo "<td>".$studentResult['forename']."</td>\n";
  50.                echo "<td>".$studentResult['studyType']."</td>\n";
  51.                echo "<td>".$studentResult['startYear']."</td>\n";
  52.                echo "</tr>\n";    
  53.  
  54. // General clean-up of resources (i.e. memory).
  55.  
  56. mysqli_free_result($studentResult);
  57. mysqli_close($link);
  58.  
  59. ?>
  60.  
  61. </body>
  62. </html>
  63.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Oct 14 '09

re: issue with displaying data with php from mysql


you should get some useful error messages
#22 $sql not defined
#47-51 (?) $studentResult is not an array

and of course you don't fetch data anywhere...
Newbie
 
Join Date: Oct 2009
Posts: 31
#3: Oct 14 '09

re: issue with displaying data with php from mysql


thank you for this, i get no error messages at all.

ill take a look and try to fix
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Oct 14 '09

re: issue with displaying data with php from mysql


Quote:

Originally Posted by whitep8 View Post

thank you for this, i get no error messages at all.

this may be due to suppressed error reporting
Newbie
 
Join Date: Oct 2009
Posts: 31
#5: Oct 14 '09

re: issue with displaying data with php from mysql


i think im having a thick morning.

ive added

error_reporting(E_ALL);
ini_set('display_errors', '1');

into php.ini

you know any good labotomy doctors?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Oct 14 '09

re: issue with displaying data with php from mysql


Quote:

Originally Posted by whitep8 View Post

you know any good labotomy doctors?

shall I recommend a good decapitator?
Newbie
 
Join Date: Oct 2009
Posts: 31
#7: Oct 14 '09

re: issue with displaying data with php from mysql


lol i think thats a good idea.

it should be in .htaccess shouldnt it
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#8: Oct 14 '09

re: issue with displaying data with php from mysql


Quote:

Originally Posted by whitep8 View Post

it should be in .htaccess shouldnt it

although you can place it there I rather recommend it to be in the PHP file* (it is easyto forget that PHP relevant code is placed in .htaccess and then you have the time of your life finding the setting)

* - especially if it is temporary
Newbie
 
Join Date: Oct 2009
Posts: 31
#9: Oct 14 '09

re: issue with displaying data with php from mysql


Ok, i dont think im going totally mad.

I have created a php.ini AND a php5.ini file. both with error activation.

I have simplified everything right down to the following but it still shows nothing.

its so annoying

Expand|Select|Wrap|Line Numbers
  1.  <?php
  2.  
  3. // Connect to the Oracle database
  4. $connection = mysql_connect("localhost","root","root","university")
  5.  or die("ERROR: Could not connect : " . mysql_error());
  6.  
  7. // Set up a query to select the required fields and records
  8. $sql = "SELECT studentCode FROM student";
  9.  
  10.  
  11. echo $sql;
  12.  
  13. $query = mysql_query($sql);
  14.  
  15. echo $query;
  16. while ($row = mysql_fetch_array($query))    {
  17.  
  18.                echo "<p>", $row['studentCode'];
  19. }
  20.  
  21.  
  22. ?>
Newbie
 
Join Date: Oct 2009
Posts: 31
#10: Oct 14 '09

re: issue with displaying data with php from mysql


sorted

error reporting is switched on again. im using MAMP. silly thing, stupid me
Reply