简体   繁体   中英

How to get single value from MySQL query in PHP

I know this post is similar to many other Stack Overflow questions. However, they quite didn't help to sort out this issue. I want the query to return single value and store it in variable. The SQL query is working fine in the database but not with PHP. Where have I gone wrong here?

$datelink = $_GET['bdate'];
$nid = $mysqli->query("select `newsletterId` from `newsletter` where ndate='$datelink'")->fetch_object()->name;  
$datelink=$_GET['bdate'];
$nid= $mysqli->query("SELECT `newsletterId` FROM `newsletter` WHERE ndate='$datelink' LIMIT 1")->fetch_object()->name;

You should initiate your connection before quering the database.

$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}

It's also dangerous to just append your string from $_GET. Your site can be attacked with SQL injection. You definitely should use PDO with prepared statements. Or some another library like that, I like dibi for example.

Update: You are accessing column name that doesn't exist. It should throw a notice ( PHP Notice: Undefined property: stdClass::$name ). If you don't see notices and you are developing locally (and you definitely should), you can enable it in your php.ini file with error_reporting = E_ALL .

You probably want column newsletterId . So altogether:

$mysqli = new mysqli('example.com', 'user', 'password', 'database');
if ($mysqli->connect_errno) {
    echo 'Failed to connect to MySQL: ' . $mysqli->connect_error;
}
$dateRaw = $_GET['bdate']; // for example '22.12.2012', if this value is already in format YYYY-MM-DD, you can step next line and use it
$date = date('Y-m-d', strtotime($dateRaw));


$statement = $mysqli->prepare("SELECT `newsletterId` FROM `newsletter` WHERE `ndate`=? LIMIT 1");
$statement->bind_param('s', $date); // replace the question mark with properly escaped date
$statement->execute();
$result = $statement->get_result();

if ($result->num_rows) {
    $newsletterObject = $result->fetch_object();
    $newsletterId = $newsletterObject->newsletterId; // you had $newsletterObject->name here
} else {
    echo "Newsletter with the date doesn't exist";
}
$statement->close();

Initially I thought that there is easy way to fetch single value but couldn't find it. So have to use full code as below to get the value.

$sql3="select newsletterId from newsletter where ndate='$bdate' limit 1";
$result3 = $conn->query($sql3);
    if ($result3->num_rows > 0) {

    while($row3 = $result3->fetch_assoc()) {                    
    $nid=$row3['newsletterId']; //Here is where the single value is stored.

    }
    } else {
        echo "0 results";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM