I need help with php basic syntax, I'm really new to the language. I'm trying to fetch data from MySQL, as you can see, 1 table 6 columns and 2 rows.
+------+--------+--------+--------+-------+------+
| first| second | third | fourth | fifth | sixth|
+------+--------+--------+--------+-------+------+
| A1 | B1 | C1 | D1 | E1 | F1 |
| A2 | B2 | C2 | D2 | E2 | F2 |
+------+--------+--------+--------+-------+------+
my php file code
<?php
define('HOST','irrelevant');
define('USERNAME','irrelevant');
define('PASSWORD','irrelevant');
define('DB','irrelevant');
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB) or die('Unable to connect');
$sql = "select * from orders";
$res = mysqli_query($con,$sql);
//while($row = mysqli_fetch_array($res)){
//array_push($result,array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5]));
//}
mysqli_close($con);
?>
All I need here is this output which is 1 array with a count(array)=2 :P
{A1
B1
C1
D1
E1
F1 , A2
B2
C2
D2
E2
F2}
without the <'br'> command between them all it should look like this
{A1 B1 C1 D1 E1 F1 , A2 B2 C2 D2 E2 F2}
in other words, I need it to turn into an array that has just 2 strings in it (the number of rows that currently exist in the table).
This is probably very easy to someone who knows php good, if id'e need to implement this in java it would take me a minute but, damn this php and all these dollars!
thanks in advance
-
Is the code you already have giving any errors?blazerunner44– blazerunner442015年09月08日 22:08:33 +00:00Commented Sep 8, 2015 at 22:08
-
the php code is just fetching the values from the database which I also added as a comment becuase I dont know if its even smart to fetch them straight away to an array.iguana– iguana2015年09月08日 22:12:55 +00:00Commented Sep 8, 2015 at 22:12
3 Answers 3
Fetching the data into an array (as you currently are doing it) isn't a problem, but it seems like you're adding each value into it's own element in the array, instead of the whole row into one element.
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result, $row[0]." ".$row[1]." ".$row[2]." ".$row[3]." ".$row[4]." ".$row[5]);
}
This will make $result[0] have the values A1 B1 C1 D1 E1 F1 and $result[1] have the values A2 B2 C2 D2 E2 F2. If you had used a comma as a separator, you would not put it all into $result as one element, but every $row as an element of its own.
1 Comment
You are on the right track, you just needed to append all your results to an array:
<?php
define('HOST','irrelevant');
define('USERNAME','irrelevant');
define('PASSWORD','irrelevant');
define('DB','irrelevant');
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB) or die('Unable to connect');
$sql = "select * from orders";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,array($row[0],$row[1],$row[2],$row[3],$row[4],$row[5]));
}
var_dump($result);
mysqli_close($con);
?>
3 Comments
var_dump(). Some people don't, but I prefer to see what is happening while I debug my code. Could you please reply with the output that was displayed onscreen with my var_dump?You could try this:
$i = 0;
while($row = mysqli_fetch_array($res)){
array_push($result[$i], array($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]));
$i++;
}
Count array:
echo count($result); // will write 2 (rows)
echo count($result[0]); // will write 6 (columns)
If you want only an array with 2 strings, you could try this:
$i = 0;
while($row = mysqli_fetch_array($res)){
array_push($result[$i], ($row[0] . ' ' . $row[1] . ' ' . $row[2] . ' ' . $row[3] . ' ' . $row[4] . ' ' . $row[5]));
$i++;
}
If you don't want spaces, just remove (. ' '). Example:
$i = 0;
while($row = mysqli_fetch_array($res)){
array_push($result[$i], ($row[0] . $row[1] . $row[2] . $row[3] . $row[4] . $row[5]));
$i++;
}