Jump to content

msql_fetch_array and in_array?


galvin

Recommended Posts

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');
		}
}

 

Link to comment
https://forums.phpfreaks.com/topic/233999-msql_fetch_array-and-in_array/
Share on other sites

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

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 :)

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');
}

}

 

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

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.