Conflict between mysql_fetch_row and mysql_fetch_array - php

I have a function in PHP that give me some values from DB
$select = mysql_query("SELECT * FROM sfide WHERE accepted = 0");
if($select){
if(mysql_fetch_row($select) != 0){
$sep = "~";
$return = "";
$i = 0;
while($row = mysql_fetch_array($select)){
if($i > 0){
$return = $return.",";
};
$return = $return.$row['id'].$sep.$row['from_'];
$i += 1;
}
return $return;
}else{
return "NO";
}
}else{
return "E, 000, SELECT, ".mysql_error();
}
The problem is that if I do like in the code above it returns me nothing, instead, if I duplicate the select variable it works:
$select = mysql_query("SELECT * FROM sfide WHERE accepted = 0");
$num_select = mysql_query("SELECT * FROM sfide WHERE accepted = 0");
if($select){
if(mysql_fetch_row($num_select) != 0){
$sep = "~";
$return = "";
$i = 0;
while($row = mysql_fetch_array($select)){
if($i > 0){
$return = $return.",";
};
$return = $return.$row['id'].$sep.$row['from_'];
$i += 1;
}
return $return;
}else{
return "NO";
}
}else{
return "E, 000, SELECT, ".mysql_error();
}
Why ?

Instead of
mysql_fetch_row($select) != 0
use
mysql_num_rows($select) != 0
that will return an int for you to compare.

Related

JSON output returns null using PHP ajax

When I view the record from the database, the result is displayed as null.
This is what I tried so far:
<?php
include("db.php");
$fetchqry = "SELECT mid, planid, paid_date, expire_date FROM payment";
$result = mysqli_query($conn, $fetchqry);
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$mid = $row['mid'];
$planid = $row['planid'];
$date1 = $row['expire_date'];
if (strtotime(date("Y/m/d")) < strtotime($date1))
{
$status = "Active";
}
else
{
$status = "Expired";
}
}
echo json_encode($row);
?>
Add the values you read from the DB to an array, then encode that.
$output = [];
$today = strtotime(date("Y/m/d"));
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$mid = $row['mid'];
$planid = $row['planid'];
$date1 = $row['expire_date'];
if($today < strtotime($date1))
{
$status = "Active";
}
else
{
$status = "Expired";
}
$output[] = ['mid' => $mid, 'planid' => $planid, 'status' => $status];
}
echo json_encode($output);

php problems with union select distinct

