0

I'm working on one site where I am trying to connect to an SQL database and post a SQL field inside a <div>. The <div> is inside a while loop. I think I have connected successfully to the database. But, I'm unable to show id field in the header. I am newbie to PHP & SQL and can't figure it out. Here is my code below:

<?php

    $db_host = "localhost";
    $db_username = "user192";
    $db_pass = "xxxx";
    $db_name = "sound-library";

    @mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to sql");
    @mysql_select_db ("$db_name") or die ("cant find database");

    $query = "select * from sound-library";
    $queryResult=mysql_query($query);
    $numrows=mysql_num_rows($queryResult);

?>

    while($row = mysql_fetch_assoc($queryResult)) {
        <div class="audio-module-parent">
            <div class="audio-module-header">
<?php
                <h1> <?php echo $row['id']?> </h1>
            </div>
            <div class="audio-module-preview"></div>
            <div class="audio-module-download">Download</div>
            <div class="audio-module-tutorial">Watch Tutorial</div>
        </div>
?>
Ramiz Wachtler
  • 5,623
  • 2
  • 28
  • 33
red house 87
  • 1,837
  • 9
  • 50
  • 99

2 Answers2

1

You are placing code outside the <?php ?> e.g here

?>
while($row = mysql_fetch_assoc($queryResult)) {

and then putting HTML inside <?php ?> e.g here

<?php
<h1> <?php echo $row['id']?> </h1>
</div>

It should be

<?php
error_reporting(E_ALL); //Enable Error Reporting
ini_set('display_errors',1); //change value to 0 to disable the error views
$db_host = "localhost";
$db_username = "user192";
$db_pass = "xxxx";
$db_name = "sound-library";
@mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to sql");
@mysql_select_db ("$db_name") or die ("cant find database");
$query = "select * from sound-library";
$queryResult=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($queryResult);
while($row = mysql_fetch_assoc($queryResult)) {
?>
<div class="audio-module-parent">
    <div class="audio-module-header">
        <h1> <?php echo $row['id']?> </h1>
    </div>
    <div class="audio-module-preview"></div>
    <div class="audio-module-download">Download</div>
    <div class="audio-module-tutorial">Watch Tutorial</div>
</div>
<?php } ?>
Shehary
  • 9,926
  • 10
  • 42
  • 71
  • I have tried your suggestion but it is still not echoing what I want :( – red house 87 Sep 23 '15 at 17:56
  • make this change `$queryResult=mysql_query($query) or die(mysql_error());` – Shehary Sep 23 '15 at 18:01
  • check updated answer, enable error reporting in page – Shehary Sep 23 '15 at 18:04
  • Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/ligerb/public_html/projectb/audio-module.php on line 5 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/ligerb/public_html/projectb/audio-module.php on line 13 – red house 87 Sep 23 '15 at 18:09
  • your query is failing – Shehary Sep 23 '15 at 18:14
  • 1
    Check this answer, it has all the possible solutions of the error you are facing http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole – Shehary Sep 23 '15 at 18:20
  • 1
    thanks for the article turns out it wasn't liking the hyphens in my database name so I renamed it with underscores and it has populated my page as expected :) – red house 87 Sep 23 '15 at 19:51
1
- <h1> <?echo $row['id'];?> </h1> missing semi colon  (;) here in this line.
- Missing } end of while loop

Use this connection. Write your password in 'xxxx' part & check it. comment your mysql_connect & mysql_select_db

$con=mysql_connect("localhost","user192","xxxx") or die ("couldnt connect to sql"); $db=mysql_select_db ("sound-library",$con) or die ("cant find database");

 <? 
    while($row = mysql_fetch_assoc($queryResult))
    {?>
    <div class="audio-module-parent">
        <div class="audio-module-header">
            <h1> <?echo $row['id'];?> </h1>
        </div>
        <div class="audio-module-preview">

        </div>
        <div class="audio-module-download">Download</div>
        <div class="audio-module-tutorial">Watch Tutorial</div>
    </div>
    <?}?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • Still not getting the result I want. Maybe the issue is the while loop I have written. What I am trying to achieve is for the html to be created for ever instance of "id" there is, not sure if I have used the right logic or syntax.. – red house 87 Sep 23 '15 at 17:58
  • are you sure? your $db_username is user192. Check it with $db_username='root' & $db_password as "" (means blank, no password) @BenLiger – Nana Partykar Sep 23 '15 at 18:08
  • I am but is there a way to check where the query might be failing? – red house 87 Sep 23 '15 at 18:19
  • See my edited answer. Check once using this. @BenLiger. And, let me know what's happening – Nana Partykar Sep 23 '15 at 18:47
  • I'd like to try your answer buy I have another issue now after testing somthing else. I renamed by database to something else using php my admin. I tried renaming it back to what it was but now it can't connect to the database!? Does renaming the database in PHP my admin change something with the connection? – red house 87 Sep 23 '15 at 18:57
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90467/discussion-between-nana-partykar-and-ben-liger). – Nana Partykar Sep 23 '15 at 19:02