Xtremer360 Posted January 27, 2011 Share Posted January 27, 2011 The following code is giving me <b>Warning</b>: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in <b>/home/content/y/a/n/yankeefaninkc/html/efedmanager/processes/menuitem.php</b> on line <b>52</b><br /> Line 52 is while ($row = mysqli_fetch_array($result)) { if (isset($_POST['submitmenuitem'])) { $menuid = (int) $_POST['menuid']; $itemname = trim($_POST['itemname']); $itemnameSQL = mysqli_real_escape_string($dbc, $itemname); $itemurl = trim($_POST['itemurl']); $itemurlSQL = mysqli_real_escape_string($dbc, $_POST['itemurl']); $sortorder = (int) $_POST['sortorder']; $contentpage = mysqli_real_escape_string($dbc, $_POST['contentpage']); $newscategory = mysqli_real_escape_string($dbc, $_POST['newscategory']); $application = mysqli_real_escape_string($dbc, $_POST['application']); $query = 'SELECT *' . ' FROM menuitems' . ' WHERE menu_id = ' . $menuid . ' AND (itemname = "' . $itemnameSQL . '"' . ($itemurlSQL ? ' OR itemurl = "' . $itemurlSQL . '"' : '') . ($contentpage ? ' OR contentpage_id = ' . $contentpage : '') . ($application ? ' OR application_id = ' . $application : '') . ($newscategory ? ' OR newscategory_id = ' . $newscategory : '') . ')'; $result = mysqli_query ( $dbc, $query ); // Run The Query echo $query; if (mysqli_num_rows($result) == 0) { $query = "INSERT INTO `menuitems` (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id, application_id, creator_id, datecreated, enabled) VALUES ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."', '".$newscategory."', '".$application."', 1, NOW(), 0)"; mysqli_query($dbc, $query); $result = "good"; } else { $result = ''; while ($row = mysqli_fetch_array($result)) { if ($row['itemname'] == $itemname) {$result .= 'bad1';} if ($itemurl && $row['itemurl'] == $itemurl) {$result .= 'bad2';} else if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else if ($application && $row['application_id'] == $application) {$result .= 'bad4';} else if ($contentpage && $row['contentpage_id'] == $contentpage) {$result .= 'bad5';} else if ($itemurl && $row['itemurl'] == $itemurl && $itemname && $row['itemname'] == $itemname) {$result .= 'bad6';} else if ($contentpage && $row['contentpage_id'] == $contentpage && $row['itemname'] == $itemname) {$result .= 'bad7';} else if ($application && $row['application_id'] == $application && $row['itemname'] == $itemname) {$result .= 'bad8';} else if ($newscategory && $row['newscategory_id'] == $newscategory && $row['itemname'] == $itemname) {$result .= 'bad9';} } } } Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/ Share on other sites More sharing options...
tomtimms Posted January 27, 2011 Share Posted January 27, 2011 get your query error, add this under your result. echo mysql_error(); Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165745 Share on other sites More sharing options...
Pikachu2000 Posted January 27, 2011 Share Posted January 27, 2011 Problem is here ---> $result = "good"; Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165750 Share on other sites More sharing options...
Xtremer360 Posted January 27, 2011 Author Share Posted January 27, 2011 What's wrong with it? My test should be coming up with bad# anyway. So it should be inside the else loop of the if Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165751 Share on other sites More sharing options...
DavidAM Posted January 27, 2011 Share Posted January 27, 2011 Actually, the problem is here: $result = ''; while ($row = mysqli_fetch_array($result)) { You have assigned an empty string to $result. It is not a query result anymore, so you can't use the mysqli functions against it. It looks like you are trying to use the same variable name for two different purposes. That's a bad idea, and it is impossible when the lifetime of the values overlap. Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165766 Share on other sites More sharing options...
Xtremer360 Posted January 27, 2011 Author Share Posted January 27, 2011 What should I do? Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165785 Share on other sites More sharing options...
tomtimms Posted January 27, 2011 Share Posted January 27, 2011 This is probably the most messed up looking code I have seen, what exactly is trying to be done? Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165795 Share on other sites More sharing options...
DavidAM Posted January 27, 2011 Share Posted January 27, 2011 What should I do? Use a different name for one of the variables. Since all we can see is this small segment of code, I would suggest changing the name of the query result: if (isset($_POST['submitmenuitem'])) { $menuid = (int) $_POST['menuid']; $itemname = trim($_POST['itemname']); $itemnameSQL = mysqli_real_escape_string($dbc, $itemname); $itemurl = trim($_POST['itemurl']); $itemurlSQL = mysqli_real_escape_string($dbc, $_POST['itemurl']); $sortorder = (int) $_POST['sortorder']; $contentpage = mysqli_real_escape_string($dbc, $_POST['contentpage']); $newscategory = mysqli_real_escape_string($dbc, $_POST['newscategory']); $application = mysqli_real_escape_string($dbc, $_POST['application']); $query = 'SELECT *' . ' FROM menuitems' . ' WHERE menu_id = ' . $menuid . ' AND (itemname = "' . $itemnameSQL . '"' . ($itemurlSQL ? ' OR itemurl = "' . $itemurlSQL . '"' : '') . ($contentpage ? ' OR contentpage_id = ' . $contentpage : '') . ($application ? ' OR application_id = ' . $application : '') . ($newscategory ? ' OR newscategory_id = ' . $newscategory : '') . ')'; # CHANGE THE NAME HERE $Qresult = mysqli_query ( $dbc, $query ); // Run The Query echo $query; # AND CHANGE THE NAME HERE if (mysqli_num_rows($Qresult) == 0) { $query = "INSERT INTO `menuitems` (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id, application_id, creator_id, datecreated, enabled) VALUES ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."', '".$newscategory."', '".$application."', 1, NOW(), 0)"; mysqli_query($dbc, $query); $result = "good"; } else { $result = ''; # AND CHANGE THE NAME HERE while ($row = mysqli_fetch_array($Qresult)) { if ($row['itemname'] == $itemname) {$result .= 'bad1';} if ($itemurl && $row['itemurl'] == $itemurl) {$result .= 'bad2';} else if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else if ($application && $row['application_id'] == $application) {$result .= 'bad4';} else if ($contentpage && $row['contentpage_id'] == $contentpage) {$result .= 'bad5';} else if ($itemurl && $row['itemurl'] == $itemurl && $itemname && $row['itemname'] == $itemname) {$result .= 'bad6';} else if ($contentpage && $row['contentpage_id'] == $contentpage && $row['itemname'] == $itemname) {$result .= 'bad7';} else if ($application && $row['application_id'] == $application && $row['itemname'] == $itemname) {$result .= 'bad8';} else if ($newscategory && $row['newscategory_id'] == $newscategory && $row['itemname'] == $itemname) {$result .= 'bad9';} } } } You will have to review the rest of your code to see if anything else needs to be changed Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1165816 Share on other sites More sharing options...
Xtremer360 Posted January 27, 2011 Author Share Posted January 27, 2011 Thank you all for assistance. I'm getting somewhere and I can tell I almost have this completely corrected. However I know I have a mistake whether I need parenthesis around something or I'm not sure but when the the itemname and itemurl are found together as being already in the database it should be echoing out bad6 via my code. What is preventing it from doing so? Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166067 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 post latest code. Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166078 Share on other sites More sharing options...
Xtremer360 Posted January 27, 2011 Author Share Posted January 27, 2011 if (isset($_POST['submitmenuitem'])) { $menuid = (int) $_POST['menuid']; $itemname = trim($_POST['itemname']); $itemnameSQL = mysqli_real_escape_string($dbc, $itemname); $itemurl = trim($_POST['itemurl']); $itemurlSQL = mysqli_real_escape_string($dbc, $_POST['itemurl']); $sortorder = (int) $_POST['sortorder']; $contentpage = mysqli_real_escape_string($dbc, $_POST['contentpage']); $newscategory = mysqli_real_escape_string($dbc, $_POST['newscategory']); $application = mysqli_real_escape_string($dbc, $_POST['application']); $query = 'SELECT *' . ' FROM menuitems' . ' WHERE menu_id = ' . $menuid . ' AND (itemname = "' . $itemnameSQL . '"' . ($itemurlSQL ? ' OR itemurl = "' . $itemurlSQL . '"' : '') . ($contentpage ? ' OR contentpage_id = ' . $contentpage : '') . ($application ? ' OR application_id = ' . $application : '') . ($newscategory ? ' OR newscategory_id = ' . $newscategory : '') . ')'; $Qresult = mysqli_query ( $dbc, $query ); // Run The Query if (mysqli_num_rows($Qresult) == 0) { $query = "INSERT INTO `menuitems` (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id, application_id, creator_id, datecreated, enabled) VALUES ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."', '".$newscategory."', '".$application."', 1, NOW(), 0)"; mysqli_query($dbc, $query); $result = "good"; } else { $result = ''; while ($row = mysqli_fetch_array($Qresult)) { if ($row['itemname'] == $itemname){$result .= 'bad1';} if ($itemurl && $row['itemurl'] == $itemurl) {$result .= 'bad2';} else if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else if ($application && $row['application_id'] == $application) {$result .= 'bad4';} else if ($contentpage && $row['contentpage_id'] == $contentpage) {$result .= 'bad5';} else if ($itemurl && $row['itemurl'] == $itemurl && $itemname && $row['itemname'] == $itemname) {$result .= 'bad6';} else if ($contentpage && $row['contentpage_id'] == $contentpage && $row['itemname'] == $itemname) {$result .= 'bad7';} else if ($application && $row['application_id'] == $application && $row['itemname'] == $itemname) {$result .= 'bad8';} else if ($newscategory && $row['newscategory_id'] == $newscategory && $row['itemname'] == $itemname) {$result .= 'bad9';} } } } Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166091 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 question: are you getting ANYTHING, something instead of bad6? or nothing at all? Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166096 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 well, i guess a start would be to echo out the values and see what they are, before the if(): echo "itemurl: $itemurl<br />"; echo "row['itemurl']: {$row['itemurl']}<br />"; echo "itemname: $itemname<br />"; echo "row['itemname']: {$row['itemname']}<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166101 Share on other sites More sharing options...
Xtremer360 Posted January 27, 2011 Author Share Posted January 27, 2011 question: are you getting ANYTHING, something instead of bad6? or nothing at all? I'm getting for this bad1bad2 Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166108 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 okay, that makes sense. after the second condition is met, all the other else's are skipped. so bad6 will never be reached. Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166166 Share on other sites More sharing options...
Xtremer360 Posted January 27, 2011 Author Share Posted January 27, 2011 Does this mean I have to flip them all then? have bad9 at the top and bad1 at the bottom Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166173 Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 no, it means you need to modify your logic (best idea) or remove the else's and test every condition. Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166174 Share on other sites More sharing options...
Xtremer360 Posted January 29, 2011 Author Share Posted January 29, 2011 Wow I didn't know I had a response in here. Anyway I've gone through and tested all of them the bad1-bad5 work however bad6-bad9 do not. Not quite sure why. Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166867 Share on other sites More sharing options...
Xtremer360 Posted January 29, 2011 Author Share Posted January 29, 2011 Okay this whole thing works except it'll only return the right error if say the itemname matched up with its newscategory, itemurl, contentpage, or application. I want it even if the name doesn't match up. if (isset($_POST['submitmenuitem'])) { $menuid = (int) $_POST['menuid']; $itemname = trim($_POST['itemname']); $itemnameSQL = mysqli_real_escape_string($dbc, $itemname); $itemurl = trim($_POST['itemurl']); $itemurlSQL = mysqli_real_escape_string($dbc, $_POST['itemurl']); $sortorder = (int) $_POST['sortorder']; $contentpage = mysqli_real_escape_string($dbc, $_POST['contentpage']); $newscategory = mysqli_real_escape_string($dbc, $_POST['newscategory']); $application = mysqli_real_escape_string($dbc, $_POST['application']); $query = 'SELECT *' . ' FROM menuitems' . ' WHERE menu_id = ' . $menuid . ' AND (itemname = "' . $itemnameSQL . '"' . ($itemurlSQL ? ' OR itemurl = "' . $itemurlSQL . '"' : '') . ($contentpage ? ' OR contentpage_id = ' . $contentpage : '') . ($application ? ' OR application_id = ' . $application : '') . ($newscategory ? ' OR newscategory_id = ' . $newscategory : '') . ')'; $Qresult = mysqli_query ( $dbc, $query ); // Run The Query if (mysqli_num_rows($Qresult) == 0) { $query = "INSERT INTO `menuitems` (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id, application_id, creator_id, datecreated, enabled) VALUES ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."', '".$newscategory."', '".$application."', 1, NOW(), 0)"; mysqli_query($dbc, $query); $result = "good"; } else { $result = ''; while ($row = mysqli_fetch_array($Qresult)) { if (($newscategory && $row['newscategory_id'] == $newscategory) AND ($itemname && $row['itemname'] == $itemname)) {$result .= 'bad9';} else if (($application && $row['application_id'] == $application) AND ($itemname && $row['itemname'] == $itemname)) {$result .= 'bad8';} else if (($contentpage && $row['contentpage_id'] == $contentpage) AND ($itemname && $row['itemname'] == $itemname)) {$result .= 'bad7';} else if (($itemurl && $row['itemurl'] == $itemurl) AND ($itemname && $row['itemname'] == $itemname)) {$result .= 'bad6';} else if ($contentpage && $row['contentpage_id'] == $contentpage) {$result .= 'bad5';} else if ($application && $row['application_id'] == $application) {$result .= 'bad4';} else if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else if ($itemurl && $row['itemurl'] == $itemurl) {$result .= 'bad2';} else if ($itemname == $row['itemname']){$result .= 'bad1';} } } } Quote Link to comment https://forums.phpfreaks.com/topic/225800-mysqli_fetch-array/#findComment-1166871 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.