I submitted these two files:
sample_annotation.txt
sample_synteny.txt
I got on the next page the following error:
304501820180500000018.304501820180500000018<br><br>select distinct org1,org2 from '.304501820180500000018.'_synteny union select distinct org1,org2 from '.304501820180500000018.'_synteny<br><br>
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''.304501820180500000018.'_synteny union select distinct org1,org2 from '.3045018' at line 1
Whole query: select distinct org1,org2 from '.304501820180500000018.'_synteny union select distinct org1,org2 from '.304501820180500000018.'_synteny[]
The mGSV/lib/data.php looks like below and caused the above error:
<?php
include('database.php');
## Get parameters
$org = $_GET['org'];
$data = $_GET['data'];
$session_id = $_GET['session_id'];
$array = array();
if($data == 'annotation') {
$query = "SELECT track_name FROM ".$session_id."_annotation WHERE org_id like '$org' GROUP BY track_name ";
$result = mysql_query($query);
if($result != ''){
while($row = mysql_fetch_assoc($result)){
array_push($array, $row['track_name']);
}
}
//array_push($syn_array, "10_50_20_70");
}
else if ($data == 'synteny') {
$query = "DESC ".$session_id."_synteny";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
if($row['Field'] == 'id'){ continue; }
if($row['Field'] == 'blocks'){ continue; }
if($row['Field'] == 'SYNcolor'){ continue; }
if(! preg_match("/^org[12]+[_]?[start|end]?/", $row['Field'])){
array_push($array, $row['Field']);
}
}
}
else if ($data == 'size'){
echo "$session_id.", $session_id, "<br>";
echo "$org", $org, "<br>";
$query = "select distinct org1,org2 from '.$session_id.'_synteny union select distinct org1,org2 from '.$session_id.'_synteny";
echo $query,"<br>";
$result = mysql_query($query);
echo $results,"<br>";
if ($results) {
while($row = mysql_fetch_assoc($result)){
echo $row['org1'], "<br>";
$q = "select max(output) as max from (select max(greatest(org1_start, org1_end)) as output from ".$session_id."_synteny where org1 like '" . $row['org1'] . "' union select max(greatest(org2_start, org2_end)) as output from ".$session_id."_synteny where org2 like '" . $row['org1'] . "') as t1";
echo $q, "<br>";
$res = mysql_query($q);
if (!$res) {
die('Could not query:' . mysql_error());
}
echo $row['org1'],"<br>";
echo $row['org2'],"<br>";
if (! isset($array[$row['org1']])){
//echo "add<br>";
$array[$row['org1']] = mysql_result($res,0);
}
$q = "select max(output) as max from (select max(greatest(org1_start, org1_end)) as output from ".$session_id."_synteny where org1 like '" . $row['org2'] . "' union select max(greatest(org2_start, org2_end)) as output from ".$session_id."_synteny where org2 like '" . $row['org2'] . "') as t1";
#echo $q, "<br>";
$res = mysql_query($q);
if (!$res) {
die('Could not query:' . mysql_error());
}
if (! isset($array[$row['org2']])){
//echo "add<br>";
$array[$row['org2']] = mysql_result($res,0);
}
}
}
else {
echo 'Invalid query: ' . mysql_error() . "\n";
echo 'Whole query: ' . $query;
}
}
else if ($data == 'order'){
$query = "select distinct org1 from ".$session_id."_synteny union select distinct org2 from ".$session_id."_synteny ";
//echo $query,"<br>";
$result = mysql_query($query);
$default = array();
while($row = mysql_fetch_assoc($result)){
array_push($default, $row['org1']);
}
$array = join('__ORDER__', $default);
}
else if ($data == 'sorder'){
$query = "select distinct org1,org2 from ".$session_id."_synteny union select distinct org1,org2 from ".$session_id."_synteny ";
//echo $query,"<br>";
$result = mysql_query($query);
$default = array();
$assarr = array();
while($row = mysql_fetch_assoc($result)){
if( ! in_array($row['org1'], $default)){
array_push($default, $row['org1']);
$assarr[sizeof($assarr)] = array();
}
if( ! in_array($row['org2'], $default)){
array_push($default, $row['org2']);
$assarr[sizeof($assarr)] = array();
}
$len_query = "select sum(org1_end) - sum(org1_start) + sum(org2_end) - sum(org1_start) as sum from ".$session_id."_synteny where (org1 like '".$row['org1']."' and org2 like '".$row['org2']."') OR (org1 like '".$row['org2']."' and org2 like '".$row['org1']."')";
$q_result = mysql_query($len_query);
$q_row = mysql_fetch_assoc($q_result);
#echo $len_query,"<br>";
#echo $q_row['sum'],"<br>";
$assarr[array_search($row['org1'], $default)][array_search($row['org2'], $default)] = $q_row['sum'];
#$assarr[array_search($row['org1'], $default)][array_search($row['org2'], $default)] = 1;
$assarr[array_search($row['org2'], $default)][array_search($row['org1'], $default)] = $q_row['sum'];
#$assarr[array_search($row['org2'], $default)][array_search($row['org1'], $default)] = 1;
#echo array_search($row['org1'], $default) . "][ ". array_search($row['org2'], $default) . '<br>';
$assarr[array_search($row['org1'], $default)][array_search($row['org1'], $default)] = 0;
#echo array_search($row['org1'], $default) . "][ ". array_search($row['org1'], $default) . '<br>';
$assarr[array_search($row['org2'], $default)][array_search($row['org2'], $default)] = 0;
#echo array_search($row['org2'], $default) . "][ ". array_search($row['org2'], $default) . '<br>';
}
$a = FindOrder($assarr);
$sugg = array();
//echo sizeof($a),'<br>';
foreach($a as $b){
//echo $default[$b],"<br>";
array_push($sugg, $default[$b]);
}
$array = join('__ORDER__', $sugg);
}
function FindOrder($graph){
$r = array();
$last = -1;
while(!isEmpty($graph)){
$start = leastEdges($graph);
$path = longestPath($graph, $start, 0);
$path = completeCycle($graph, $path);
if($path[0] != $last){
array_push($r, $path[0]);
}
for($x = 1; $x < sizeof($path); $x++){
array_push($r, $path[$x]);
}
$last = $path[sizeof($path)-1];
$graph = removePath($graph, $path);
}
return $r;
}
function longestPath($graph, $cur, $len){
$path = array();
array_push($path, $cur);
$longestSubpath = array();
for($x = 0; $x < sizeof($graph); $x++){
if($graph[$cur][$x] != 0){
$subpath = longestPath(removeVertex($graph, $cur), $x, $len + $graph[$cur][$x]);
if(sizeof($subpath) > sizeof($longestSubpath)){
$longestSubpath = $subpath;
}
}
}
foreach($longestSubpath as $x){
array_push($path, $x);
}
return $path;
}
function completeCycle($graph, $path){
$graph = removePath($graph, $path);
$last = $path[sizeof($path)-1];
for($x = 0; $x < sizeof($graph); $x++){
if($graph[$last][$x] != 0){
array_push($path, $x);
return $path;
}
}
return $path;
}
function removePath($graph, $path){
for($x = 0; $x < sizeof($path) - 1; $x++){
$arr = $path[$x]; //***forgot the $ in front of path***
$b = $path[$x+1]; //***forgot the $ in front of path***
$graph[$arr][$b] = 0;
$graph[$b][$arr] = 0;
}
return $graph;
}
function removeVertex($graph, $vtx){
if($vtx < 0 || $vtx >= sizeof($graph)){
return copy($graph);
}
for($x = 0; $x < sizeof($graph); $x++){
$graph[$x][$vtx] = 0;
$graph[$vtx][$x] = 0;
}
return $graph;
}
function numEdges($vtx){
$r = 0;
foreach($vtx as $x){
if($x != 0){
$r++;
}
}
return $r;
}
function leastEdges($graph){
$r = -1;
$min = 2147483647;
for($x = 0; $x < sizeof($graph); $x++){
$e = numEdges($graph[$x]);
if($e != 0 && $e < $min){
$r = $x;
$min = $e;
}
}
return $r;
}
function isEmpty($arr){
$r = true;
foreach($arr as $x){
$r = $r && numEdges($x) == 0;
}
return $r;
}
## Return the JSON object
echo json_encode($array);
?>
I created a docker-compose.yml and the can be run in the following way:
git clone https://github.com/mictadlo/mGSV-docker.git
docker-compose up --build
The service can be accessed via localhost and PhpMyAdmin can be accessed via localhost:8183
What did I miss?
Thank you in advance
Try to run independently both query in your Mysql phpmyadmin
select distinct org1,org2 from '.304342020180200000016.'_synteny
select distinct org1,org2 from '.304342020180200000016.'_synteny
As i see your query seems to be correct since union requires the same numbers of columns and same data type.
And why is your table name in the union query is both the same.

