-2

I have reviewed, and edited, all the code previously discussed and it seems to be working fine now. Thanks very much for your help. Below is the last snippet that seems to be hanging the code:

print('N. of records: '.$result->num_rows.'<br />');
print('<table>');
while ($row = $result->fetch_object()){
    print('<tr><td>'.$row['last'].'</td><td>'.$row['first'].'</td></tr>');
}
print('</table>');

I can not find an error in this snippet. As well, I can verify that the connection to the database is working because a test in the code shows that: "N. of records: 5" were selected. Those 5 records all have data in the 'first' and 'last' fields. So, I think there must be an error in the code above which would not cause an error, but would also still cause the table to not be created. To be clear, the table is not just empty, it is not there at all.

Since the table is not being created, my guess was that the error must be in the print('<table>'); line, but for the life of me, I can't see an error in it. Can you?

Reibwo
  • 1
  • 4
  • 2
    1- when looking for problems, remove error supression (`@`); 2- `mysqli_connect_connect` is not a thing; 3- you need to put the connection in a variable ($con = mysqli_connect..); 4- `mysqli_query` takes the connection as first paramenter; 5- ??? – FirstOne Sep 18 '17 at 03:12
  • Don't use quotes in column names. [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – FirstOne Sep 18 '17 at 03:16
  • I suggest you to google those `mysqli_` functions and check proper usage in the manual. There are examples in each page. – FirstOne Sep 18 '17 at 03:16
  • Thanks for the suggestions! Implemented them as best I knew how. Here is the updated code. Still, the same results, no error messages. Seems the updated code is too long to post here. – Reibwo Sep 18 '17 at 03:26
  • Use the [edit](https://stackoverflow.com/posts/46271070/edit) button to update the question. – FirstOne Sep 18 '17 at 03:28
  • Many thanks again. Will go back to the manual. Seems my attempt was a little too green to pick and examine. – Reibwo Sep 18 '17 at 03:35
  • A quick look at the example from [mysqli_fetch_assoc](http://php.net/manual/en/mysqli-result.fetch-assoc.php) might shed you some light (check example 2 - Procedural style). – FirstOne Sep 18 '17 at 03:41

1 Answers1

0

It seems there are many errors in your code and also you didn't use the proper MySQLi functions. You should have some more research on the MySQLi with PHP and then it will be quite easy for you.

I refined your code have a look and try this:

<html>
<body>

<?php

$dbhost = 'localhost:3306';
$username = "root";
$password = "";
$database = "newworld";

$conn =mysqli_connect($dbhost, $username, $password, $database);

if(! $conn ) {
    die('Could not connect: ' . mysqli_error());
} else {
    #db connected
}

/**/
$sql = 'SELECT first, last FROM photos';
$result = mysqli_query($conn, $sql);

/**/ 

?>

    <table border="1" cellspacing="2" cellpadding="2">
        <tr>
            <td><font face="Arial, Helvetica, sans-serif"><u>first</u></font></td>
            <td><font face="Arial, Helvetica, sans-serif"><u>last</u></font></td>
        </tr>

        <?php

        if (mysqli_num_rows($result) > 0) {

            while($row = mysqli_fetch_assoc($result)) {

               ?>

               <tr>
                <td><font face="Arial, Helvetica, sans-serif"><?php echo $row["first"]; ?></font></td>
                <td><font face="Arial, Helvetica, sans-serif"><?php echo $row["first"]; ?></td>
               </tr>

               <?php
            }

        } else {
            echo "0 results";
        }
        ?>
    </table>
</body>
</html>

Note*: Update your database details like DB name, username, and password

Harish
  • 462
  • 6
  • 13
  • Many thanks for this refinement. I will have a go with it to see if it works. I apologize if I asked a question before I should have, but I could not seem to make any progress. Many thanks again. I really do appreciate all the help. – Reibwo Sep 18 '17 at 03:59
  • @Reibwo Your welcome and no worries man, "Every expert was once a beginner." (copied) ;) – Harish Sep 18 '17 at 04:10
  • I gave your code a go and came up with the empty table with this printed just above it: "0) { while($row = mysqli_fetch_assoc($result)) { ?>" Does that give you further insight? At least it is not just a blank table. I will look at the manual for mysqli_fetch_assoc as FirstOne suggested to see if I can take it further. Again, thanks. – Reibwo Sep 18 '17 at 04:29
  • @Reibwo If it is showing you the output as `0 results` it means there no record in your database so is there any records in your database table. But the above script should work at your end. If you will see any error then comment the error here so we can track the issue – Harish Sep 18 '17 at 05:32
  • Thanks again. That is what I thought the output meant, but there are definitely records in the database. I'll figure it out... – Reibwo Sep 18 '17 at 10:53
  • So you don't have the database access? There is no other issue if you can't access the database. – Harish Sep 19 '17 at 00:25
  • Hi Harish, no. I DO have access. The result just shows what I put there, as if the query is not well formed and therefore not getting the target data. Will look at this more later today. – Reibwo Sep 19 '17 at 03:05
  • Ok and let me know when you resolve this issue. And if this script helped to a certain extent then please you can mark it as accepted answer ;) – Harish Sep 19 '17 at 10:45
  • @Reibwo you unaccepted the answer any reason? we can resolve the issue. – Harish Sep 29 '17 at 03:52