about2flip Posted December 21, 2008 Share Posted December 21, 2008 Hi: I want to place data in mysql DB in two columns. I want to place sandwich in one column, and then the price in another column. It is a restaurant menu. I am using the following code: Thanks for your help <?php $username = "user"; $password = "password"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Fresh Sandwich Menu<br><br>"; //select a database to work with $selected = mysql_select_db("menus",$dbhandle) or die("Could not select menus"); //execute the SQL query and return records $result = mysql_query("SELECT id, sandwich, price FROM sandwiches"); //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo " ".$row{'sandwich'}." ". //display the results $row{'price'}."<br>"; } //close the connection mysql_close($dbhandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/ Share on other sites More sharing options...
Maq Posted December 21, 2008 Share Posted December 21, 2008 Where is this data coming from? All you do in your script is display all the sandwiches and id's from the sandwiches table. You need to use INSERT. But again, where is this data you would like to 'place in your mysql DB'? Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721007 Share on other sites More sharing options...
about2flip Posted December 21, 2008 Author Share Posted December 21, 2008 I'm pulling the data from my MySql DB. I want to display in a table the data that is in the table sandwiches. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721019 Share on other sites More sharing options...
Maq Posted December 22, 2008 Share Posted December 22, 2008 If you want a table you need to use HTML table tags to format it. Did you get an errors? Try this: //execute the SQL query and return records $result = mysql_query("SELECT id, sandwich, price FROM sandwiches") or die(mysql_error()); //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo $row['sandwich'] . " " . $row['price'] . " "; //display the results } Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721026 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 That made the screen blank. It is not printing anything. Any way I can tab between the sandwich and the price? I spend all day googling, reading php books, and I can't figure out how to get the two into a neat column. Thanks again for the help you are suggesting. Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721046 Share on other sites More sharing options...
Maq Posted December 22, 2008 Share Posted December 22, 2008 Yeah that mean you have an error somewhere. Add this to the top of your script: ini_set ("display_errors", "1"); error_reporting(E_ALL); I spend all day googling, reading php books, and I can't figure out how to get the two into a neat column. Like I said, you need to use HTML table tags to format neatly. Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721062 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 I tried what you told me and I didn't get any errors, but it still did not do anything. I'm trying to use the HMTL TABLE TAGS, but I'm stuck here: echo "<table width=298 border=0 cellspacing=0 cellpadding=0>"; echo "<tr>"; echo $row['sandwich'] </td>. " " . $row['price'] . "<br />"; //display the results } Not sure where I begin and end my TD because of this ." ". Thanks Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721083 Share on other sites More sharing options...
phpSensei Posted December 22, 2008 Share Posted December 22, 2008 Try <?php $username = "user"; $password = "password"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Fresh Sandwich Menu<br><br>"; //select a database to work with $selected = mysql_select_db("menus",$dbhandle) or die("Could not select menus"); //execute the SQL query and return records $result = mysql_query("SELECT `id`,`sandwich`, `price` FROM `sandwiche`s"); // Print the titles echo '<table width="28%" border="0" cellspacing="2" cellpadding="0"> <tr> <td width="50%" height="3" align="center"><strong>Sandwich</strong></td> <td width="50%" height="3" align="center"><strong>Price</strong></td> </tr> </table>'; echo '<table width="28%" border="0" cellspacing="2" cellpadding="0"> <tr>'; //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo '<td width="50%" height="3" align="center">'.$row['sandwich'].'</td> <td width="50%" height="3" align="center">'.$row['price'].'</td>'; } echo '</tr>'; echo '</table>'; //close the connection mysql_close($dbhandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721084 Share on other sites More sharing options...
Maq Posted December 22, 2008 Share Posted December 22, 2008 ^--- Why do you have 2 tables? Is one for the headers and one for the data? You should just use tags... Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721089 Share on other sites More sharing options...
phpSensei Posted December 22, 2008 Share Posted December 22, 2008 ^--- Why do you have 2 tables? Is one for the headers and one for the data? You should just use <th></th> tags... I was too lazy to just add those in dreamweaver, so I copy pasted the tables to create headers. Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721091 Share on other sites More sharing options...
phpSensei Posted December 22, 2008 Share Posted December 22, 2008 There <?php $username = "user"; $password = "password"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "Fresh Sandwich Menu<br><br>"; //select a database to work with $selected = mysql_select_db("menus",$dbhandle) or die("Could not select menus"); //execute the SQL query and return records $result = mysql_query("SELECT `id`,`sandwich`, `price` FROM `sandwiche`s"); echo '<table width="28%" border="0" cellspacing="2" cellpadding="0"> <tr> <td width="50%" height="3" align="center"><strong>Sandwich</strong></td> <td width="50%" height="3" align="center"><strong>Price</strong></td>'; //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo '<td width="50%" height="3" align="center">'.$row['sandwich'].'</td> <td width="50%" height="3" align="center">'.$row['price'].'</td>'; } echo '</tr>'; echo '</table>'; //close the connection mysql_close($dbhandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721093 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 Thanks PHPSensei for your help, but it displayed your headers, and not the mysql DB results. Hope this is not against rules, but here is the link to show you: http://freshfreshseafood.com/test/table.php Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721095 Share on other sites More sharing options...
phpSensei Posted December 22, 2008 Share Posted December 22, 2008 Do me a favor and add this code below the $result var echo mysql_num_rows($result) . '<br>'; if($result){ echo '1'; } else { echo '2'; } Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721099 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 Nope. check the results now...http://freshfreshseafood.com/test/table.php here is the code so far: //execute the SQL query and return records $result = mysql_query("SELECT id, sandwich, price FROM sandwiches"); echo mysql_num_rows($result) . '<br>'; if($result){ echo '1'; } else { echo '2'; } // Print the titles echo '<table width="28%" border="0" cellspacing="2" cellpadding="0"> <tr> <td width="50%" height="3" align="center"><strong>Sandwich</strong></td> <td width="50%" height="3" align="center"><strong>Price</strong></td> </tr> </table>'; echo '<table width="28%" border="0" cellspacing="2" cellpadding="0"> <tr>'; //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo '<td width="50%" height="3" align="center">'.$row['sandwich'].'</td> <td width="50%" height="3" align="center">'.$row['price'].'</td>'; } echo '</tr>'; echo '</table>'; //close the connection mysql_close($dbhandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721102 Share on other sites More sharing options...
phpSensei Posted December 22, 2008 Share Posted December 22, 2008 Okay I am guessing the script spits out results now. How does your data look in the db, it seems you put it all in one column. Lets raise the tables width to 100% change '<table width="28%" border="0" cellspacing="2" cellpadding="0"> to '<table width="100%" border="0" cellspacing="2" cellpadding="0"> Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721109 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 Ok. I'll try it. The data is in three columns: id |sandwich |price now I have price and per person in one. Not sure if that makes a difference. Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721113 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 First, I appreciate your help. Thank you again. Second almost if you look at the link now, shrimp sandwich, and the price is lined up. I took this out because it was displaying an 8 1 echo mysql_num_rows($result) . '<br>'; if($result){ echo '1'; } else { echo '2'; } Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721114 Share on other sites More sharing options...
sasa Posted December 22, 2008 Share Posted December 22, 2008 try <?php $result = mysql_query("SELECT id, sandwich, price FROM sandwiches"); //echo mysql_num_rows($result) . '<br>'; //Sif($result){ echo '1'; } else { echo '2'; } // Print the titles echo '<table width="28%" border="0" cellspacing="2" cellpadding="0"> <tr> <td width="50%" height="3" align="center"><strong>Sandwich</strong></td> <td width="50%" height="3" align="center"><strong>Price</strong></td> </tr>',"\n"; //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo '<tr><td width="50%" height="3" align="center">'.$row['sandwich'].'</td> <td width="50%" height="3" align="center">'.$row['price'].'</td></tr>',"\n"; } echo '</table>'; //close the connection mysql_close($dbhandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721128 Share on other sites More sharing options...
about2flip Posted December 22, 2008 Author Share Posted December 22, 2008 After adjusting the numbers on the width, that was it. THANK YOU SASA!!!! I was wondering do you know how to put lead lines in? These.....................................................Price Quote Link to comment https://forums.phpfreaks.com/topic/137945-need-help-please-readmysql-results-table/#findComment-721270 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.