Click here to Skip to main content
15,796,456 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Let's say I have an "append" table and a "weight" column in the database.

I want the "weight" value put into a PHP variable.

How can I handle that?

What I have tried:

<pre lang="PHP">
    $sql = "SELECT weight FROM append";

    if ($conn->query($sql) === TRUE) {
        echo "Successful";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;

    echo $sql;
Updated 26-Jan-23 4:03am
Andre Oosthuizen 26-Jan-23 11:18am    
Have a look at these 2 sites on how to use PHP's Data Objects - AND

I suggest you start with a tutorial such as this one PHP MySQL: Querying Data from Database[^]
Share this answer
$query = 'SELECT weight FROM append';
$statement = $db->prepare($query);
$row_data = $statement->fetchAll(); // gets rows from database
$total_rows = $statement->rowCount(); // counts how many rows found

if ($total_rows > 0){
  foreach ($row_data as $row) {
    echo '<br>'.$row['weight'];
    // or as follows below
    $result = $row['weight'];
    echo '<br>'.$result;

Assuming you are already connected to the database ($db) this will work 100%.
Share this answer
OriginalGriff 20-Jul-23 3:44am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 6 month old ones - particularly when they are clearly homework. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

I'd also strongly suggest that you don't post multiple solutions to the same question - again, this will be frowned upon by some, and the ban hammer wielded if it looks too much like point hunting - there is a comment facility for that, as well as editing your existing solution to improve it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900