0

When trying to get data from database the stmt fails. Using similar type code in same file to GET data out of an other database and it works fine. Does it have anything to do with the fact that the 'thread-cat' is a foreign key in the database?

<?php
        $catname = $_GET['cat'];
        $sql = "SELECT * FROM `threads` WHERE thread-cat=?;";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)){
            echo "SQL stmt failed";
        } else {
            mysqli_stmt_bind_param($stmt, "s", $catname);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            
            while($row = mysqli_fetch_assoc($result)){
                $id = $row['thread-id'];
                $title = $row['thread-title'];
                $content = $row['thread-content'];
                $timestamp = $row['timestamp'];
 
    
            echo '<tr>
                <td class="tg-0pky"><img src="https://via.placeholder.com/32x32"></td>
                <td class="tg-0pky"><b><a href="">'.$title.'</a></b><br>by <a href="">(User)</a> - (Date created)</td>
                <td class="tg-0pky">(Reply count)</td>
                <td class="tg-0pky">by <a href="">(User)</a><br>(Date Last Post)</td>
              </tr>';
            }
        }
    ?> 
Babo
  • 3
  • 5
  • `if (!mysqli_stmt_prepare($stmt, $sql)){` is a very wrong way of doing things. you need a particular error message from a database, while telling a site user that "SQL stmt failed" makes absolutely no sense – Your Common Sense Mar 12 '21 at 08:47
  • at least one error is apparent though for some reason you want to get a subtraction result `thread - cat` which means the value from `cat` field should be subtracted from `thread` field – Your Common Sense Mar 12 '21 at 08:49
  • I changed all the dashes to underscores and it works fine now. Thanks for mentioning this stupid fault! – Babo Mar 12 '21 at 09:28
  • you don't actually need to change. remember that tiny ` symbol you used around the threads table name? Actually, adding them around threads doesn't change anything but adding it to the field name with special characters will fix the error – Your Common Sense Mar 12 '21 at 09:31
  • No that did no fix the error because I already did that after you did say that cat gets subtracted from thread field when using `-`. – Babo Mar 12 '21 at 09:33
  • It **does** fix this particular error. Probably you had another one in your code – Your Common Sense Mar 12 '21 at 09:34
  • Yes thats why I changed all of them just to be sure everything was the same – Babo Mar 12 '21 at 09:36

0 Answers0