-3

I have a select option form in php/html. In the selection option forms I put php like this:

<option name="time" value="0900"><?php $a=$r1; echo $a;?></option>

Above it I need some php code to do the following:

Search my database for the value 0900; if it is found, give the variable $r1 a NULL value so that the option does not appear, otherwise give it the value of 0900.

Basically, what I need is for someone to tell me how I can search for a value in my database and give a variable a value accordingly. Thanks in advance.

edit: This is a bit of what I have

$conn= new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT time FROM Guidance";

$result = $conn->query($sql);

The only thing under time in my database is 'volvo'; but when I echo $result, I get nothing. Why?

1 Answers1

0

I assume you have table t1, and t1 has column value1, and you want to search 0900 on value1.

To search in t1.value1 it's good to use SELECT COUNT(*).

To make option disappear when 0900 is found, just use if.

// php code, use PDO for mysql
// this PDO arguments depend on your mysql environment, username, password
$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'username', 'password');
$sql = 'select COUNT(*) name from t1 where value1 = 0900';
$stmt = $dbh->query($sql);
$count = $stmt -> fetchColumn();
// $count is a value how many 0900 has found.

if ($count > 0) {
  $r1 = null;
else {
  $r1 = '0900';
}

// output html
<?php if ($r1 != null) { ?>
  <option name="time" value="0900"><?php $a=$r1; echo $a;?></option>
<?php } ?>
noymer
  • 240
  • 1
  • 11
  • Is there anything that goes above the $sql='select...' line? If so, what do I put? – Ahmed Imran Jan 07 '18 at 03:11
  • and how do I use a pdo for mysql? my database is on phpmyadmin and my code is on 000webhost. – Ahmed Imran Jan 07 '18 at 03:19
  • It's not matter whether pdo or mysqli. You can use `SELECT COUNT(*)` in mysqli. https://stackoverflow.com/questions/3613074/mysqli-count-always-returns-1 In addition, you will get pdo tutorial like below. http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – noymer Jan 07 '18 at 03:42
  • and is there any code that goes above the first line you gave me? – Ahmed Imran Jan 07 '18 at 03:57
  • I have editted the answer and added one line code above the first line for PDO initialization. You can get more detail from some tutorials or php document or etc... – noymer Jan 07 '18 at 04:52
  • I get this error: Uncaught Error: Call to a member function fetchColumn() on boolean in /storage/ssd1/716/4198716/public_html/test.php:12 Stack trace: #0 {main} thrown in /storage/ssd1/716/4198716/public_html/test.php on line 12 – Ahmed Imran Jan 07 '18 at 22:27
  • `Call to a member function fetchColumn() on boolean in ` means $stmt = false, because query method failed http://php.net/manual/en/pdo.query.php You should `Include just enough code to allow others to reproduce the problem. ` *([How do I ask a good question?](https://stackoverflow.com/help/how-to-ask))* It's better you show the test.php code. – noymer Jan 07 '18 at 22:59