dlmagers Posted February 23, 2013 Share Posted February 23, 2013 I didn't want to have to post this question. I need someones professional help with connecting to my school phpmysql server. I have my code here.</p> If someone sees that there might be something wrong please please help me and at least point me in the right direction.<br /> What I need to do is extract data from a table that I have created employee_directory that I need to display in a table on a responsive site. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Phone Directory</title> <!--Mobile specific meta goodness --> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--google fonts--> <link href='http://fonts.googleapis.com/css? family=PT+Sans:400,700|PT+Serif:400,700,400italic' rel='stylesheet' type='text/css'> <!--css--> <link rel="stylesheet" type="text/css" href="main.css" /> <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!--[if !IE]><!--> <style> /* This query will take effect for any screen smaller than 760px and also iPads specifically. */ @media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px) { /* Force table to not be like tables anymore */ #employeedb { border:1px solid #000; border-collapse:collapse; font-family:Vollkorn; font-size:12px; text-align:right; } #employeedb th { border: 1px solid #333; padding: 3px 6px; color: #e9edd5; } #employeedb td { border:1px solid #999; padding:3px 6px; } table, thead, tbody, th, td, tr { display: block; color: #c6f104; font-family: Vollkorn; margin-right: auto; margin-left: auto; padding-left: 5px; } /* Hide table headers (but not display: none;, for accessibility) */ thead tr { position: absolute; top: -9999px; left: -9999px; } tr { border: 1px solid #ccc; } td { /* Behave like a "row" */ border: none; border-bottom: 1px solid #eee; position: relative; padding-left: 50%; } td:before { /* Now like a table header */ position: absolute; /* Top/left values mimic padding */ top: 6px; left: 6px; width: 45%; padding-right: 10px; white-space: nowrap; } /* Label the data */ td:nth-of-type(1):before { content: "ID"; } td:nth-of-type(2):before { content: "Last Name"; } td:nth-of-type(3):before { content: "Cell Number"; } td:nth-of-type(4):before { content: "Email"; } } /* Smartphones (portrait and landscape) ----------- */ @media only screen and (min-device-width : 320px) and (max-device-width : 480px) { body { padding: 0; margin: 0; width: 320px; } } /* iPads (portrait and landscape) ----------- */ @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) { body { width: 495px; } } .wrapper .background-illusion h2 { color: #d4f24e; font-family: Vollkorn; font-size: 29px; } body table tr th h3 { color: #c6f104; } body div table tr th { color: #B0F104; } </style> </head> <body> <div class="wrapper"> <header class="background-illusion"> <h2 align="center">Phone Directory</h2> <p align="center">for Student Admin Department at WTCC<br> WEB 000 | Assignment 7<br> </p> </header> </div> <nav class="nav right"> <ul> <li class="current"><a href="index.php">Home</a></li> <li><a href="#">Assignments</a></li> <li><a href="#">Discussion Board</a></li> <li><a href="#">Support</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <table width="85%"> <tr> <th>ID</th> <th>Last Name</th> <th>Cell</th> <th>Email</th> </tr> <tr> <?php $dbhost = '198.86.244.3/phpmyadmin/'; $dbuser = 'XXXX_dbXXXXXX'; $dbpass = 'DxXxXxX'; // Connect to database server $conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die (mysql_error ()); echo "You are now connected to MySQL<br /><br />"; // Select database $dbname = 'employee_directory'; mysql_select_db($dbname); // Get data from the database depending on the value of the id in the URL $query = "SELECT * FROM employee_directory ORDER BY lastname"; $result = mysql_query($query) or die(mysql_error()); ?> <?php while($row = mysql_fetch_array($result)); ?> <td><?php echo $row['id']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['cellphone']; ?></td> <td><?php echo $row['email']; ?></td> </tr> </table> <footer class="background-illusion"> <div class="container"> <div align="center"> <p>Copyright © 2013 | <a href="http://dianamagers.com">XXXXXXX</a> | Spring Semester 2013 </p></div> </div> </footer> </div> </body> </html> Of course, I couldn't provide my username and password ..... Sorry. I don't need to know that. This is what it looks like on the server now. http://wcet2.waketec...es(1)/index.php Quote Link to comment Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 (edited) Where is the while loop supposed to finish? Looking at your code, it will only run on the first line.. and not loop over any code block.. You should use curley braces to wrap the code block that you actually want to loop over. Denno Edited February 23, 2013 by denno020 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2013 Share Posted February 23, 2013 For the record - phpmyadmin is a PHP based tool for viewing a mysql database. It is not a server and does not contain your database. Quote Link to comment Share on other sites More sharing options...
dlmagers Posted February 24, 2013 Author Share Posted February 24, 2013 Denno, Ok, should I put curly brackets after the while statement? And Gurus, so maybe I should use the ip address in from of the phpmyadmin? I think I need to fix the error that Denno saw in my code first. This is the error I am getting now. "; // Select database $dbname = 'employee_directory'; mysql_select_db($dbname); // Get data from the database depending on the value of the id in the URL $query = "SELECT * FROM employee_directory ORDER BY lastname"; $result = mysql_query($query) or die(mysql_error()); ?> { } Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2013 Share Posted February 24, 2013 That's not an error, that's code. Yes you should use a specific host, not a folder on the host. Quote Link to comment Share on other sites More sharing options...
denno020 Posted February 24, 2013 Share Posted February 24, 2013 (edited) <?php while($row = mysql_fetch_array($result)){ //Opening curly brace?> <td><?php echo $row['id']; ?></td> <td><?php echo $row['lastname']; ?></td> <td><?php echo $row['cellphone']; ?></td> <td><?php echo $row['email']; ?></td> <?php } //closing curly brace?> Everything is the curly braces will be run for every row in your mysql result set. Personally, I would write it like this too, especially seeing as though you're already using echo: <?php while($row = mysql_fetch_array($result)){ //Opening curly brace echo "<td>".$row['id']."</td>"; echo "<td>".$row['lastname']."</td>"; echo "<td>".$row['cellphone']."</td>"; echo "<td>".$row['email']."</td>"; } //closing curly brace ?> Edited February 24, 2013 by denno020 Quote Link to comment Share on other sites More sharing options...
dlmagers Posted February 24, 2013 Author Share Posted February 24, 2013 (edited) Denno, I used your advice and when I bring it up in the browser I am not seeing what I need to see. I am not even seeing any HTML <p> or <h2>. I think I am doing something else wrong. This is what I changed it to. <?php $dbhost = 'localhost'; $dbuser = 'w187_dxxxxxxx'; $dbpass = 'Dxxxxxxx'; // Connect to database server $conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die (mysql_error ()); echo "You are now connected to MySQL<br /><br />"; // Select database $dbname = 'employee_directory'; mysql_select_db($dbname); // Get data from the database depending on the value of the id in the URL $query = "SELECT * FROM employee_directory ORDER BY lastname"; $result = mysql_query($query) or die(mysql_error()); ?> <table width="50%"> <tr> <th width="3%">ID</th> <th width="48%">Last Name</th> <th width="25%">Cell</th> <th width="24%">Email</th> </tr> <tr> <?php while($row = mysql_fetch_array($result)){ //Opening curly brace echo "<td>".$row['id']."</td>"; echo "<td>".$row['lastname']."</td>"; echo "<td>".$row['cellphone']."</td>"; echo "<td>".$row['email']."</td>";} //closing curly brace?> </tr> </table> Can you see anything else that might throw it off? Thank you in advance. I appreciate your opinion. Edited February 24, 2013 by dlmagers Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2013 Share Posted February 24, 2013 Try fetch_assoc() and do a print_r() on $row Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2013 Share Posted February 24, 2013 Oh and remove the space between mysql_error and the (). Quote Link to comment Share on other sites More sharing options...
daviddivine Posted February 24, 2013 Share Posted February 24, 2013 Here's what you might really want to do : : Also lookout for the "You are now Connected to MySQL" text as echoed upon a succeful connection. If this doesn't work, you might have to look at your connection details <?php $dbhost = 'localhost'; $dbuser = 'w187_dxxxxxxx'; $dbpass = 'Dxxxxxxx'; // Connect to database server $conn = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die (mysql_error()); echo "You are now connected to MySQL<br /><br />"; // Select database $dbname = 'employee_directory'; mysql_select_db($dbname,$conn);#Connection call added as second parameter // Get data from the database depending on the value of the id in the URL $query = "SELECT * FROM employee_directory ORDER BY lastname"; $result = mysql_query($query) or die("Query Could Not Be Established".mysql_error()); ?> <table width="50%"> <tr> <th width="3%">ID</th> <th width="48%">Last Name</th> <th width="25%">Cell</th> <th width="24%">Email</th> </tr> <?php $rows = array(); while($allrows = mysql_fetch_array($result))$rows []= $allrows; foreach ($rows AS $row) { echo "<tr>"; echo "<td>".$row['id']."</td>"; echo "<td>".$row['lastname']."</td>"; echo "<td>".$row['cellphone']."</td>"; echo "<td>".$row['email']."</td>"; echo "</tr>"; } //clossing brackets ?> </table> Quote Link to comment Share on other sites More sharing options...
dlmagers Posted February 24, 2013 Author Share Posted February 24, 2013 David, Ok first of all thank you. I tried the code and I did a couple tweeking and it is giving me a message in the browser the I have connect to mysql but then on the next line it is telling me that the Query could not be established because no database was selected. Not sure what I did. My URL is here. http://wcet2.waketech.edu/dlmagers/WEB187/PhoneDirectoryPageFiles%281%29/index.php Quote Link to comment Share on other sites More sharing options...
daviddivine Posted February 25, 2013 Share Posted February 25, 2013 Hi, this is probably because your SQL query is querying from the dbname (employee_directory, which is probably wrong) rather than the table. (Maybe .. employees or something..). A correct query should look like this : SELECT * FROM `employee_directory`.`tablename` ORDER BY lastname; All make sure that your database name is really "employee_directory" and not just a tablename in the database. I really doubt if both the queried table and dbname have the same name (employee_directory). Good luck! 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.