1

I really need some help. I am trying to build a website and pull content from a mysql database. The div I am trying to populate is below. The problem I have is how do I incorporate the php code into the div?

<div class="cardetails">
    <img src="img/carpic.jpg"/>
    <p class="make"><a href="car.html">'model of car'<a></p>
    <p class="price">£Price</p>
    <div class="clear"></div>
</div>


$db = mysql_connect("localhost", "root", "Password1")
    or die ("Couldn't connect to Server!");
$result = mysql_select_db("my_cars", $db)
    or die ("Couldn't select Database!");
$query = "select img, make, price, from tbl_automatic WHERE MakeID = '7'";
$result = mysql_query($query,$db)
    or die ("Couldn't execute query!");

Thanking you all in advance

7
  • Let me get it stright, you want to post query information/details in a div? Commented Oct 5, 2013 at 18:54
  • Please don't use any mysql_ functions as they are deprecated. Use PDO instead. Commented Oct 5, 2013 at 18:56
  • Or mysqli: php.net/manual/en/mysqli.query.php Commented Oct 5, 2013 at 18:58
  • Hi Evceyone, thanks for getting back to me. Sorry for late reply, I had to take a break as it was "doing my head in". Commented Oct 6, 2013 at 9:28
  • Yotam, yes that is what I am trying to do. Commented Oct 6, 2013 at 9:29

2 Answers 2

2
<?php
$db = mysql_connect("localhost", "root", "Password1")
    or die ("Couldn't connect to Server!");
$result = mysql_select_db("my_cars", $db)
    or die ("Couldn't select Database!");
$query = "select img, make, price, from tbl_automatic WHERE MakeID = '7'";
$result = mysql_query($query,$db)
    or die ("Couldn't execute query!");

while($q = mysql_fetch_array($result)){
?>
<div class="cardetails">
    <img src="<?=$q['img']?>"/>
    <p class="make"><a href="car.html"><?=$q['make']?><a></p>
    <p class="price"><?=$q['price']?></p>
    <div class="clear"></div>
</div>

<?php
}
?>

I hope it solves your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi neeagl, Sorry for late reply. Many thanks for your solution, unfortunately nothing is being pulled into the page. If I run the query in phpMyadmin, it returns the correct information. Any further help is appreciated.
Can you please paste content of your page by doing view-source? Thanks
0

neeagl beat me to it but here's mine anyhow. As mentioned in the comments it's best to use mysqli or pdo now.

<?php
$db = mysql_connect("localhost", "root", "Password1")
    or die ("Couldn't connect to Server!");
$result = mysql_select_db("my_cars", $db)
    or die ("Couldn't select Database!");
$query = "select img, make, price, from tbl_automatic WHERE MakeID = '7'";
$result = mysql_query($query,$db)
    or die ("Couldn't execute query!");


$cardetails = '';

while($row = mysql_fetch_array($result)) {
$cardetails .= '
<div class="cardetails">
    <img src="img/'.$row['img'].'"/>
    <p class="make"><a href="car.html">'.$row['make'].'<a></p>
    <p class="price">£'.$row['price'].'Price</p>
    <div class="clear"></div>
</div>';
}

echo $cardetails;

1 Comment

Hi logic-unit, Many thanks for your input. You mention mysqli or pdo unfortunately, I am not sure how to do that as I am new to all of this. Any guidance is much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.