Jump to content

[SOLVED] PHP and MYSQL Output


aztec

Recommended Posts

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.