aztec Posted January 6, 2008 Share Posted January 6, 2008 Hello Through your forums I have been leaning how to use PHP CSS and MYSQL. At this time I can use PHP to make contact with my Database and output using PHP. <?php // Request the text of all the information $result = @mysql_query('SELECT id,name,sname FROM gen20'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } $sp=" "; // Display the text of each item in a paragraph while ($row = mysql_fetch_array($result)) { echo $row['id'] . $sp . $row['name'] . $sp . $row['sname'] . '<br/>'; } ?> This will place the date across the page in rows. What I would like to do is place the data into the div id "middle". ?> <p> </p> <p> </p> <div id="container"> <div id="left"></div> <div id="middle"></div> <div align="center"></div> <br/> </div> <blockquote> <?php I realise this must be something simple to those people who know how to do it, but I have spent many hours trying to figure it out and must have read most of the recent posts, but to no avail. Please help if you can or point me to a tutorial that addresses this topic. Kind Regards Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/ Share on other sites More sharing options...
eRott Posted January 6, 2008 Share Posted January 6, 2008 Try saving the output to a variable, or output the HTML code using an echo. <?php // Request the text of all the information $result = @mysql_query('SELECT id, name, sname FROM gen20'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } $sp=" "; // Display the text of each item in a paragraph while ($row = mysql_fetch_array($result)) { $show = $row['id'] . $sp . $row['name'] . $sp . $row['sname'] . '<br/>'; } ?> <div id="container"> <div id="left"></div> <div id="middle"><?php echo $show; ?></div> <div align="center"></div> <br/> </div> -- OR -- <?php // Request the text of all the information $result = @mysql_query('SELECT id, name, sname FROM gen20'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } $sp=" "; echo "<div id=\"container\">"; //open container div echo "<div id=\"left\">"; //open left div echo "</div>"; //close left div echo "<div id=\"middle\">"; //open middle div // Display the text of each item in a paragraph while ($row = mysql_fetch_array($result)) { echo $row['id'] . $sp . $row['name'] . $sp . $row['sname'] . '<br/>'; } echo "</div>"; //close middle div echo "<div align=\"center\">"; //open center div echo "</div>"; //close center div echo "<div> </div>"; //i normally use this to act as a line break instead of <br> echo "</div>"; //close container div ?> Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-431803 Share on other sites More sharing options...
redbullmarky Posted January 6, 2008 Share Posted January 6, 2008 also - slightly off topic - as you're still learning (and i'd recommend this anyway, to anybody) avoid the 'shut up' operator (@) before your mysql_query function call. if the query fails, you're going to want to know what exactly went wrong, but the @ will hide any errors from you making debugging hard. just: $result = mysql_query('SELECT id, name, sname FROM gen20'); is fine. else, at least check the result to make sure it's working as expected. Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-431808 Share on other sites More sharing options...
aztec Posted January 6, 2008 Author Share Posted January 6, 2008 Hello Thanks eRott and for the advice from redbullmarky. The first solution did not work at all, it just output a page with the borders produced by a stylesheet. The second option output the data but inside a second page with borders. It appears that the div is being called twice, is it possible to advise me if this is the case Kind Regards Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-431833 Share on other sites More sharing options...
eRott Posted January 6, 2008 Share Posted January 6, 2008 It is slightly hard for me to understand what exactly the problem is without visualizing it. First, off, start with a simple database like this one: SQL QUERY: CREATE TABLE 'users' ( 'id' int(6) NOT NULL auto_increment, 'name' varchar(30) NOT NULL, 'sname' varchar(30) NOT NULL, PRIMARY KEY ('id') ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; INSERT INTO 'users' VALUES(1, 'Jack Black', 'Jack07'); INSERT INTO 'users' VALUES(2, 'Sussy Smith', 'Sussy17'); INSERT INTO 'users' VALUES(3, 'John Doe', 'JohnnyAP'); Next, we need to connect to the database. USERS.PHP $conn = mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); mysql_select_db('DATABASE', $conn) or die(mysql_error()); Next, we will want to grab the information from the database. USERS.PHP $query = "SELECT * FROM users"; $result = mysql_query($query) or die(mysql_error()); $grab = mysql_fetch_array($result) or die(mysql_error()); Now to show the results in the page: USERS.PHP $show = "Welcome " . $grab['name'] . "! Your current user ID is " . $grab['id'] . " and your USERNAME is " . $grab['sname']; I have created a simple page with [EXAMPLES] for you, complete with the [sOURCE CODE]. Have a poke around. Maybe you will figure out what your looking for. Once again, I am unsure as to what exactly is the problem you are receiving. Perhaps showing us the page you are working on and it's code would help. As for now, I hope the examples I created give you what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-432174 Share on other sites More sharing options...
redarrow Posted January 6, 2008 Share Posted January 6, 2008 errot your example link not working mate........ afther all that work/........... Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-432178 Share on other sites More sharing options...
eRott Posted January 6, 2008 Share Posted January 6, 2008 Yea, my host is currently experiencing some SQL server issues. JUST happened. Should be working soon enough UPDATE: SQL server problems fixed. That example is now working again. Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-432188 Share on other sites More sharing options...
aztec Posted January 7, 2008 Author Share Posted January 7, 2008 eRott Thanks for all the work you put into the reply. When I looked at your code I learn't that it is necessary when using style sheets to declare a class. ie .output <style> .output { text-align: center; border: 2px; border-style: dashed; } </style> and then to call it to get the data into the div. <div class="output"> ?> <b>EXAMPLE #1:</b><br /> <div class="output"> <?php I know there must be other ways to achieve the same results but for now I can use your examples to get my information placed correctly. Once again thanks. Kind Regards Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-432777 Share on other sites More sharing options...
eRott Posted January 7, 2008 Share Posted January 7, 2008 Hey aztec, You are welcome. However, I would just like to clarify one thing if I may. It is not necessary to declare a class. In my example, I just created a class in CSS and applied it to the DIV, to style it as I saw fit. CSS (or Cascading Style Sheets), are used to style your website and the various elements found within your website. The class .output, has no impact upon your PHP code. I believe I now understand why your code was not working. In your code, you had declared classes, (styles), for your DIV's, however, since you did not define those classes, your DIV's were not working. Therefore, anything inside them, would not show up. Take care. Quote Link to comment https://forums.phpfreaks.com/topic/84731-solved-php-and-mysql-output/#findComment-432805 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.