1

I want to get stings form a mysql server to use as text on my webpage. That way I can edit the text without editing the html file.

Problem is that the code I have to get the string is quite long, and I don't want to paste it everywhere on the page.

I would also like a tip on how to get just one datafield from the server, and not the whole column like I do here.

So this is what I got. And what I think is to write a function I can call from all the places I want the webpage to get a string or field from the sqlserver. But I don't know how. Can anyone help me?

<?php
$con=mysqli_connect("localhost","user..", "passwd..","db");
// Check connection
if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
$sql = "SELECT topic FROM web_content";
$result = $con->query($sql);

if ($result->num_rows > 0) 
    {
    // output data of each row
    while($row = $result->fetch_assoc()) 
        {
        echo $row["topic"]. "<br>";
        }
    } else 
    {
         echo "error";
    }
$con->close();
?>

1 Answer 1

4

Problem is that the code I have to get the string is quite long, and i dont want to paste it everywhere on the page.

Put the code into a function, call that function wherever you need to. Then it is just a single line you have to insert.

PHP:

<?php
function connect() {
    $con=mysqli_connect("localhost","user..", "passwd..","db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } else {
        return $con;
    }
}

function renderContent($con) {
    $sql = "SELECT topic FROM web_content";
    $result = $con->query($sql);

    if ($con && ($result->num_rows > 0)) 
    {
        // output data of each row
        while($row = $result->fetch_assoc()) 
        {
            echo $row["topic"]. "<br>";
        }
    } else {
         echo "error";
    }
}

HTML:

<?php $con = connect(); ?>
[...]
<div>
    <?php renderContent($con); ?>
</div>
[...]

I would also like a tip on how to get just one datafield from the server, and not the whole coloumn like i do here.

Not the whole column would mean not all rows, but one or some selected ones. That means you are looking for sqls ''WHERE'' clause.

SELECT topic FROM web_content WHERE <where clause>;

Where <where clause> is some clause to narrow down the result set. For example you can narrow down to topics containing some string: ... WHERE topid LIKE '%word%'; or by the IDs are a date range of the entries in your table. You should take a look into the documentation of the query syntax for an explanation: http://dev.mysql.com/doc/refman/5.0/en/select.html


Obviously all of this is just a rough sketch of what you are looking for. Lots of things need improving. Using exceptions for error handling is one thing, just to give an example...

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

1 Comment

Well this obviously means that you dit not feed a correct database object into the function renderContent(). Several possible reasons: the database connection fails, has been closed inbetween or you simply did not really use the code above. As said: this is far from perfect. It is meant to give you an idea of how to approach this. You cannot simply take such code and expect everything to work smoothly. You have to understand and adapt such code for your usage.

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.