0

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;  
7
  • you select newsletterId and fetch name ? Commented Jan 29, 2015 at 22:34
  • @Mario, I did this but didn't work.. $nid = $mysqli->query("select newsletterId from newsletter where ndate='30-01-2015'")->fetch_object()->newsletterId; Commented Jan 29, 2015 at 22:41
  • 1
    Your code is open to SQL injection attacks. Please read this: stackoverflow.com/questions/60174/… Commented Jan 29, 2015 at 22:50
  • @Nephil: if ndate is a DATE column (and it should be, since you're using it to store dates) you need to use the yyyy-mm-dd date format, like 2015-01-30. You won't get results when you use other formats. Commented Jan 29, 2015 at 22:53
  • @Arjan what if I want to use it as string type? Commented Jan 29, 2015 at 22:55

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Pretty sure mysqli supports prepared statements; he doesn't need to switch libs, just needs to use it appropriately.
I check the connection .. it seems okay but not working
@Nephil, what's your DB schema (structure)?
@xxdavid , here it is: newsletterId smallint(6) ndate date edition tinytext pdfURL text webURL mediumtext hasRadio tinyint(1) radioLink text welcomeNote mediumtext poem mediumtext
Thanks a lot xxdavid, however i managed to get it using full while loop algorithm and I have posted the solution too...
0
$datelink=$_GET['bdate'];
$nid= $mysqli->query("SELECT `newsletterId` FROM `newsletter` WHERE ndate='$datelink' LIMIT 1")->fetch_object()->name;

Comments

0

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";
    }

1 Comment

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.