php variable inside a variable in a while query - php

I have a variable:
$category = 3;
$data = MY SQL QUERY....
while ($data2 = mysql_fetch_array( $data))
{ echo $data2['$category'] ; }
I want to get the sql value of echo $data2['3']; by using the vairable $category ...
is there a way to do this ?

Your code looks correct but you have to remove the single quotes
{ echo $data2['$category'] ; }
should be changed to
{ echo $data2[$category] ; }

Related

Why is the array is always empty at this point despite the fact that I added data there?

$result = mysqli_query($con, "SELECT * FROM users");
$usersArray=[];
tableArrayPushData($result, $usersArray);
function tableArrayPushData($result, $tableArray){
while ($row = $result->fetch_assoc()) {
$str = '';
foreach ($row as $value) {
$str = $str.$value.'|';
}
$newStr = rtrim($str, "| ");
array_push($tableArray,$newStr);
}
}
for ($i=0; $i<count($usersArray); $i++){//array is always empty at this point
echo "Ok";
echo "<br>";
}
I don't understand why, but usersArray is empty despite the fact that I added data there.
The MySQL table has rows with data, so it can't be empty.
You should use the & operator to allow the function to access the outer variable, like this:
function tableArrayPushData($result, &$tableArray) {}
Or use return.

Database Retrieval to Session Variable Storage

PHP 7 and MySQL. I want to take a set of labels stored in the database and individually place each label in its own session variable so I can use them as tab labels in my application.
I can retrieve the data and echo it but the corresponding session variables all show a not set status.
<?php
require_once('dbconnect.inc.php');
$_SESSION['histopic'] = "dash";
$numnames = 100;
for ($i = 1; $i <= $numnames; $i++) {
$stmt = $pdo->prepare('SELECT hislabelsName FROM hislabels WHERE hislabelsID = ?');
$temp = $i;
$stmt->execute(array($temp));
$result = $stmt->fetchALL();
foreach ($result as $row) {
$_SESSION['hislabelsName$i'] = $row["hislabelsName"];
}
echo $_SESSION['hislabelsName$i'] . "<br>";
echo $_SESSION['hislabelsName5'] . "<br>";
}
echo $_SESSION['hislabelsName5']; produces an undefined index error.
How do I fix this?
TIA.
You have to use double quotes like this:
$_SESSION["hislabelsName$i"] = $row["hislabelsName"];
You also do it like this:
$_SESSION["historylabelsName][$i] = $row["hislabelsName"];
Then to echo either:
echo $_SESSION["hislabelsName5"] or echo $_SESSION["hislabelsName][5];

How to use variable from a while, but outside the while?

I have this:
$nome='x.png';
if(file_exists('../../images/produtos/'.$nome)){
$i = 1;
while(file_exists('../../images/produtos/'.$i."_".$nome))
{
$i++;
}
$nome = $i."_".$nome;
}
and then i need to put the variable $nome into a mysql_query, just like this
mysql_query("INSERT INTO produtos (nome) VALUES ('$nome)");
but it doesnt show the 'edited' variable.
As noted in the answer to the following Stackoverflow post by #PhpXp, you could try to define the variable before the while loop. See below:
$report = "";
while ($row = mysql_fetch_array($result)) {
$report += "a"."b".$row["prevDOCid"]+1;
}
echo $report;

PHP Reference for MySQL query

My function:
function sql_query($s, $x) {
$query = mysql_query($s);
global $mysql;
while($mysql = mysql_fetch_array($query)) {
return;
}
}
Now it's work only with $mysql variable:
echo $mysql['username'];
How to make it works only with:
sql_query("select * from users where id = '1' limit 1", "varname");
$varname['username'];
I want to set a SQL Query and Variable name in function, like:
sql_query("sqlquery", "variable");
echo $variable['id'];
Thanks for reply!
function sql_query($s, &$x) {
global $mysql;
$query = mysql_query($s);
$result = mysql_fetch_array($query);
foreach($result as $key => $value) {
$x[$key] = $value;
}
}
This should assign each variable that is returned by the query to a key in array $x (assuming it is an array). Notice that I am passing $x by reference instead of by value, eliminating the need to return anything.

Foreach loop with MySQL query

I have an array.
$select_crm=mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
$select_array=array($select_crm);
if(mysql_num_rows($select_crm)>0)
{
foreach($select_array as $v)
{
$fetch_crm=mysql_fetch_array($v);
echo $fetch_crm['party_name'];
echo $fetch_crm['partyid'];``
}
}
But it can't works properly. $select_crm have two rows but it print only one.
$select_array is an array with just one element: the query result resource. Therefore the foreach loop will only ever run once, printing only the first item.
Your loop should look like every single tutorial out there:
while($fetch_crm = mysql_fetch_assoc($v)) { ... }
Note fetch_assoc, not fetch_array. It is pointless to call fetch_array if you don't intend to use the numeric indices.
As far as I'm concerned, you have to do this:
$select_crm=mysql_query("select * from party_details where subcaseid='{$under_row['partyid']}'");
if(mysql_num_rows($select_crm)>0)
{
while ($select_array = mysql_fetch_assoc($select_crm))
{
echo $select_array['party_name'];
echo $select_array['partyid'];
}
}
$select_crm="select * from party_details where subcaseid='{$under_row[partyid]}'";
$crm = mysql_query($select_crm) or die(mysql_error());
$row_crm = mysql_fetch_assoc($crm);
$totalRows_crm = mysql_num_rows($crm);
if($totalRows_crm > 0) {
do {
/*foreach ($row_crm as $field) {
echo $field;
}*/
echo $row_crm['party_name'];
echo $row_crm['partyid'];
} while ($row_crm = mysql_fetch_assoc($crm));
}
do not use foreach , you should use in this way ,
$select_crm= mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
while($result = $db->fetchByAssoc($select_crm))
{
echo $result ['party_name'];
echo $result ['partyid'];
}
try this:
<?php
$select_crm =
mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
while($select_array = mysql_fetch_assoc($select_crm))
{
echo $select_array['party_name'];
echo $select_array['partyid'];
}
?>
hope it will help.
happy coding!
try this
$result = mysql_query("your select query");
while($row = mysql_fetch_assoc($result)){
echo $row['columnNam1'];
echo $row['ColumnName2'];
}
Use this:
$select_crm=mysql_query("select * from party_details where subcaseid='$under_row[partyid]'");
while($row=mysql_fetch_assoc($select_crm)) {
echo $row['party_name'];
echo $row['party_id'];
}
Your code is iterating over an array which only 2 lines above you explicitly created to have exactly one element ;-)
You have misplaced the use of mysql_fetch_assoc. Check out the manual page and the sample code there for your solution. (and while you are there, notice the big red DEPRECATED notice; read on!).

Categories