nloding Posted January 25, 2007 Share Posted January 25, 2007 Why doesn't this work? I get the following error:"Fatal error: Call to a member function fetch_array() on a non-object in F:\wwwroot\write\index.php on line 49"Line 49 is the if statement checking $result->fetch_array(). If I remove the quotes around the table name (from 'pieces' to pieces), I get no error, but the while loop doesn't seem to process because nothing gets echo'd to the HTML.[code]<?php $mysqli = new mysqli("localhost", "user", "pass", "library"); if (mysqli_connect_errno()) { echo 'Error. Could not connect to database. Please try again later.'; exit; } $query = "select * FROM 'pieces'"; $result = $mysqli->query($query); if (!$result->fetch_array()) { echo '<p>No information present.</p>'; exit; } while ($row = $result->fetch_array()) { echo '<p class="line-height">\n'; echo '<span class="bold">title:</span> '.$row['title'].'<br />\n'; echo '<span class="bold">author:</span> '.$row['auth'].'<br />\n'; echo '<span class="bold">pages:</span> '.$row['pages'].' <span class="bold">genre:</span> '.$row['genre'].'<br />\n'; echo '</p>\n'; echo '\n'; echo '<p class="line-height">\n'; echo '<span class="bold">description:</span> '.$row['desc'].'</p>\n</div>\n'; } ?>[/code] Quote Link to comment Share on other sites More sharing options...
Caesar Posted January 25, 2007 Share Posted January 25, 2007 [code]<?phpwhile ($row = $result->fetch_array())?>[/code]For starters, where's the array you're telling MySQL to fetch in this loop? Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 It's the values of the MySQL table named pieces. Quote Link to comment Share on other sites More sharing options...
Caesar Posted January 25, 2007 Share Posted January 25, 2007 [quote author=nloding link=topic=123924.msg512857#msg512857 date=1169687337]It's the values of the MySQL table named pieces.[/quote]Where is the array in that line? You're telling that function to fetch an array, but you don't state which array to fetch. If that's the actual code you're using, how are you passing the value to the fetch_array function?See....[code]<?php $query = $db->query("SELECT * FROM table WHERE userid='$userid'"); $query = $db->fetch_array($query);?>[/code]In addition, if you're going to use a while loop, why not:[code]<?php $query = "SELECT * FROM table WHERE userid='$userid'"; $info = $db->query($query, $var); while($row = $db->fetch_row($info)) { echo"$row[0] $row[1] <br />"; }?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 [quote]Where is the array in that line? You're telling that function to fetch an array, but you don't state which array to fetch. If that's the actual code you're using, how are you passing the value to the fetch_array function?[/quote]You don't need to. This is mysqli_* not mysql_*.You need to make sure your query is successfull before trying to use it.[code]<?php $mysqli = new mysqli("localhost", "user", "pass", "library"); if (mysqli_connect_errno()) { echo 'Error. Could not connect to database. Please try again later.'; exit; } $query = "select * FROM 'pieces'"; if ($result = $mysqli->query($query)) { if (!$result->fetch_array()) { echo '<p>No information present.</p>'; exit; } while ($row = $result->fetch_array()) { echo '<p class="line-height">\n'; echo '<span class="bold">title:</span> '.$row['title'].'<br />\n'; echo '<span class="bold">author:</span> '.$row['auth'].'<br />\n'; echo '<span class="bold">pages:</span> '.$row['pages'].' <span class="bold">genre:</span> '.$row['genre'].'<br />\n'; echo '</p>\n'; echo '\n'; echo '<p class="line-height">\n'; echo '<span class="bold">description:</span> '.$row['desc'].'</p>\n</div>\n'; } } else { echo "Query failed. ".$mysqli->error; }?>[/code] Quote Link to comment Share on other sites More sharing options...
Caesar Posted January 25, 2007 Share Posted January 25, 2007 [quote author=thorpe link=topic=123924.msg512869#msg512869 date=1169688680]This is mysqli_* not mysql_*.[/quote]Crap...just noticed. :P Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 [quote author=thorpe link=topic=123924.msg512869#msg512869 date=1169688680]You need to make sure your query is successfull before trying to use it.[/quote]OK, but it's obviously not successful here ... why? Is it the code or is it the database itself?I either get that error message, or the while loop isn't parsed. Why? Quote Link to comment Share on other sites More sharing options...
trq Posted January 25, 2007 Share Posted January 25, 2007 With the code Ive posted, yoiu should get some other error. You have a syntax error in your query.[code=php:0]$query = "select * FROM pieces";[/code]No quotes around pieces. Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 [quote author=thorpe link=topic=123924.msg512954#msg512954 date=1169700227]With the code Ive posted, yoiu should get some other error.[/quote]Nope. Notta. It appears to connect to the database because it passes the if(mysqli_connect_errno()) test, but then nothing else gets parsed to the HTML output. Here is my new code:[code]<?php $mysqli = new mysqli("localhost", "user", "pass", "library"); if (mysqli_connect_errno()) { echo 'Error. Could not connect to database. Please try again later.'; exit; } $query = 'SELECT * FROM pieces'; if ($result = $mysqli->query($query)) { if (!$result->fetch_array()) { echo '<p>No information present.</p>'; exit; } while ($row = $result->fetch_array()) { echo '<p class="line-height">\n'; echo '<span class="bold">title:</span> '.$row['title'].'<br />\n'; echo '<span class="bold">author:</span> '.$row['auth'].'<br />\n'; echo '<span class="bold">pages:</span> '.$row['pages'].' <span class="bold">genre:</span> '.$row['genre'].'<br />\n'; echo '</p>\n'; echo '\n'; echo '<p class="line-height">\n'; echo '<span class="bold">description:</span> '.$row['desc'].'</p>\n</div>\n'; } } else { echo 'Query failed: '.$mysqli->error; } ?>[/code]I added the if/else to check if the query failed, and attempted to include the error, but nothing gets displayed. I have some HTML before this PHP code and that displays properly. I have some HTML after this code and that displays properly. The file is named index.php. PHP and MySQL are installed properly, as I tested them with basic tests, like displaying the version number. I verified the spelling of the database and table names. I verified the username and password ...What else is there?EDIT: It also does not work with $result->fetch_assoc() Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 OK, so I did a test, and after each step I put this line:echo '<p>Query was successful!</p>';or echo '<p>While loop should be done</p>';and all those get printed. But nothing inside the while loop get parsed. Why? Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 Weird, the board wouldn't let me edit my last post ... anyhow ... another update.I ran the query exactly as typed in the PHP within phpMyAdmin, and it worked fine. It pulled the test information I had entered into the table.So I don't understand why the while loop isn't processing!?? Quote Link to comment Share on other sites More sharing options...
nloding Posted January 25, 2007 Author Share Posted January 25, 2007 So I've tried some more stuff ... here's ANOTHER update!!I tried replacing the while loop with a for statement, and it displayed the HTML part of the echo statements, but nothing from the $row array. So I added in this line:$num_results = $result->num_rows;echo $num_results;And that works perfectly! So here is my code once again:[code]<?php $mysqli = new mysqli("localhost", "user", "pass", "library"); if (mysqli_connect_errno()) { echo 'Error. Could not connect to database. Please try again later.'; exit; } echo '<p>Connection successful!</p>'; $query = "SELECT * FROM pieces"; $result = $mysqli->query($query); if ($result) { echo '<p>Query successful!</p>'; if (!$result->fetch_array()) { echo '<p>No information present.</p>'; exit; } echo '<p>There is information!</p>'; $num_results = $result->num_rows; echo $num_results; while ($row = $result->fetch_array()) { echo "<p class=\"line-height\">\n"; echo "<span class=\"bold\">title:</span> ".$row['title']."<br />\n"; echo "<span class=\"bold\">author:</span> ".$row['auth']."<br />\n"; echo "<span class=\"bold\">pages:</span> ".$row['pages']." <span class=\"bold\">genre:</span> ".$row['genre']."<br />\n"; echo "</p>\n"; echo "\n"; echo "<p class=\"line-height\">\n"; echo "<span class=\"bold\">description:</span> ".$row['desc']."</p>\n</div>\n"; } echo '<p>While loop should be done</p>'; } else { echo 'Query failed: '.$mysqli->error; } ?>[/code]And here is what my output looks like:[code]<p>Connection successful!</p><p>Query successful!</p><p>There is information!</p>1<p>While loop should be done</p>[/code]So what is wrong with that while loop?????????????????????????Someone help, I'm tearing my hair out here! LOL!EDIT: In doing some more research, I found this thread and there seems to be no answer for it!http://bugs.php.net/bug.php?id=34515I guess I just can't use mysqli ... :( Quote Link to comment Share on other sites More sharing options...
Destruction Posted January 25, 2007 Share Posted January 25, 2007 I believe it is because of the internal array pointer as discussed in WebChat. When you are checking with if(!$result->fetch_array()) you're moving the internal pointer forward one place and there's only one record. Thus, when it tries to loop, there's nothing for it to loop. Comment this out and try again.Ideally, try checking the number of rows returned is greater than 0 rather than using fetch_array() to check.Dest Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.