Jump to content

Display help with php mysql


renfley

Recommended Posts

hey just curious if i could pull this off some how

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("database", $con);

 

$result = mysql_query("SELECT title FROM table1");

 

foreach($result)

{

?>

<div><?php echo title; ?> </div>

 

<?php

mysql_close($con);

?>

 

Basicly im trying to query the database and foreach result display it within divs? ive been trying to figure it out for a while and im stuck any help would be awsome

thanks

 

Link to comment
Share on other sites

http://php.net/manual/en/function.mysql-fetch-row.php

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$result = mysql_query("SELECT title FROM table1");

while($row = mysql_fetch_array($result)){
$title = $row['title'];

echo "<div>".$title."</div>";

}
mysql_close($con);
?> 

Link to comment
Share on other sites

iwas thinking more something like

 

 

$this is the qurey above

 

foreach($result)

 

{

?>

 

<div>

 

<p>this is the title of the row</p><?php echo title; ?>

</div>

 

this way it will deisplay every row and give me more control over the content?

Link to comment
Share on other sites

im trying to avoid this

 

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

echo <div></div>

 

lol

Link to comment
Share on other sites

@renfley,

 

You were trying to use a foreach loop on the reference ID for a database result set. You need to create a loop (typically a while() loop) to extract each record one at a time and then display/process the data however you want. QuickOldCar gave you a solution in Reply #2. I *think* your reply, in Reply #5, was to prevent having to have multiple echo statemetns - one for each record rather than having empty values displayed. Unless, of course, you have empty values in your database.

 

@QuickOldCa; Why define a variable $title just to use it one time?

$title = $row['title'];

echo "<div>".$title."</div>";

 

This would be easier

$title = $row['title'];

echo "<div>{$row['title']}</div>";

 

 

Anyway, here is what I would do (based off of QuickOldCar's script)

//Connect to Database
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);

//Create and run query
$query = "SELECT title FROM table1";
$result = mysql_query($query) or die("Query:<br>{$query}<br>Error:<br>" . mysql_error());

//Output results
while($row = mysql_fetch_assoc($result))
{
    echo "<div>{$row['title']}</div>\n";
}

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.