Jump to content

mysql fetch assoc array?


EchoFool

Recommended Posts

Hey - i was told that mysql_fetch_assoc($Query); Is automatically an array...

 

So i tried in a function to do return ( mysql_fetch_assoc($Query);

 

Then did a "in_array" against the function but it doesn't work. When i print the array in the function i get "ArrayArrayArray"

 

This is how i do the in array:

 

<?php 

function completedresearch($User){
//query to get all rows in user/research table which have been completed assigned to $Query
return(mysql_fetch_assoc($Query)
}

//general stuff here

If(in_array($ResearchID,completedresearch($_SESSION['Current_User'])){
Echo 'Research completed';
}Else{
Echo 'Not Completed';
}

?>

 

Hope you can explain where im going wrong because it should be saying "Research Completed" but it doesn't.

Link to comment
Share on other sites

Not sure if this is a syntax error due to retyping here but this line is wrong:

 

return(mysql_fetch_assoc($Query)

 

..which can and should be:

 

return mysql_fetch_assoc($Query);

 

Along with that, the variable $Query is not accessible via this function because it is not within scope. You will need to pass that along with the user:

 

completedresearch($Query, $_SESSION['Current_User'])

 

and change your function:

 

function completedresearch($Query, $User){

 

So, completed, should look like so:

 

<?php 

function completedresearch($Query, $User){
//query to get all rows in user/research table which have been completed assigned to $Query
return mysql_fetch_assoc($Query);
}

//general stuff here

If(in_array($ResearchID,completedresearch($Query, $_SESSION['Current_User'])){
Echo 'Research completed';
}Else{
Echo 'Not Completed';
}

?>

 

Hope that helps.

Link to comment
Share on other sites

Oh yeh i just retyped to get the idea accorss  i get no syntax errors its just a logic error..

 

You kinda got confused with my function ill type it out full:

 

<?php 

function completedresearch($User){
$Query = mysql_query("SELECT RecordID FROM researchusers WHERE Completed='1' AND UserID='$UserID' ORDER BY ResearchID ASC") or die(mysql_error());
return mysql_fetch_assoc($Query);
}

//general stuff here

If(in_array($ResearchID,completedresearch($_SESSION['Current_User'])){
Echo 'Research completed';
}Else{
Echo 'Not Completed';
}

?>

 

Hope this now explains a bit better :)

Link to comment
Share on other sites

Look at your param:

 

function completedresearch($User){

 

The name is $User.

 

Now look at the variable that you are using in the query.  Is that that same variable name?

 

You can figure these things out with a simple echo statement, but I have to point out that you don't actually check to see if the mysql_query returns a valid result set.  If you make that type of assumption, when things go wrong you get a runtime error, because fetching against a 'false/null' result set causes a runtime error.

Link to comment
Share on other sites

Ok turns out it doesn't automatically make an array with mysql_fetch_assoc. Thats a shame :(

 

I use it all the time and works fine for me..? what does mysql_fetch_array produce if you use that instead here?

 

print_r($Query);

 

If your query has produced a mysql resource it will throw that into an associative array with mysql_fetch_assoc. In fact, once of the only differences is that mysql_fetch_array is twice in size because it produces both an associative array and regular array.

Link to comment
Share on other sites

mysql_fetch_assoc() most certainly does return an array, when it actually has something to fetch.  It returns exactly what the name implies -- an array keyed by the names of the column.  You have exactly one column in your select statement, so the array for one fetch would look something like:

 

$row = array('RecordID' => ?);

 

From your code, I'm not sure what you're trying to do, since I don't really understand your database design.  What I can say is this -- when you fetch one row, you get one row.  That is not by any means the entire result SET, it is only the first row -- and in this case a single value.  If you're expecting a whole series of rows, your code is not accounting for that. 

 

You also don't want an array of arrays with a single key/value pair, since you're trying to use in_array() apparently to try and find the value.  This would be more likely the code you would want:

 

function completedresearch($User){
  $Query = mysql_query("SELECT ......") or die(mysql_error());
   $rows = array()
   while ($row = mysql_fetch_assoc()) {
       $rows[] = $row['RecordID'];
   }
   return $rows;
}

 

Again just my best guess, given the information you've provided to this point, which is far from clear.  I don't know why you do a query that references one column, even though your initial query tried to ORDER BY another column, and then seems to look for another value.  It's about as clear as mud, and also begs the question of why your query doesn't figure this out more directly... the database is perfectly able to return rows or not based on WHERE criteria, so I can't figure out why you would want to pull out a result set and then search through it, when the database is better and faster at doing that itself, and just giving you the exact result set specified.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.