galvin Posted April 17, 2011 Share Posted April 17, 2011 This is PHP 101, but I'm struggling to see the obvious. I have the query below which brings back a list of quizids. I put them into an array using mysql_fetch_array. I then want to check to see if a certain quizid is actually in the quizzes table, so I thought I could simply us in_array. But it's only checking the first quizid. That doesn't make sense to me. Isn't mysql_fetch_array creating an array with all values automatically? If I actually have to use a "while" loop or something, can someone help me with how to write that code most efficiently. $query = "SELECT quizid FROM quizzes"; $getquizids = mysql_query($query, $connection); if (!$getquizids) { die("Database query failed: " . mysql_error()); } else { $allquizids=mysql_fetch_array($getquizids); if (in_array($_GET['quizid'], $allquizids)) { //continue since quiz exists } else { redirect_to('index.php'); } } Quote Link to comment https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/ Share on other sites More sharing options...
dcro2 Posted April 17, 2011 Share Posted April 17, 2011 No, mysql_fetch_array fetches one row at a time. Please read the manual entry. I think you'd be better off doing this in the query anyways... $query = "SELECT quizid FROM quizzes WHERE quizid = '{$_GET['quizid']}'"; $result = mysql_query($query, $connection) or die("Database query failed: " . mysql_error()); if(mysql_num_rows($result) == 0) redirect_to('index.php'); //no rows found, meaning the quizid is not in the table Quote Link to comment https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/#findComment-1202742 Share on other sites More sharing options...
galvin Posted April 17, 2011 Author Share Posted April 17, 2011 I tried that method, but if a user manipulates the URL and puts something like quizid=345e2f21ef, it gives an error rather than just redirect them to the home page. I want to avoid any error pages if possible, so I think I have to do the thing where I grab all quizids, put them into an array and search for the specific quizid in the array. Thanks for letting me mysql_fetch_array only does one row at a time. I just assumed it made a normal array immediately. I gotta stop assuming so much I'll keep hacking away at it Quote Link to comment https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/#findComment-1202743 Share on other sites More sharing options...
galvin Posted April 17, 2011 Author Share Posted April 17, 2011 I got it working with the code below, but I'm not convinced it's the proper/most efficient way to do it. If anyone sees any issues with the code I wrote, please let me know... $query = "SELECT quizid FROM quizzes"; $getquizids = mysql_query($query, $connection); if (!$getquizids) { die("Database query failed: " . mysql_error()); } else { while ($allquizids=mysql_fetch_array($getquizids)) { $quizids[]=$allquizids[0]; } if (in_array($_GET['quizid'], $quizids)) { //continue since quiz exists } else { redirect_to('index.php'); } } Quote Link to comment https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/#findComment-1202744 Share on other sites More sharing options...
dcro2 Posted April 17, 2011 Share Posted April 17, 2011 You're trying to avoid error pages, but you have one in your code... You really should let MySQL handle it... try this: $query = "SELECT quizid FROM quizzes WHERE quizid = '".mysql_real_escape_string($_GET['quizid'])."'"; $result = mysql_query($query, $connection); if(!$result || mysql_num_rows($result) == 0) redirect_to('index.php'); //if mysql error OR no rows found, meaning the quizid is not in the table Quote Link to comment https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/#findComment-1202755 Share on other sites More sharing options...
galvin Posted April 18, 2011 Author Share Posted April 18, 2011 Beautiful, exactly what I wanted and it's nice, concise code to boot. Thanks a lot!!! Quote Link to comment https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/#findComment-1202787 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.