Array in PHP does not work - php

I am trying to add array data from mysql. But only the last row details are getting added. I tried array_push also but did not work. Can any one help
$sql="SELECT * FROM server_details";
$result=mysqli_query($dbC, $sql);
while ($row = mysqli_fetch_array($result))
{
$services=array(
$row['server_name'] => array($row['server_add'] => $row['port'])
);
}

By not creating a new array in every iteration of the loop :
$sql = "SELECT * FROM server_details";
$result = mysqli_query($dbC, $sql);
$services = array();
while ($row = mysqli_fetch_array($result)) {
$services[$row['server_name']] = array($row['server_add'] => $row['port']);
}

Perhaps you're looking for this:
$services[ $row['server_name'] ] = array($row['server_add'] => $row['port']);
In the end you'll get in your $services variable an associative array, indexed by server_name column values.
If they're not unique, you should do this instead...
$services[ $row['server_name'] ][] = array($row['server_add'] => $row['port']);
This way you'll still get the same associative array, but its values will be indexed arrays; thus you won't lose any information for records with the same server_name.

Related

merge two columns into an array

I want to merge two of my columns (yanlis_cevaplar, cevap_icerik) into an array and this code here gives me only one column in array when I print it (yanlis_cevaplar).
How do I fix it?
$cevaplar = "SELECT yanlis_cevaplar FROM cevaplar";
$cevap_sonuc = $conn->query($cevaplar) or die(mysqli_error($conn));
$cevap1 = array(); //create empty array
while ($row = $cevap_sonuc->fetch_array()) { //loop to get all results
$cevap1[] = $row; //grab everything and store inside array
}
$cevaplar2 = "SELECT cevap_icerik FROM cevaplar";
$cevap_sonuc2 = $conn->query($cevaplar) or die(mysqli_error($conn));
$cevap2 = array(); //create empty array
while ($row = $cevap_sonuc2->fetch_array()) { //loop to get all results
$cevap2[] = $row; //grab everything and store inside array
}
$tumcevaplar = array_merge($cevap1, $cevap2);
print_r($tumcevaplar);
Instead of making multiple queries, you can just fetch all the columns you want in one single query:
$cevaplar = "SELECT yanlis_cevaplar, cevap_icerik FROM cevaplar";
$cevap_sonuc = $conn->query($cevaplar) or die(mysqli_error($conn));
// Now you can fetch all the rows straight away without any loop.
// The MYSQLI_ASSOC will return each row as an associative array
$result = $cevap_sonuc->fetch_all(MYSQLI_ASSOC);
print_r($result);
This will result in something like this:
Array
(
[0] => Array
(
[yanlis_cevaplar] => some value
[cevap_icerik] => some value
)
[1] => Array
(
[yanlis_cevaplar] => some value
[cevap_icerik] => some value
)
... and so on ..
)
If this isn't what you want, then you need to show us an example.
I also recommend that you go through some basic SQL tutorials. How SELECT works is SQL 101. Here's one of many guides: https://www.tutorialspoint.com/mysql/mysql-select-query.htm

php mysql remove numbered key's from fetched array row

I am trying to do a mysql fetch but it keeps adding numbered and labeled keys to the array. I want it to record only the labeled names and data in the array.
Am I using the wrong mysql call?
global $con,$mysqldb;
$sql="SHOW FIELDS FROM ".$dbtable;
$tr = mysqli_query($con,$sql);
$tl = mysqli_fetch_array($tr);
$tl = mysqli_fetch_array($tr);
$sql="SELECT * FROM ".$mysqldb.".".$dbtable." ORDER BY ".$tl['Field']." LIMIT 3";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$table[$row[1]] = $row;
}
foreach($table as $item => $data){
foreach(array_keys($data) as $pointer => $field) {
echo"pointer=".$pointer."\t";
echo"field=".$field."\n";
echo "data=".$data[$field]."\n";
}
}
reults
pointer=0 field=0 data=3823
pointer=1 field=PID data=3823
pointer=2 field=1 data=AA
pointer=3 field=symbol data=AA
pointer=4 field=2 data=1
pointer=5 field=value data=1
I want to omit 0, 2, & 4 from the array.
Take a look at the PHP.net manual for the mysqli_fetch_array() function.
You'll see there's an option called resulttype that will accept 1 of 3 values - MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH the default.
Using MYSQLI_ASSOC will remove the numbered keys.
Or check mysqli_fetch_assoc().
Thanks to thebluefox for a speedy response.
I replaced the fetch with:
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
And now the results are being recorded as they should.

Easily add data from database to multidimensional array using php

How can I easily add data from MySQL to a multidimensional array in PHP? I am making a search box and I want to store the first name, and the last name in the array.
Why does not this work.
$result = mysqli_query($db, $sql) or die(mysqli_error());
$data = array();
while ($row = mysqli_fetch_array($result)) {
$data[$row['primary']] = $row['firstname'], $row['lastname'];
}
$data[$row['primary']] = array($row['firstname'], $row['lastname']);

Unable to push value into array

There is some stupid mistake with what i am doing, but for some reason i cannot push another key value to an array of arrays...
Here is what i am trying to do:
$result = mysqli_query($conn, $query) or die("Sql Error: " . mysqli_error($conn)); $creatives = array();
while($row = mysqli_fetch_assoc($result)) {
$elements[$row["id"]] = array("min_load_size" => $row["min_load_size"],
"avg_load_size" => $row["avg_load_size"], "max_load_size" => $row["max_load_size"]);
}
After this i am making another call to a differend database and i want to add to each of the arrays another key/value pair. And the code looks like that:
while($row = mysqli_fetch_assoc($result)) {
$arr = $elements[$row["id"]];
$arr["technical_attribute_id"] = $row["technical_attribute_id"];
}
after that i print out my elements array and there is no "technical_attribute_id" in any of the underlying arrays. there is no doubt that the id's match the keys in the regard.
Thanks
You should use references:
$arr = &$elements[$row["id"]];
Without the & that statement will just make a copy of $elements[$row['id']] once it executes the next code line.
Alternatively, you could type the whole thing out:
$elements[$row["id"]]["technical_attribute_id"] = $row["technical_attribute_id"];

Create dynamic associated array from database fields

I got a database with 2 fields: amount and name.
I want to get all the data from the database (40 rows) and create an associated array out of it like this:
$arr = array("amount" => "12", "name" => "John");
How is this done dynamically in PHP? I am stuck.
If you're using the mysql_* function, you can do the following to get one row at a time:
$res = mysql_query("SELECT amount, name FROM mytable");
while ($row = mysql_fetch_assoc($res)) {
var_export($row); // outputs array ( 'amount' => '12', 'name' => 'John', )
}
To get all rows in a single array:
$customers = array();
$res = mysql_query("SELECT amount, name FROM customers");
while ($row = mysql_fetch_assoc($res)) {
$customers[] = $row;
}
Well, if you run your query, e.g.
$result = mysql_query('SELECT amount, name FROM table');
you can loop over the result like that:
$values = array();
while(($row = mysql_fetch_assoc($result))) {
$values[] = $row;//$row will be like array("amount" => "12", "name" => "John")
}
and you will have an array of arrays.
Check out mysql_fetch_assoc if you want to fetch database rows as an associative array (the documentation comes with a good example, too).
$data = $pdo->query('SELECT amount, name FROM ...')->fetchAll(PDO::FETCH_ASSOC);
That's it in PDO. And if you're not using PDO, well, that's your problem then...

Categories