-2

My original question was not clear enough. SO I'll attempt to give more information. I have a page that pulls data from a database using PHP (default.php). I built this page so that, based on the 'ID' - the content will change on the page. This is because I will have 100+ entries. I don't want to create 100 individual pages since the only the content will be changing.

Example:

https://mywebsite.com/default.php?id=xxxxx1
https://mywebsite.com/default.php?id=xxxxx2
https://mywebsite.com/default.php?id=xxxxx3

etc...

My table have columns for each row including: 'id' 'name' 'image' 'nominations' etc...

The 'default.php' page is a template that has the following code:

In the header:

<?php
include_once '../dbh.php';
 $id = $_GET['id'];
?>

And in the body for each of the columns above:

<?php
$sql = "SELECT nominations FROM channel_info";
$result = mysqli_query($conn, "SELECT nominations FROM channel_info WHERE id='$id'");
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0){
   while ($row = mysqli_fetch_assoc($result)) {
      echo $row['nominations'];            
   }
} else {
   echo "<p>Sorry. No nominations listed. </p>";
}
?>

etc..

Currently the messages does NOT show when columns in the same row of the table is empty. How can I make this message show so that if it's empty it will display a message? This happens to all the data entries.

Empty column: [screenshot] prnt.sc/tkcw80 Page displa: [screenshot] http://prntscr.com/tk54iv

  • I don't understand your question. Could you give us a better example? Can you tell what data you are dealing with? – Dharman Jul 18 '20 at 11:41
  • @Dharman Sorry. I work PHP before, but this is my first time using it to pull from a database. I'm trying to have an if/else statement where 'if' data exist show data. 'else' show a message: "nothing found". The data is just text data. If I understand your question. So using screenshots from my example: [data exist and displays data]: http://prntscr.com/tk5f8r [data does not exist and does not display message]: http://prntscr.com/tk54iv Let me know if this helps. – user3773297 Jul 18 '20 at 11:50
  • First, if you are only starting please learn PDO instead of mysqli. Your code should display a message, so you need to explain to us what is wrong. Do you see "Sorry. No nominations listed." at all? – Dharman Jul 18 '20 at 11:52
  • @Dharman My fault. My comment sent before hitting finishing my comment. So using screenshots from my example: [data exist and displays data]: prntscr.com/tk5f8r [data does not exist and does not display message]: prntscr.com/tk54iv Let me know if this helps. – user3773297 Jul 18 '20 at 11:55
  • Do you ever see the message "Sorry. No nominations listed." or not? – Dharman Jul 18 '20 at 11:58
  • @Dharman No I do not see it. – user3773297 Jul 18 '20 at 12:12
  • Then it means you always have at least one record. `mysqli_num_rows` tells you the number of records fetched from the database. If it is greater than 0 it means you fetched at least 1 record – Dharman Jul 18 '20 at 12:13
  • What you posted *should* work. What's the value of `$id` and where is it coming from? Are you checking for errors at all on the PHP and the query? I don't see any of that. – Funk Forty Niner Jul 18 '20 at 13:53
  • @FunkFortyNiner $Id is referring to $_GET. This code is at the top of my page. /// When I view console I don't see any error. – user3773297 Jul 18 '20 at 19:24
  • @Dharman I restructured the question and hopefully it's more understandable. – user3773297 Jul 18 '20 at 19:50
  • What do you mean columns are empty? Can you show full code which prepared that table? – Dharman Jul 18 '20 at 19:52
  • @Dharman Database row [screenshot] https://prnt.sc/tkcw80 Regarding the table, do you mean the PHP code? That's all I have those two blocks. One in the header and the other in the body. – user3773297 Jul 18 '20 at 20:16
  • `if(!$row['nominations']) echo "No nominations!";` – Dharman Jul 18 '20 at 20:17
  • Is this what you want? https://3v4l.org/Lg85P – Dharman Jul 18 '20 at 20:20
  • @Dharman Oh my god! Yes. That's what I waned. It works. Thank you – user3773297 Jul 18 '20 at 20:24

1 Answers1

-2

I'll try to help you but first, could you please tell me what $id is referring to ? $_GET ? $_POST ? ... it could be a security issue.

So, if you would like to connect to your database I would suggest you to use PHP PDO like so :

define('user', "HERE_YOUR_USER");
define('password', "HERE_YOUR_PASSWORD");
define('DB_name', "HERE_YOUR_DATABASE_NAME");

// REPLACE "localhost:8889" WITH YOUR SERVER:PORT
define('DB_server', "localhost:8889");

try {
    $connect = new PDO("mysql:host=".DB_server.";dbname=".DB_name, user, password);
    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    var_dump($e->getMessage());
}

Then, you would like to make a request :

$results = $connect->prepare("SELECT nominations FROM channel_info WHERE id='$id'");
if ($results->execute()) {
    while ($r = $results->fetch(PDO::FETCH_ASSOC)) {
        echo $r['nominations'];
    }
} else {
    echo "<p>Sorry. No nominations listed. </p>";
}

Let me know if you have any question about it :)

Bernard
  • 155
  • 1
  • 10
  • $Id is referring to $_GET. This code is at the top of my page. /// – user3773297 Jul 18 '20 at 12:27
  • Imagine if someone write on the url : http://YourWebsite.com/index.php?id=A_VERY_MEAN_SQL_REQUEST_THAT_WE_BE_ADDED_FOLLOWING_YOUR_ORIGINAL_REQUEST. It could be a real problem for you, isn't ? this is what we call "sql injection" that's dangerous. Read this : https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Bernard Jul 18 '20 at 12:36
  • Thanks for the info. I never thought this would be an issue as my site is particulary small – user3773297 Jul 18 '20 at 12:48
  • Awesome. I've just modified a little bit the code that I've posted, I made it more readable. Let me know it doesn't work. – Bernard Jul 18 '20 at 13:15