PHP Fusing two arrays - php

How to connect 2 arrays? I want that $new[$code]=$color, how can I do this? Below is my code:
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$code[] = $row['user_id'];
$color[] = $row['user_color'];
}

Declare the variable outside the while loop
$new = array();
Then inside while loop
$new[$row['user_id']] = $row['user_color'];

In the while loop...
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$new[$row['user_id']] = $row['user_color'];
}
If you need the arrays seperate for some reason you can do it later using array_combine, http://php.net/manual/en/function.array-combine.php.
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$code[] = $row['user_id'];
$color[] = $row['user_color'];
}
...
$new = array_combine($code, $color);

Related

Fetch data in array format in php & mysql

Fetch the data in array format like :
$value = array ("ABC","XYZ","OPQ");
$query = "SELECT * FROM designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
$designation= $row["designation"];
}
Need Result:
$value = array ("ABC","XYZ","OPQ");
Fetch the designation column, and push it into an array with the syntax $array[] = $value.
$designation = [];
$query = "SELECT designation FROM designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
$designation[] = $row["designation"];
}
print_r($designation);
if the "ABC","XYZ","OPQ" are the same field [designation], you can convert $designation to array.
$query = "SELECT * FROM designation_master";
$result = mysqli_query($mysqli, $query);
$designation = array();
while ($row = mysqli_fetch_array($result))
{
$designation[] = $row["designation"];
}

PHP - mysqli_fetch_assoc, 2 results then into an array

$caballoganador = rand(1,9);
$selectganadores3 = array();
$arrayresultados = array();
$selectGanadores ="SELECT `usuario` from `jugadacaballo` WHERE `caballo` =' $caballoganador'";
$selectGanadores1 = mysqli_query($conn, $selectGanadores);
while($selectganadores2 = mysqli_fetch_assoc($selectGanadores1)){
$selectganadores3 = $selectganadores2['usuario'];
array_push($arrayresultados,$selectganadores3);
}
Why the results are not pushing into the array? I'm new with Programming, sorry for my errors.
Try it with $arrayresultados[]
$caballoganador = rand(1,9);
$selectganadores3 = array();
$arrayresultados = array();
$selectGanadores ="SELECT `usuario` from `jugadacaballo` WHERE `caballo` ='$caballoganador'";
$selectGanadores1 = mysqli_query($conn, $selectGanadores);
while($selectganadores2 = mysqli_fetch_assoc($selectGanadores1)){
$arrayresultados[] = $selectganadores2['usuario'];
}

JSON encoding not working

Here is my code to encode data in JSON format, but it doesn't work. The result is []. Where is my mistake?
<?php
$conn = new mysqli('localhost','root','','project');
$data =array();
if(!empty($_GET['masp'])){
$masp =$_GET['masp'];
$sql ="SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn,$sql);
if($result){
while($r = mysqli_fetch_assoc($result)){
$r['masp'] =$data['masp'];
$r['loai'] =$data['loai'];
$r['hangsx']=$data['hangsx'];
$r['tensp']=$data['tensp'];
$r['img']=$data['img'];
$r['gia']=$data['gia'];
$r['nx']=$data['nx'];
}
}
}
print json_encode($data);
?>
You are setting your variables wrong.
In every while cycle you get a new $r variable that you want to add to your $data variable.
$conn = new mysqli('localhost', 'root', '', 'project');
$data = array();
if (!empty($_GET['masp'])) {
$masp = $_GET['masp'];
$sql = "SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn, $sql);
$i = 0;
if ($result) {
while ($r = mysqli_fetch_assoc($result)) {
$data[$i]['masp'] = $r['masp'];
$data[$i]['loai'] = $r['loai'];
$data[$i]['hangsx'] = $r['hangsx']];
$data[$i]['tensp'] = $r['tensp'];
$data[$i]['img'] = $r['img'];
$data[$i]['gia'] = $r['gia'];
$data[$i]['nx'] = $r['nx'];
$i += 1;
}
}
}
print json_encode($data);
You make mistake. You should swap variable data with r inner loop, but probably than also will works unpropely. write in while loop $data [] = $r;

Can I looping this code in PHP using for or while?

<?php
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 1";
$query1 = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 2";
$comments = mysql_query($query);
$comments1 = mysql_query($query1);
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
while($row = mysql_fetch_array($comments1, MYSQL_ASSOC)) {
$bobot1 = $row['bobot'];
$bobot1 = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
?>
I want to make this code can looping until 10. I hope that there aren't many variable, ex: $query, $query1, $query2, ..., $query10, $comments, $comments1, $comments2, ..., $comments10, $bobot, $bobot1, $bobot2, ..., $bobot10. Someone help me, please ...
You're almost there. But I have to mention that you should start using parameterized queries with prepared statements instead of constructing your queries manually.
$id = 1;
while($id <= 10) {
// construct your query
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = $id";
// execute and get results
$comments = mysql_query($query);
// iterate over records in result
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
$bobot = $row['bobot'];
$bobot = htmlspecialchars($row['bobot'],ENT_QUOTES);
}
// increment the id for next cycle through the loop
$id = $id + 1;
}

select query through function.. to fetch data from db

How do I fetch data from db using select query in a function?
Example
function ec_select_query($student, $row = '', $fields=array()) {
$qry = "SELECT * FROM student";
$qry = mysql_query($qry);
while($row = mysql_fetch_assoc($qry)){}
return $row;
}
If you want to return all rows then first save it in an array in while loop then return this array.
function ec_select_query($student,$row='',$fields=array())
{
$qry = "SELECT * FROM student";
$qry = mysql_query($qry);
$result = array();
while($row = mysql_fetch_assoc($qry))
{
$result[] = $row;
}
return $result;
}
Its is running code. Modify it according to your needs
$con = mysql_connect('localhost','root','') or die("Unable to connect to MySQL");
mysql_select_db('demo', $con) or die("Database not found");
function ec_select_query($student)
{
$query = "SELECT * FROM $student";
$result = mysql_query($query);
$row = array();
$getData = array();
while($row = mysql_fetch_array($result))
{
$getData[]=$row;
}
return $getData;
}
$information = ec_select_query('accountplans');
echo "<pre>"; print_r($information); die;
Try it
function select_query($table, $where=array(),$fields=array()){
$select_fields = $table."*";
if(!empty($fields) && is_array($fields)){
$select_fields = implode(",", $fields);
}
$sql = "select ".$select_fields." from ".$table." where 1=1 ";
if(!empty($where) && is_array($where)){
foreach ($where as $key => $value) {
$sql .= " AND ".$value;
}
}
$query = mysql_query($sql);
$result = array();
while($row = mysql_fetch_assoc($result)){
$result[] = $row;
}
return $result;
}
Call Function
$fields = array("id","name","city");
$where = array('name = "abc"','city like "aaa"');
$students = select_query("studendts", $where, $fields);
This code might help you :
function ec_select_query($student,$row='',$fields=array())
{
$q = "SELECT * FROM student";
$q = mysql_query($qry);
while($row = mysql_fetch_array($qry))
{
return $row;
}
}
It is easiest way to produce entire data in array
function db_set_recordset($sql) {
$qry = mysql_query($sql);
$row= array();
while($out = mysql_fetch_assoc($qry)) {
$row[] = $out;
}
return $row;
}
$qry = "SELECT * FROM student";
$result = db_set_recordset($qry);

Categories