0

I want to select a value from the database with variables I get from $_GET, but it doesn't show any results. Could any one help me find what is wrong with my code?

here is my first page how i get the value to GET:

<?php
            if($result->num_rows > 0) {
               while($row = $result->fetch_assoc()) {
        ?>
                 <tr>
                    <td><a href="file.php?subject=<?= $row['subject'] ?>"><?= $row['subject'] ?></a></td>
                    <td><?= $row['location'] ?></td>
                    <td><?= $row['geo'] ?></td>
                    <td><?= $row['date'] ?></td>
                    <td><?= $row['piority'] ?></td>
                 </tr>
        <?php
               }
            }
        ?>

and here is my second page that i want to get data from database with subject variable from first page:

<?php
$varPage = $_GET['subject'];
                $servername = "localhost";
                $username = "bayansh_user";
                $password = "u)nHf,Accmo)";
                $dbname = "bayansh_bmc";
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                     die("Connection failed: " . $conn->connect_error);
                } 
                $result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '.$varPage.'");

while($row = mysqli_fetch_array($result))

?>

and now i want to write the date here is my code:

<p style="font-family:B Zar; direction:rtl; font-size:165%;"> <?= $row['date'] ?> </p>

but it writes nothing. Is there anything wrong with my code?

Farhad paikan
  • 89
  • 2
  • 10

2 Answers2

1

There seems to be an oversight on your part with regards to your SQL. You have an extra-dot, which likely was a Typo. Try addressing that part and your code would most likely work as you expected. Some Guys here have suggested working with Prepared Statement (which is quite necessary) in which case this Snippet would use PDO as opposed to MySQLi... although the principles are not so extremely different.

    <?php

            $varPage     = $_GET['subject'];
            $servername  = "localhost";
            $username    = "bayansh_user";
            $password    = "u)nHf,Accmo)";
            $dbname      = "bayansh_bmc";
            $dbh         = null;

            // USING PDO INSTEAD::
            $dsn         = 'mysql:host=' . $servername . ';dbname=' . $dbname;  
            try {
                $dbh     = new PDO($dsn, $username, $password);
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                throw new Exception($e->getMessage());
            }

            // USING PREPARED STATEMENT... NOTICE "=:SBJ"
            $sql         = "SELECT `date` FROM `editor` WHERE subject=:SBJ";
            // USING PREPARED STATEMENT CONTINUED: NOTICE "$dbh->prepare()"
            $stmt        = $dbh->prepare($sql);
            // PASSING VALUES: NOTICE "['SBJ'=>$varPage]"
            $stmt->execute( array('SBJ'=>$varPage) );
            $resultSet   = $stmt->fetchAll(PDO::FETCH_OBJ);

            // LOOP THROUGH THE RESULT-SET AND DO SOME THINGS WITH THE DATA...
            foreach($resultSet as $intKey=>$objData){
                var_dump($objData->date);  //<== DUMP SOME DATA TO THE STREAM
            }
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • it gives an error: Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[42000] [1044] Access denied for user 'bayansh_user'@'localhost' to database 'dbname'' in /home2/bh/public_html/IS/file.php:15 Stack trace: #0 {main} thrown in /home2/bh/public_html/IS/file.php on line 15 – Farhad paikan Dec 04 '16 at 16:19
  • it gives an error: Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[42000] [1044] Access denied for user 'bayansh_user'@'localhost' to database 'dbname'' in /home2/bh/public_html/IS/file.php:15 Stack trace: #0 {main} thrown in /home2/bh/public_html/IS/file.php on line 15 – Farhad paikan Dec 04 '16 at 16:20
  • @Farhadpaikan Sorry.... The Dollar Symbol was lacking in the `$dbname` Variable.... Before, it read: `dbname` rather than `$dbname` .... You might re-check it one more time (*if you will*).... however, ***also make sure that the Username, Password and DB-Name are all Correct on your end...*** – Poiz Dec 04 '16 at 16:34
  • it works but it give null result, i mean it didnt write anything and one think i need to mention that subject column has arabic character value, does it has any effect???? – Farhad paikan Dec 04 '16 at 16:42
  • @Farhadpaikan You are not writing... *You are rather reading.* **SELECT** is a Read Operation.... Anyways, so long as you have the right Character Set, Language shouldn't be a Problem ;-) – Poiz Dec 04 '16 at 16:57
  • Sorry i thought it is like ADO.net , in SQL command if you want to read a arabic character from a textbox you need to add N at the start of Value like N'"+ textboxt.Text +"' without N it doesnt read the Arabic Character – Farhad paikan Dec 04 '16 at 17:06
  • Any way why i cant get the value from date where my subject is $varPage , it return a NULL value, i mean it doesnt write anything : / – Farhad paikan Dec 04 '16 at 17:07
0

You should use the following code

$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '".$varPage."'");