I have this code...
$results_query = "SELECT * FROM profile WHERE string LIKE '%" . $search_deliminator . "%'";
$results_result = $database->query($results_query);
$result = mysql_fetch_array($results_result);
What I'm trying to do is pull all the rows in which the string column contains my search deliminator. Further, I would like to get the values from each column of the rows.
How would I pull the multidimensional array I need containing each row, and each value of each column within each row?
EDIT:
I'm looking to do something to this effect...
while ($row = mysql_fetch_assoc($results_result)) {
$result[] = $row;
}
Then echo each column like this...
foreach ($result as $row) {
echo $row["0"];
}
To get a flattened array of the result set, try this:
$result = array();
while ($row = mysql_fetch_assoc($results_result)) {
$result[$row['id']] = $row;
}
echo json_encode($result);
Also, you should look into either MySQLi or PDO to replace the (now deprecated) MySQL extension.
If you use PDO, you can use the fetchAll() method:
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
With mysql or mysqli, you use a loop:
$rows = array();
while ($row = $stmt->fetch_assoc()) {
$rows[] = $row;
}
try this
$i=0;
while ($row = mysql_fetch_assoc($results_result)) {
$result[$i]["column1"] = $row["column1"];
$result[$i]["column2"] = $row["column2"];
$i++;
}
To display the output use:
foreach ($result as $row) {
echo $row["column1"];
echo $row["column2"];
}
Related
I have database query which returns an array of string values. I want them to be comma separated and appended to another string.
$sql1 = "select location.name as name from location,destination where destination.name='".$destination."' AND location.destination_id=destination.destination_id";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
$insert = "";
while($locations = $result1->fetch_assoc()) {
foreach ($locations as $location) {
$insert .= $location['name'] . ",";
}
}
$insert = rtrim($insert, ",");
}
$sql2 = "select destination.destination_description as description from destination where destination.name='".$destination."'";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row = $result2->fetch_assoc()) {
$response->speech = $destination." is the ".$row["description"].". ".$insert . "are some of the places you can try.";
echo json_encode($response);
}
}
This $insert array doesn't give the expected result. It gives just "N".
You can remove the line foreach ($locations as $location)
Your while already fetch all rows to locations so you just need to append your locations[‘name’]. Your foreach has retrieved each character in the string $locations
I think you should look at explode function that might help you Explode function in php
If i understand your problem. You need to use implode() function of php.Try this:-
if ($result1->num_rows > 0) {
$ImplodeArray =array();
while($locations = $result1->fetch_assoc()) {
foreach ($locations as $location) {
$ImplodeArray[] = $location['name'];
}
}
$insert = implode(',',$ImplodeArray);
echo $insert;
}
Hope it helps!
i want to convert my array into string i mean i want to echo only array value
i use this code
$query = "SELECT `message` FROM `appstickers`";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row;
}
//echo $string;
echo json_encode($jsonArray);
i get this output
[{"message":"welcome to team with us"},{"message":"this is for light dispatch"}]
but i want this output
[welcome to team with us,this is for light dispatch"]
so please give me proper solution with example
So why are you using json_encode() ?
use implode() instead:
// ... previous code
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row;
}
echo '[' . implode(',', $jsonArray) . ']';
I'm going on a limb here, and assuming your required array is not what you actually want, but you do want this:
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row['messsage'];
}
echo json_encode($jsonArray);
which will result in
["welcome to team with us", "this is for light dispatch"]
<?php
echo implode(" ",$jsonArray);
?>
Just you need to use implode() function.
$query = "SELECT `message` FROM `appstickers`";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$jsonArray = array();
while($row = $result->fetch_assoc()) {
$jsonArray[]= $row;
}
//echo $string;
$ans = implode(",",$jsonArray);
echo $ans;
The code is supposed to get a row at each loop and build td elements for each data piece. However, it only gets some rows while missing others even though all rows were selected:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>{$val}</td>";
}
}
mysql_fetch_assoc returns an associative array. To display each returned row, do something like this (taken directly from the php.net page for mysql_fetch_assoc
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
Try removing the curly braces like this:
echo "<td>$val</td>";
Beside you didn't close the <tr> tag. So you should do this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
foreach ($rows as $val) {
echo "<td>$val</td>";
}
echo"</tr>";
}
Now like Jack Albright said you should consider various columns of your table to display specific data. Upon that the while() is already a look, I think you don't need to add any foreach() inside. SO your final code should look like this:
$query = "select * from category";
$ret = mysql_query($query, $conn);
while ($rows = mysql_fetch_assoc($ret)) {
echo "<tr>";
echo "<td>$rows['columnName']</td>";
echo "</tr>";
}
$query = "SELECT * FROM main";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) {
$name= $row["name"];
$image = $row["image"];
}
}
then somewhere in my code I print it out using like echo $name; but I got only one result, which is the last row. I know I have to do foreach but what variable should I put?
foreach($result as $results) ? like this?
On every iteration, you reassign the values to $name and $image, causing it to show only the last value when it leaves the loop.
You can either echo them right away in the loop, or populate an array or an object with them, so they will be available later.
Example:
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = array('name'=>$row["name"], 'image'=>$row["image"]); // push into the array
}
var_dump($data); // it's all here now
And to echo the data later, one of the ways is foreach:
foreach($data as $row) {
echo "Name: ", $row['name'], "; Image: ", $row['image];
}
$query = "SELECT * FROM main";
$result = $db->query($query);
$row = $result->fetch_assoc();
foreach ($row as $rows) {
echo $rows['name'] ." ". $rows['image'];
}
i noticed that there are a lot of topics like this in stackoverflow, but the ones that i read are with just 1 variable, and i have two and i'm a little confuse.
so this is my code,
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
while($row = mysql_fetch_array($query)){
echo "'".$row['provider_id']."':'".$row['company_name']."',";
}
I am using this to generate javascript code for Jeditable.
I need to take out the last comma, so I can wrap up with the rest of the code...
I like to use an array:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(",",$out);
It looks like you're trying to output some kind of JSON though, so perhaps you could use:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[$row['provider_id']] = $row['company_name'];
}
echo json_encode($out);
So i usually just stack things like this into an array and then implode:
$parts = array();
while($row = mysql_fetch_array($query)){
$parts[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(',', $parts);
That said it looks like youre outputting the body of a JSON string. If so you shouldnt manually build it you should use json_encode instead.
$data = array();
while($row = mysql_fetch_array($query)){
$data[$row['provider_id']] = $row['company_name'];
}
echo json_encode($data);
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = "";
while($row = mysql_fetch_array($query)){
$result .= "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = rtrim($result, ',');
OR
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = array;
while($row = mysql_fetch_array($query)){
$result[] = "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = implode(',' $result);
OR
get count of records (mysql_num_rows($query)) and when you hit last row just do what you want