Maximum execution time of 30 seconds with dead lock

it seem like i get stocked in loop ,i didn't know why this is my code-
$db = new mysqli("localhost", "root", "", "gdwal");
$sub = mysqli_query($db, "SELECT *,priority FROM subjects,teacher WHERE teach =techID ");
$stack = array();
$num = mysqli_query($db, "SELECT count(capacity)'h' FROM rooms ");
$table[][] = array();
$x = mysqli_fetch_array($num);
$table[0][0] = 0;
if ($db) {
echo 'connected';
}
/* function seet()
{
global $db;
$a=mysqli_query($db,"SELECT * from options ");
while ($row = mysqli_fetch_array($a)) {$z=$row['choices'];
echo($z);
$e=mysqli_query($db,"SELECT * from timeslot where sname=$z ");
$y=mysqli_fetch_array($e);
$x=$y['sindex'];
echo"//////$x/////+";
$b=mysqli_query($db,"UPDATE options SET `choices`=$x WHERE choices=$z ");
if($db->query($b)===true)
{
echo "lk";
}
}
}
seet(); */
for ($u = 0; $u <= 70; $u++) {
for ($g = 0; $g < 10; $g++) {
$table[$u][$g] = 0;
}
}
choose();
function choose() {
$sum = 0;
$sums = 0;
$best = array();
$prev = 0;
$best[0] = 0;
$best[1] = 0;
$best[2] = 100;
$best[3] = 0;
$best[4] = 0;
global $sub;
global $table;
global $stack;
global $db;
$ee = mysqli_query($db, "SELECT a.cid,c.NOstd,t.priority,c.hours,a.choices ,class,c.teacher_id FROM options a LEFT JOIN subjects c ON c.subject_id = a.cid LEFT JOIN teachers t ON t.id = c.teacher_id ");
while ($row = mysqli_fetch_array($ee)) {
if (!in_array($row['cid'], $stack)) {
$r = $row['cid'];
echo "$r-----";
$new = $r;
if ($prev===$new) {
} else {
$sum = 0;
$prev =$new;
}
$z = mysqli_query($db, "SELECT ind FROM rooms where capacity>='$r'");
while ($j = mysqli_fetch_array($z)) {
$hh = $row['choices'];
$uu = $j['ind'];
if ($table[$hh][$uu] == 0) {
$sum++;
}
}
if ($sum == 0) {
break;
}
$sums = $sum;
if ($best[2] == $sum && $best[3] < $row['priority']) {
echo $row['cid'];
echo '09090909090909';
$best[0] = $row['cid'];
$best[1] = $row['NOstd'];
$best[2] = $sums;
$best[3] = $row['priority'];
$best[4] = $row['teacher_id'];
$best[5] = $row['hours'];
$best[6] = $row['class'];
} else {
if ($best[2] > $sum) {
$best[0] = $row['cid'];
$best[1] = $row['NOstd'];
$best[2] = $sums;
$best[3] = $row['priority'];
$best[4] = $row['teacher_id'];
$best[5] = $row['hours'];
$best[6] = $row['class'];
}
}
}
}
back($best);
}
function back($courses) {
global $stack;
global $db;
global $table;
$y =$courses[0];
$i = 0;
global$x;
$numsub = mysqli_query($db, "SELECT COUNT(DISTINCT cid)'jj' FROM options ");
$nn = mysqli_fetch_array($numsub);
$t = 0;
echo "XXX";
echo "//$y//";
echo "SELECT * FROM options WHERE cid='$y'";
echo "XXX";
$slot = mysqli_query($db, "SELECT * FROM options WHERE cid='$y'");
while ($rows = mysqli_fetch_array($slot)) {
$rom = mysqli_query($db, "SELECT * FROM rooms where capacity>='$courses[1]'");
$j = mysqli_query($db, "SELECT * FROM rooms where capacity>='$courses[1]'");
while ($D = mysqli_fetch_array($rom)) {
if ($courses[5] == 3) {
echo "22";
if ($table[$rows['choices']][$D['ind']] != 0 && $table[($rows['choices'] + 1)][$D['ind']] != 0) {
$z = $table[$rows['choices']][$D['ind']];
$ff = mysqli_query($db, "SELECT teacher FROM subjects where course_id=$z ");
$selec = mysqli_fetch_array($ff);
$b = mysqli_query($db, "SELECT COUNT(`class`)'kk'FROM `subjects` WHERE `class` IN(SELECT `class` FROM subjects WHERE `course_id`=$z ) AND( course_id =$y OR`ssubject`=$y");
$count = mysqli_fetch_array($b);
if ($count['kk'] != 0 || $selec['teacher'] == $courses[4]) {
$t++;
}
}
} else
if ($table[$rows['choices']][$D['ind']] != 0) {
$z = $table[$rows['choices']][$D['ind']];
$ff = mysqli_query($db, "SELECT teacher FROM subjects where course_id=$z ");
$selec = mysqli_fetch_array($ff);
$b = mysqli_query($db, "SELECT COUNT(`class`)'kk'FROM `subjects` WHERE `class` IN(SELECT `class` FROM subjects WHERE `course_id`=$z ) AND( course_id =$y OR`ssubject`=$y");
$count = mysqli_fetch_array($b);
if ($count['kk'] != 0 || $selec['teacher'] == $courses[4]) {
$t++;
if ($t != 0) {
break;
}
}
echo "1";
}
if ($t != 0) {
break;
}
}
while ($colmn = mysqli_fetch_array($j)) {
if ($courses[5] == 3) {
echo "uuuuuu";
$w = $table[$rows['choices']][$colmn['ind']];
$y = mysqli_query($db, "SELECT hours FROM subjects where course_id='$w' ");
$op = mysqli_fetch_array($y);
if ($op['hours'] == 3) {
$i = $table[($rows['choices'] - 1)][$colmn['ind']];
$q = mysqli_query($db, "SELECT hours ,course_id , FROM subjects WHERE course_id=$i");
$opi = mysqli_fetch_array($q);
if ($opi['hours'] == 3 && $opi['course_id'] == $w) {
array_push($stack, $courses[0]);
$table[($rows['choices'] + 1)][$colmn['ind']] = $courses[0];
if (count($stack) == $nn['jj']) {
database($table);
break;
}
choose();
$table[($rows['choices']) + 1][$colmn['ind']] = 0;
array_pop($stack);
}
} elseif ($table[$rows['choices']][$colmn['ind']] == 0) {
array_push($stack, $courses[0]);
$table[$rows['choices']][$colmn['ind']] = $courses[0];
$table[($rows['choices'] + 1)][$colmn['ind']] = $courses[0];
if (count($stack) == $nn['jj']) {
database($table);
break;
}
choose();
$table[$rows['choices']][$colmn['ind']] = 0;
$table[($rows['choices'] + 1)][$colmn['ind']] = 0;
array_pop($stack);
}
//end of t
}//end of courses 3
else {
if ($t == 0 && $table[$rows['choices']][$colmn['ind']] == 0) {
array_push($stack, $courses[0]);
$table[$rows['choices']][$colmn['ind']] = $courses[0];
if (count($stack) == $nn['jj']) {
database($table);
break;
}
choose();
$table[$rows['choices']][$colmn['ind']] = 0;
array_pop($stack);
}
}
}
}
}
global $sub;
function database($tab) { // print_r($tab);
global $db;
$g = mysqli_query($db, "SELECT * FROM timeslot");
$r = mysqli_query($db, "SELECT * FROM rooms");
while ($ro = mysqli_fetch_array($g)) {
while ($co = mysqli_fetch_array($r)) {
if ($tab[$ro['sindex']][$co['ind']] === 0) {
} else {
echo 'iam in 2';
$x = $tab[$ro['sindex']][$co['ind']];
$u = mysqli_query($db, "select subject_id,hours,teacher_id,class from subjects WHERE subject_id='$x'");
$cor = mysqli_fetch_array($u);
$na = $cor['subject_id'];
$ho = $cor['hours'];
$tea = $cor['teacher_id'];
$clas = $cor['class'];
$tim = $ro['sindex'];
$km = $co['ind'];
$rmm = mysqli_query($db, "SELECT name FROM rooms where ind=$km");
$rom = mysqli_fetch_array($rmm);
$romm = $rom['name'];
$n = mysqli_query($db, "INSERT INTO ttable(name, teacher,class, timeslot, room, hours) VALUES ($na,$tea,$clas,$tim,$romm,$ho)");
$t = "INSERT INTO ttable(name, teacher, timeslot, room, hours) VALUES ('$na','$tea','$tim','$romm','$ho')";
echo "uuututyttuy";
if ($db->query($t) === true) {
echo "lklklklklklklklkl";
}
}
}
}
}
It seems your "back" function calls "choose" function.
And your "choose" function calls your "back" function.
The loop is complete.
I have marked it with //////// to make it easy to find.
Edit I see now that there is several places in back you call choose. I have only marked one.
It's not to hard to find it with search function of any developer app.
$db = new mysqli("localhost", "root", "", "gdwal");
$sub = mysqli_query($db, "SELECT *,priority FROM subjects,teacher WHERE teach =techID ");
$stack = array();
$num = mysqli_query($db, "SELECT count(capacity)'h' FROM rooms ");
$table[][] = array();
$x = mysqli_fetch_array($num);
$table[0][0] = 0;
if ($db) {
echo 'connected';
}
/* function seet()
{
global $db;
$a=mysqli_query($db,"SELECT * from options ");
while ($row = mysqli_fetch_array($a)) {$z=$row['choices'];
echo($z);
$e=mysqli_query($db,"SELECT * from timeslot where sname=$z ");
$y=mysqli_fetch_array($e);
$x=$y['sindex'];
echo"//////$x/////+";
$b=mysqli_query($db,"UPDATE options SET `choices`=$x WHERE choices=$z ");
if($db->query($b)===true)
{
echo "lk";
}
}
}
seet(); */
for ($u = 0; $u <= 70; $u++) {
for ($g = 0; $g < 10; $g++) {
$table[$u][$g] = 0;
}
}
choose();
function choose() {
$sum = 0;
$sums = 0;
$best = array();
$prev = 0;
$best[0] = 0;
$best[1] = 0;
$best[2] = 100;
$best[3] = 0;
$best[4] = 0;
global $sub;
global $table;
global $stack;
global $db;
$ee = mysqli_query($db, "SELECT a.cid,c.NOstd,t.priority,c.hours,a.choices ,class,c.teacher_id FROM options a LEFT JOIN subjects c ON c.subject_id = a.cid LEFT JOIN teachers t ON t.id = c.teacher_id ");
while ($row = mysqli_fetch_array($ee)) {
if (!in_array($row['cid'], $stack)) {
$r = $row['cid'];
echo "$r-----";
$new = $r;
if ($prev===$new) {
} else {
$sum = 0;
$prev =$new;
}
$z = mysqli_query($db, "SELECT ind FROM rooms where capacity>='$r'");
while ($j = mysqli_fetch_array($z)) {
$hh = $row['choices'];
$uu = $j['ind'];
if ($table[$hh][$uu] == 0) {
$sum++;
}
}
if ($sum == 0) {
break;
}
$sums = $sum;
if ($best[2] == $sum && $best[3] < $row['priority']) {
echo $row['cid'];
echo '09090909090909';
$best[0] = $row['cid'];
$best[1] = $row['NOstd'];
$best[2] = $sums;
$best[3] = $row['priority'];
$best[4] = $row['teacher_id'];
$best[5] = $row['hours'];
$best[6] = $row['class'];
} else {
if ($best[2] > $sum) {
$best[0] = $row['cid'];
$best[1] = $row['NOstd'];
$best[2] = $sums;
$best[3] = $row['priority'];
$best[4] = $row['teacher_id'];
$best[5] = $row['hours'];
$best[6] = $row['class'];
}
}
}
}
/////////////////
/////////////////
/////////////////
back($best);
}
function back($courses) {
global $stack;
global $db;
global $table;
$y =$courses[0];
$i = 0;
global$x;
$numsub = mysqli_query($db, "SELECT COUNT(DISTINCT cid)'jj' FROM options ");
$nn = mysqli_fetch_array($numsub);
$t = 0;
echo "XXX";
echo "//$y//";
echo "SELECT * FROM options WHERE cid='$y'";
echo "XXX";
$slot = mysqli_query($db, "SELECT * FROM options WHERE cid='$y'");
while ($rows = mysqli_fetch_array($slot)) {
$rom = mysqli_query($db, "SELECT * FROM rooms where capacity>='$courses[1]'");
$j = mysqli_query($db, "SELECT * FROM rooms where capacity>='$courses[1]'");
while ($D = mysqli_fetch_array($rom)) {
if ($courses[5] == 3) {
echo "22";
if ($table[$rows['choices']][$D['ind']] != 0 && $table[($rows['choices'] + 1)][$D['ind']] != 0) {
$z = $table[$rows['choices']][$D['ind']];
$ff = mysqli_query($db, "SELECT teacher FROM subjects where course_id=$z ");
$selec = mysqli_fetch_array($ff);
$b = mysqli_query($db, "SELECT COUNT(`class`)'kk'FROM `subjects` WHERE `class` IN(SELECT `class` FROM subjects WHERE `course_id`=$z ) AND( course_id =$y OR`ssubject`=$y");
$count = mysqli_fetch_array($b);
if ($count['kk'] != 0 || $selec['teacher'] == $courses[4]) {
$t++;
}
}
} else
if ($table[$rows['choices']][$D['ind']] != 0) {
$z = $table[$rows['choices']][$D['ind']];
$ff = mysqli_query($db, "SELECT teacher FROM subjects where course_id=$z ");
$selec = mysqli_fetch_array($ff);
$b = mysqli_query($db, "SELECT COUNT(`class`)'kk'FROM `subjects` WHERE `class` IN(SELECT `class` FROM subjects WHERE `course_id`=$z ) AND( course_id =$y OR`ssubject`=$y");
$count = mysqli_fetch_array($b);
if ($count['kk'] != 0 || $selec['teacher'] == $courses[4]) {
$t++;
if ($t != 0) {
break;
}
}
echo "1";
}
if ($t != 0) {
break;
}
}
while ($colmn = mysqli_fetch_array($j)) {
if ($courses[5] == 3) {
echo "uuuuuu";
$w = $table[$rows['choices']][$colmn['ind']];
$y = mysqli_query($db, "SELECT hours FROM subjects where course_id='$w' ");
$op = mysqli_fetch_array($y);
if ($op['hours'] == 3) {
$i = $table[($rows['choices'] - 1)][$colmn['ind']];
$q = mysqli_query($db, "SELECT hours ,course_id , FROM subjects WHERE course_id=$i");
$opi = mysqli_fetch_array($q);
if ($opi['hours'] == 3 && $opi['course_id'] == $w) {
array_push($stack, $courses[0]);
$table[($rows['choices'] + 1)][$colmn['ind']] = $courses[0];
if (count($stack) == $nn['jj']) {
database($table);
break;
}
choose();
$table[($rows['choices']) + 1][$colmn['ind']] = 0;
array_pop($stack);
}
} elseif ($table[$rows['choices']][$colmn['ind']] == 0) {
array_push($stack, $courses[0]);
$table[$rows['choices']][$colmn['ind']] = $courses[0];
$table[($rows['choices'] + 1)][$colmn['ind']] = $courses[0];
if (count($stack) == $nn['jj']) {
database($table);
break;
}
choose();
$table[$rows['choices']][$colmn['ind']] = 0;
$table[($rows['choices'] + 1)][$colmn['ind']] = 0;
array_pop($stack);
}
//end of t
}//end of courses 3
else {
if ($t == 0 && $table[$rows['choices']][$colmn['ind']] == 0) {
array_push($stack, $courses[0]);
$table[$rows['choices']][$colmn['ind']] = $courses[0];
if (count($stack) == $nn['jj']) {
database($table);
break;
}
///////////////////////
///////////////////////
///////////////////////
choose();
$table[$rows['choices']][$colmn['ind']] = 0;
array_pop($stack);
}
}
}
}
}
global $sub;
function database($tab) { // print_r($tab);
global $db;
$g = mysqli_query($db, "SELECT * FROM timeslot");
$r = mysqli_query($db, "SELECT * FROM rooms");
while ($ro = mysqli_fetch_array($g)) {
while ($co = mysqli_fetch_array($r)) {
if ($tab[$ro['sindex']][$co['ind']] === 0) {
} else {
echo 'iam in 2';
$x = $tab[$ro['sindex']][$co['ind']];
$u = mysqli_query($db, "select subject_id,hours,teacher_id,class from subjects WHERE subject_id='$x'");
$cor = mysqli_fetch_array($u);
$na = $cor['subject_id'];
$ho = $cor['hours'];
$tea = $cor['teacher_id'];
$clas = $cor['class'];
$tim = $ro['sindex'];
$km = $co['ind'];
$rmm = mysqli_query($db, "SELECT name FROM rooms where ind=$km");
$rom = mysqli_fetch_array($rmm);
$romm = $rom['name'];
$n = mysqli_query($db, "INSERT INTO ttable(name, teacher,class, timeslot, room, hours) VALUES ($na,$tea,$clas,$tim,$romm,$ho)");
$t = "INSERT INTO ttable(name, teacher, timeslot, room, hours) VALUES ('$na','$tea','$tim','$romm','$ho')";
echo "uuututyttuy";
if ($db->query($t) === true) {
echo "lklklklklklklklkl";
}
}
}
}
}

