Jump to content

PHP and MySQL query error


nloding

Recommended Posts

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'].'&nbsp; &nbsp; &nbsp; <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]
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

[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'].'&nbsp; &nbsp; &nbsp; <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]
Link to comment
Share on other sites

[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?
Link to comment
Share on other sites

[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'].'&nbsp; &nbsp; &nbsp; <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()
Link to comment
Share on other sites

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!??
Link to comment
Share on other sites

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=34515

I guess I just can't use mysqli ... :(
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.