Hi all,

I am trying to populate text on my website from data stored in a MySQL database.

I have this script before the <head> of my HTML to establish the connection:

<?php
        $username = "USERNAME";
        $password = "PASSWORD";
        $hostname = "127.0.0.1:3306";

    //connection to the database
        $dbhandle = mysql_connect($hostname, $username, $password)
            or die("Unable to connect to MySQL");
        echo "";

    //select database
        $selected = mysql_select_db("Strong_Links",$dbhandle)
            or die("could not select database");

    ?>

I have loaded that script in isolation with an echo success message, and know it works correctly.

Then, in the body of my HTML I use this script:

<?php

        $result = mysql_query("SELECT content FROM web WHERE id = 'news1'", $dbhandle);
            if (!$result) {
                    die("query failed");
                    }
            echo "<p>$result</p>";



?>

However, on the page, I get one line saying "$result" then a line break, and then another line with ""; ?>"

Does anybody know what I am doing wrong? I have checked my query on the host machine in mySQL and it works fine.

Recommended Answers

All 3 Replies

Welcome to aboard.
First, you shouldn't use mysql driver but you should use mysqli if procedural (as this example is) or PDO (preferable) if OOP.
Secondly, you need to loop over returned results. Check example 2.

Also, check what have you got if print_r($result) without html tags.

The $result does not contain the rows from the database yet. It is just a special PHP type variable that enables fetching of rows. So you have to fetch rows, usually in a loop:

The following is an example using mysqli object oriented way:

$username = "USERNAME";
$password = "PASSWORD";
$hostname = "127.0.0.1:3306";
$database = "Strong_Links";

//connection to the database
$dbhandle = new mysqli($hostname, $username, $password, $database) or die "Unable to connect to MySQL"];
echo "";

// query the database
if(!$result = $dbhandle->query("SELECT content FROM web WHERE id = 'news1'")){
    die("query failed");
}

// loop over the result set
while($row = $result->fetch_assoc()){
    echo '<div>' . $row['content'] . '</div>';
}

You can use mysqli also in procedural way or maybe you would prefer PDO. Let us know if you need help with that.

Just using your codes this should make it work

<?php
        $username = "USERNAME";
        $password = "PASSWORD";
        $hostname = "127.0.0.1:3306";

    //connection to the database
        $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

    //select database
        $selected = mysql_select_db("Strong_Links",$dbhandle) or die("could not select database");

        $result = mysql_query("SELECT content FROM web WHERE id = 'news1'", $dbhandle) or die(mysql_error());

           while($get_data = mysql_fetch_assoc($result)){
                    echo $get_data;
           }

?>

@broj1 answered it in mysqli

This is also another way around this is PDO format

//database connection
$username = "USERNAME";
$password = "PASSWORD";
$database = "attendance";
$host     = "127.0.0.1:3306";
try {
    $conn = new PDO('mysql:host='.$host.';dbname=' .$database , $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    die();
}

  `//Query`
                    $sql = ""SELECT content FROM web WHERE id = 'news1'";
                    $query = $conn->query($sql) or die(mysql_error());
  `Looping through database`               
                 while ($row = $query->fetch()) {

                     echo  $row['content'];
                           }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.