Matt Ridge Posted November 8, 2011 Share Posted November 8, 2011 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); ?> Link to comment https://forums.phpfreaks.com/topic/250688-warning-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql-result/ Share on other sites More sharing options...
ManiacDan Posted November 8, 2011 Share Posted November 8, 2011 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. Link to comment https://forums.phpfreaks.com/topic/250688-warning-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql-result/#findComment-1286188 Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2011 Share Posted November 8, 2011 ...or you cannot mix a mysql_ (non-i) statement with mysqli_ statements. You would need to use mysqli_fetch_array(). Link to comment https://forums.phpfreaks.com/topic/250688-warning-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql-result/#findComment-1286200 Share on other sites More sharing options...
ManiacDan Posted November 8, 2011 Share Posted November 8, 2011 Damn man, great catch. Link to comment https://forums.phpfreaks.com/topic/250688-warning-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql-result/#findComment-1286247 Share on other sites More sharing options...
Matt Ridge Posted November 8, 2011 Author Share Posted November 8, 2011 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. Link to comment https://forums.phpfreaks.com/topic/250688-warning-mysql_fetch_array-supplied-argument-is-not-a-valid-mysql-result/#findComment-1286266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.