LOSTBOY Posted November 23, 2007 Share Posted November 23, 2007 hi am an PHP Beginner when i try this code it throws if (!in_array($qno,$tempArray)) Warning: in_array() [function.in-array]: Wrong datatype for second argument what can i do to overcome this warning! Thanks in Advance Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/ Share on other sites More sharing options...
rajivgonsalves Posted November 23, 2007 Share Posted November 23, 2007 well it just states that the second argument is not an array Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397424 Share on other sites More sharing options...
rajivgonsalves Posted November 23, 2007 Share Posted November 23, 2007 as for overcoming it this code should work, you just stop the error not the functionality if (!@in_array($qno,$tempArray)) Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397425 Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 thanks for ur valuable reply ............ Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397429 Share on other sites More sharing options...
rajivgonsalves Posted November 23, 2007 Share Posted November 23, 2007 No problem at all, but I would suggest to you never use the @ operator to overide an error it becomes very hard to debug, find out why $tempArray is not an array and try to overcome it before the if statement (something like if its not an array, initialize it to an array), it would be a good programming practice Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397431 Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 here is my code.....actually i want to generate Randam Quiz's .....as single quiz per page here $_session['$max'] is the maximum questions that user select from our db if (!isset ($_SESSION['$max'])){ $_SESSION['$max']=$_POST['sel_ques']; $tempArray= array(); } do { $qno=rand(1, $_SESSION['max']); if (!@in_array($qno,$tempArray)) { $tempArray[$r] = $qno; $query="SELECT * FROM question WHERE qid=$qno" ; $result=mysql_query($query); $id=mysql_result($result,$i,"qid"); $question=mysql_result($result,$i,"question"); mysql_result($result,$i,"question"); $query1="SELECT * from answer where qid=$id"; $result1=mysql_query($query1); $r++; } But it not working properly .......plz give ur suggestions Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397436 Share on other sites More sharing options...
rajivgonsalves Posted November 23, 2007 Share Posted November 23, 2007 try this code if (!isset ($_SESSION['$max'])){ $_SESSION['$max']=$_POST['sel_ques']; } if (!isset($_SESSION['tempArray'])) { $_SESSION['tempArray'] = array(); } $query = "SELECT * FROM question limit 1"; if (!empty($_SESSION['tempArray'])) { $query="SELECT * FROM question WHERE qid not in (".implode(",",$_SESSION['tempArray']).") limit 1" ; } $result=mysql_query($query); $id=mysql_result($result,1,"qid"); $_SESSION['tempArray'][] = $id; $question=mysql_result($result,1,"question"); mysql_result($result,$i,"question"); $query1="SELECT * from answer where qid=$id"; $result1=mysql_query($query1); and please backup your old code incase I am wrong Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397440 Share on other sites More sharing options...
LOSTBOY Posted November 23, 2007 Author Share Posted November 23, 2007 ur code is not wrong ...but doesnt bring any data from data base.................................................... Link to comment https://forums.phpfreaks.com/topic/78535-wrong-datatype-for-second-argument-while-using-in_array/#findComment-397450 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.