Kong Meng Xiong
Kong Meng Xiong

Reputation: 71

How to SELECT column value FROM table?

Here's my code:

<?php
//recently added
$result = mysql_query("SELECT background FROM " . $shadowless_background_table . " WHERE id = 1");
if ($result == 1){
?>
    <script>
        jQuery(document).ready(function(){
            jQuery(".eltdf-psc-slide").addClass("no-background");
        });
    </script>
<?php
}
//=============
?>

Basically what I'm trying to do is checking and see if the value stored in the $shadowless_background_table "DB" is == 1 and I only want that column (background). I have browse the web, but what I see are examples with while loops which I was wondering if I could do something like this instead.

Upvotes: 0

Views: 89

Answers (2)

DaAmidza
DaAmidza

Reputation: 336

There are couple of issues with your code.The first thing that i have noticed is that you are using mysql API instead of PDO.I don't blame you since the internet is full of old tutorials and you probably didn't have a chance to get some guidance.

MySql is getting old It doesn't support modern SQL database concepts such as prepared statements, stored procs, transactions etc... and it's method for escaping parameters with mysql_real_escape_string and concatenating into SQL strings is error prone and old fashioned.


Organize your project better. As i have seen from this example you probably have a poor project organization.You should consider reading about PSR Standards


And to go back to your question ,and to update it a bit.

Instead of doing

  mysql_query("SELECT background FROM " . $shadowless_background_table . " WHERE id = 1");

I would do it this way:

<?php
$host = "localhost";
$username = "user name of db";
$password = "password of db";
$dbname = "database name ";

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    //your data
    $id = 1; // id

    $stmt = $conn->prepare("SELECT background FROM database_name WHERE id=:id");
    $stmt->bindParam(':id', $id);
    $stmt->execute();

    $data = $stmt->fetchAll();

    foreach ($data as $row) {
        echo $row["row_name"];
    }


    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }

Go read more about PHP in general ,it will help you out a lot.The biggest problem is that there are so much wrong tutorials and references or they are just old.And people learn from wrong sources.

I had the same problem ,but thanks to right people on this site i have managed to learn more.

My suggestion is that you read about PSR,PDO and PHP in general!!!

Also a thing you should consider reading about is security in php.

Good luck mate :D

Upvotes: 1

Rudraksh Pathak
Rudraksh Pathak

Reputation: 898

If you want to fetch a single record based on a condition you can do this -

$result = mysql_query("SELECT background FROM " . $shadowless_background_table . " WHERE id = 1");
if (mysql_num_rows($result)>0){
    $fetchedColum = mysql_result($result, 0, 'COLUMN_NAME');
}

Upvotes: 1

Related Questions