Jump to content

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result: ?


Matt Ridge

Recommended Posts

Hi, first post, and yes it is a question.

 

I am stuck, and not by choice, this error has given me more headaches than I care to admit.

 

I have a script that I am attempting here, that is very simple for now, all I want to show is the Added_By field to show it is accessing the database correctly and the right row/line all together.

 

The page to test this at is here:

 

http://kaboomlabs.com/PDI/@dm!n/viewncmr.php?id=2

 

The Error is this:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/pawz/public_html/kaboomlabs.com/PDI/@dm!n/viewncmr.php on line 18

 

This is row 18:    $row = mysql_fetch_array($data);

 

Now I know this is not a secure form yet, I am working on getting the basic functions down then I'll secure it, so please let me worry about that when the time comes.

 

Here is the script, can anyone see a blatant issue or not? Thanks in advance.

 

<?php
  require_once('../connectvars.php');

        // Connect to the database
        $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  // Grab the profile data from the database
  if (!isset($_GET['id'])) {
    $query = "SELECT * FROM ncmr WHERE id = '" . $_SESSION['id'] . "'";
  }
  else {
    $query = "SELECT * FROM ncmr WHERE id = '" . $_GET['id'] . "'";

  $data = mysqli_query($dbc, $query);
  }
  if (mysqli_num_rows($data) == 1) {
    // The user row was found so display the user data
    $row = mysql_fetch_array($data);

echo '<fieldset>';

        if (!empty($row['Added_By'])) {
        echo '<div id ="added"><label>Added By:</label>' . $row['Added_By'] . '</div></fieldset>';
        }
}
  // End of check for a single row of user results
  else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }

  mysqli_close($dbc);
?>

That error (which is easily googled) tells you that $data is not what you expect it to be.  In your case, it's probably null since I see this block:

 

  if (!isset($_GET['id'])) {
    $query = "SELECT * FROM ncmr WHERE id = '" . $_SESSION['id'] . "'";
  }
  else {
    $query = "SELECT * FROM ncmr WHERE id = '" . $_GET['id'] . "'";

  $data = mysqli_query($dbc, $query);
  }

If $_GET['id'] isn't set, $data never gets populated.

I actually got the script from a headfirst book, seems I am going to stop using their examples.  I decided to write my own up and it looks like I solved it myself.

 

<?php
  require_once('../connectvars.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  // Grab the profile data from the database
  if (!isset($_GET['id'])) {
    $query = "SELECT * FROM ncmr WHERE id = '$id'";
  }
  else {
    $query = "SELECT * FROM ncmr WHERE id = '" . $_GET['id'] . "'";
  }
  $data = mysqli_query($dbc, $query);

  if (mysqli_num_rows($data) == 1) {
    // The user row was found so display the user data
    $row = mysqli_fetch_array($data);
  
		if (!empty($row['Added_By'])) {
			echo '<tr><td>Added By: </td><td>' . $row['Added_By'] . '</td></tr>';

   
  }
}  
}
// End of check for a single row of user results
  else {
    echo '<p class="error">There was a problem accessing your profile.</p>';
  }
  mysqli_close($dbc);
?>

 

It works, I'm happy... thanks for the help though.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.