I am having problems with the fetch statement in my script. First of all, I have the following script that works perfectly:
<?php
include 'login.php';
$cnct = mysqli_connect($hostname, $usernam, $passwrd) or die('Unable to connect to MySQL server.');
$database = 'wardrick_all_words_bigger';
mysqli_select_db($cnct, $database) or die('Unable to select database.');
$query = "SELECT word FROM allwords";
$doit = mysqli_query($cnct, $query) or die("Unable to select from database");
$rowcount=mysqli_num_rows($doit); //number of records
echo "<p>rowcount is $rowcount</p>";
$count = 5;
for($i=0; $i<$count; $i++)
{
$fetchit = mysqli_fetch_assoc($doit) or die('Couldnt fetch');
$w = $fetchit["word"];
echo "<p>$w</p>";
}
echo "done";
mysqli_close($cnct);
?>
The page that I am trying to fix is the following (the entire script):
<?php
include 'login.php';
$cnct = mysqli_connect($hostname, $usernam, $passwrd) or die('Unable to connect to MySQL server.');
$database = 'wardrick_all_words_bigger';
mysqli_select_db($cnct, $database) or die('Unable to select database.');
$query = "SELECT word FROM allwords";
$doit = mysqli_query($cnct, $query) or die("Unable to select from database");
$rowcount=mysqli_num_rows($doit); //number of records
echo "<p>rowcount is $rowcount</p>";
for($i=0; $i<=$rowcount; $i++)
{ $fetchit = mysqli_fetch_assoc($doit) or die('Couldnt fetch');
$w = $fetchit["word"];
$stringlength = strlen($w);
if($stringlength == 5)
{
//insert into table five_letter_words
$query2 = "INSERT INTO five_letter_words (word) VALUES ('$w')"; ########## hangs up on this query #################
$doit = mysqli_query($cnct, $query2) or die("Unable to insert into db");
}
}
echo "done";
mysqli_close($cnct);
As you can see, the fetch statement is exactly the same in both scripts, but I get the following error when running the second script:
[07-Aug-2025 17:26:25 America/Boise] PHP Fatal error: Uncaught TypeError: mysqli_fetch_assoc(): Argument #1 ($result) must be of type mysqli_result, bool given in /home1/wardrick/public_html/create_table.php:13
Stack trace:
#0 /home1/wardrick/public_html/create_table.php(13): mysqli_fetch_assoc(true)
#1 {main}
thrown in /home1/wardrick/public_html/create_table.php on line 13
It seems that it doesn’t like the $doit argument, but it is the same as it is in the script that does work. (the $doit = msqli… statement is also the same.) Does somebody know what the problem is?