alert duplicate records in csv upload

my csv upload code is like this want to alert duplicate records which are already exists in database i am getting result with $status variable what should i do to alert every duplicate records what is way to alert every duplicate records
if(isset($_POST["Import"]))
{
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$row = 1;
if (($handle = fopen("$filename", "r")) !== FALSE)
{
while (($data = fgetcsv($handle)) !== FALSE)
{
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if($c!="" && $c+1!="" && $c+2!=""){
$a[] = $data[$c];
}
}
}
$total = count($a);
$m=0;
$k = 1;
for ($c=0; $c < $total; $c++)
{
$address[$m][] = $a[$c] ;
$z = $address[$m][0];
$q = $address[$m][1];
$q .= $address[$m][2];
$q .= $address[$m][3];
$q .= $address[$m][4];
$q .= $address[$m][5];
$q .= $address[$m][6];
$q .= $address[$m][7];
$q .= $address[$m][8];
$q .= $address[$m][9];
if($a[$c]=="")
{
if($k!=1)
{
$selectcon = "SELECT user_id FROM contact
WHERE user_id = '".$z."'";
$selectRes = mysql_query($selectcon);
if($rows = mysql_fetch_array($selectRes))
{
$user_id = $rows['user_id'];
if($user_id == $z)
{
$staus = 1;
}
else
{
$staus = 1;
}
}
$insertParty = "INSERT INTO contact(user_id)values('$z')";
$res = mysql_query($insertParty);
$contact_id = mysql_insert_id();
$insertAddress = "INSERT INTO address(contact_id,address)VALUES($contact_id,'$q')";
$insertAddressRes = mysql_query($insertAddress);
if(!$insertAddressRes)
{
echo "<sctipt>";
echo "sweetAlert('oops','Import Data Fail','error')";
echo "</sctipt>";
}
else
{
echo "<script>";
echo "swal('Sucess!', 'File Imported Sucessfully!','success')";
echo "</script>";
}
$m++;
}
$k++;
}
}
fclose($handle);
}
}
}
?>
You need to make a unique row inside database. You can't do it with PHP.
Add all columns to a single UNIQUE index, the you can't insert duplicate rows in that table.

Why Getting only 1 array instead of many arrays?

I am a completely newbie in programming php I would like to make this code below return many arrays(to flash as3), however I only receive one array.Can anyone please pinpoint what is my mistake here? thanks.
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
echo "returnStr=$data_array";
exit();
}
When you write your exit insight your loop you stop executing your program and you get only one record. You should set the echo and exit after your while loop.
$data_array = "";
$i = 0;
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql)) {
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1) {
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
} else {
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
Those two last line of your should be outside of your loop:
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
If you would name the columns that you want in the SELECT then it's much simpler. Make sure to use MYSQLI_ASSOC in the fetch:
$sql = mysqli_query($conn, "SELECT Username, Fb_id, Access_token, Fb_sig, Char_id FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC))
{
$data_array[] = implode('|', $row);
}
echo "returnStr=" . implode('(||)', $data_array);
exit();

Categories