Put all the associative array in one variable - php

I have some associative array whose values are inserted from a while{}.
//some sql select
$comar=array();
while($row=mysql_fetch_array($result)){
//another sql countrow
$rowdd=mysql_fetch_row($result);
$postuid=$row[2];
$comar[$postuid]= $rowdd[count(idnum)];
$_SESSION['comar']=$comar;
}
But this code just displays the idnum for last postuid. How do i add it all to one variable and then put that in a session.

Don't set your session variable in the while loop
count(idnum). This makes no sense. Is idnum a constant. If so your fetching the same row.
What do you mean another SQL countrow. You're trying to fetch a row for which you have alreay run a fetch_array on. This makes no sense, and is probably the root problem

Related

Creating an array by looping through a mySQL database, capturing entries only once in PHP

i'm relatively new to coding and I need a little help. I'm basically trying to loop through an entry in a mySQL database and push any new entry into an array , so that it only comes up once in my array.
// SQL query
$response = $bdd->query('SELECT serie_bd FROM inventaire_bd');
//creating array to group all elements from the db so that they do not repeat
$serie_bd_groupe=array();
while($data_collected = $response->fetch())
{
if(array_key_exists($data_collected,$serie_bd_groupe)==false)
{
array_push($data_collected,$serie_bd_groupe);
}
}
Will this work? - it seems like the loop will just stay stuck after it comes accross an entry a second time because the if statement wont execute itself.
Also in the future, are their any php equivalent to jsfiddle.net so i can test code syntaxically?
Thank you for your time
Your array keys will be default integers, so you don't want to check those. Instead of this:
if(array_key_exists($data_collected,$serie_bd_groupe)==false)
you should do this:
if(!(in_array($data_collected,$serie_bd_groupe)))
http://php.net/manual/en/function.in-array.php
On the other hand, if you're expecting your collected data to be the array key rather than value, you'd do something like this, instead of your array_push:
$serie_bd_groupe[$data_collected] = 1;
then your key check would work.
If you are looking for UNIQUE values (serie_bd) from your database, update your query to include "DISTINCT" like this:
$bdd->query('SELECT DISTINCT serie_bd FROM inventaire_bd');
On the other hand, I think you are looking for http://phpfiddle.org/

save MySQL rows in an array and save it in session variable

I want to get rows from MySQL Database and save two specific values(row[0] & row[6]) in $_SESSION variable for comparison later on.
The code looks like below:
$sqlResult =mysql_query("SELECT * FROM questionbank WHERE quizName ='$quizNames' ORDER BY RAND() limit 6")
while ($row= mysql_fetch_array($sqlResult))
{
$questionPkId = $row[0];
echo $_SESSION['$questionPkId'] = $row[6];
}
The Problem is I can't get this values in other pages. Can I make an associative array with session variable (though i tried array_push(), But it did not work).
A couple issues with your code. First up, make sure you call session_start on any page that uses sessions. Without it, you will not be able to access the variables previously set.
Second up, when a variable is called inside single quotes it is taken literally, meaning your session define statement is always assigning the variable to same thing, and hence overwriting what it should be.
$_SESSION[$questionPkId] = $row[6];
And that will assign the value to the actual value of $questionPkId. Aside from those issues, your session items should work, and if they are not you will need to provide us with more of your code.
Try without single quotes:
$_SESSION[$questionPkId] = $row[6];

fetch a single postgres record as an associative array without using while loop

If I have a statement that I know will return only one result:
$emp_query = "SELECT * FROM table WHERE id = 'employee_id' LIMIT 1"
Is there any way I can access the result of pg_query($con, $csr_query); as an associative array without using a pg_fetch_assoc and a while loop? A while loop seems unnecessary when I know I'm retrieving only one record. Maybe there is some way in PDO as well, I'm just setting it up this way first because I'm more comfortable with the simple way. Thanks.
Something like pg_fetch_result:
http://www.php.net/manual/en/function.pg-fetch-result.php

mysql In clause to avoid loop

I have to retrieve the history of a user and I have 4 tables whose data depend on each other.I can retrieve the data using loops,but I instead used the "where IN ()" clause and I implode the output of the previous query.However,if the list I provide to "where IN()" is empty it return an error.Is it that IN() cannot be empty?
When imploding an array for the IN clause, i do one of two things
1: Check if you even need to run the query at all
if(!empty($some_array)) {
//run mysql query
}
else {
// if you need to do something if the array is empty, such as error or set some defaults, do it here
}
2: A value in the array initiliser which is not ever in the database (for example, if im selecting based on a auto incrememnt id, i use zero as a default array value to stop any issues with empty data sets, as zero will never be in my id column).
$some_array = array(0);
You can add an empty value to the start, such as IN (0,your values here)

Error in Comparing the same column in mysql

I am fetching the values from the column as the value of integer and doing this for two user so i tried to get the value from the table and compare it but unfortunately for both greater and smaller comparsion i am getting the same result nothing changed.
How do i compare the column values?
My code is like below-----
$sqlres="select membership from register where mid='".$_SESSION['mid']."' ";
$pres=mysql_query($sqlres);
$prest=mysql_fetch_array($pres);
$sqlres1="select membership from register where matri_id='".$row['mtr_id']."' ";
$pres1=mysql_query($sqlres);
$prest1=mysql_fetch_array($pres);
if($pres<$pres1)
{
//somethiung enter code here
}
First of all don't use mysql_* it is deprecated, use mysqli_* or PDO instead.
As for your question, mysql_fetch_array as the name suggests, returns an array, not a single value, so you need to get the first value in the array:
if($pres[0]<$pres1[0])
{
//somethiung enter code here
}
You save the result in $prest and $prest1 (with "t"), not in $pres and $pres1. I suggest always using the variable $query for the query string and $result for the result table. Only when you fetch the result should you use a custom variable name to not get confused.
You can use something like
if($prest['membership']<$prest1['membership']){//do stuff here}
In the piece of code that you provided, you are comparing the resources that mysql_query returned not the values of the columns. You have to do this:
if( $prest['membership'] < $prest1['membership'] ){
//Some stuff going here
}
Advice: Name your variables properly. Use more describing names. After 2 months you won't remember the difference between $prest and $prest1

Categories