how form array in do-while? - php

I try to form array in the do-while:
$result = mysql_query("SELECT * FROM data WHERE company='$companyID'", $db);
if (mysql_num_rows($result) > 0)
{
$resultData = mysql_fetch_array($result);
do
{
$json[] = array('product' => $resultData['product'], 'title' => $resultData['title']);
}
while($resultData = mysql_fetch_array($result));
echo json_encode($json);
}
In exit all datais empty(error):
"[{"product":"","title":""},{"product":"","title":""},{"product":"","title":""},{"product":"","title":""}]"

does it actually give you an error?
try printing out your output to see if you actually fetch anything from mysql
$result = mysql_query("SELECT * FROM data WHERE company='".$companyID."'", $db);
if (mysql_num_rows($result) > 0)
{
while($resultData = mysql_fetch_array($result))
{
echo '<pre>';
print_r($resultData);
echo '</pre>';
}
}
if you do get output that way just replace the content between {} of while with echo json_encode($json);
hope this helps.

Related

PHP json_encode doesn't print anything

I'm running PHP and MySQL and have the following code:
$data = array();
$result = mysql_query($search_query);
if ($result){
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
if (sizeof($data) > 0) {
//var_dump($data);
echo json_encode($data);
} else {
echo 'empty';
}
}
If my query has no rows I do get empty returned.
But if there's any records I get a Resource has no content in Safari.
But if I uncomment my //var_dump($data); then I do get a nice array of values.
Try this:
// Database connection.
$mysqli = new mysqli('localhost', 'user', 'password', 'db_name');
// Your query.
$search_query = "SELECT * FROM yuor_table";
$data = array();
$result = $mysqli->query($search_query);
if ($result){
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
if (sizeof($data) > 0) {
//var_dump($data);
echo json_encode($data);
} else {
echo 'empty';
}
}
This is very simple solution. I would suggest to use "mysqli".

How to make a list from sql with php while loops

So I have a database table, named todolist, and I want to display the whole to do list in a box. What I have right now is this:
$db = (INFO HERE :));
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
} else {
}
What do I put in the else, for it to display everything on a list? Thanks
try something like below:
<?php
$db = (INFO HERE :));
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
}
else{
?>
<ul>
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<li><?php echo $row["yourColumnName"] ?></li><?php
}?>
</ul><?php
}
?>
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
// To see all data
// print_r($row);
// to print single column value
//echo $row['id];
}
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
//Do something here with $row array , for example print_r
print_r($row);
}

How to get db results into view

I have some php that gets data from the database, and I just want to get it into my view. I had this code from earlier where I just wanted 1 row, however now I want to get all the data into the view.
Do I have to make a loop and get each row into a string and keep appending to it?
here's the php:
if ($result = $mysqli->query("SELECT * FROM myData")) {
$row_cnt = $result->num_rows;
if ($row_cnt > 0) {
$row = $result->fetch_assoc();
$data = $row["data"];
echo $data;
} else {
echo "no data";
}
/* close result set */
$result->close();
}
Something like this should do it, unless you need it in the code later. If that is the case store it to an array.
while($row = $result->fetch_assoc()) {
echo $row["data"];
//$array[] = $row["data"];
}
Try this way :
if ($result = $mysqli->query("SELECT * FROM myData")) {
$row_cnt = $result->num_rows;
if ($row_cnt > 0) {
While ($row = $result->fetch_assoc()) {
$data .= $row["data"]."<br>";
}
echo $data;
} else {
echo "no data";
}
/* close result set */
$result->close();
}

How do I extract specific values from a mySQL table using php?

So I've connected to my mySQL database, and I am able to view all of the columns within the table that I'm looking to extract information from. Now I need to be able to read all of the values, from specific fields within "ds_users" which are "password" and "username". I'd like to store these into an array and print them out. Here's the code that I have so far:
$result = mysql_query("SHOW COLUMNS FROM ds_users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
}
also, is there a way to print the results in JSON format?
$result = mysql_query("SELECT username, password FROM ds_users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray['user'] = $row->user;
$dataArray['password'] = $row->password;
}
print_r(json_encode($dataArray));
}
On side note: mysql functions are deprecated, you should choose between mysqli or PDO.
Use json_encode() in php
$arr = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$arr[] = $row;
}
}
print_r(json_encode($arr));
To store in array
$dataArray = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray[] = $row;
}
}
Convert into JSON format using json_encode function
$jsonString = json_encode($dataArray);

How to convert mysqli_fetch_assoc results into json format?

i am trying to convert the result of my query into a json format so i can grap it with jquery in another file. I dont get any errors but its not recognised as json.
$patientquery = mysqli_query($connect, "SELECT * FROM login WHERE assignedTo='$logID'");
$numrows = mysqli_num_rows($patientquery);
if($numrows > 0)
{
while($rows = mysqli_fetch_assoc($patientquery))
{
$dbloginID = $rows['loginID'];
$dbname = $rows['name'];
$result[] = array('patient'=>array('id' => $dbloginID, 'name' => $dbname));
}
}
else
{
$result[] = 'No Patients yet';
}
echo json_encode($result);
please try this:
$patientquery = mysqli_query($connect, "SELECT * FROM login WHERE assignedTo='$logID'");
$numrows = mysqli_num_rows($patientquery);
$result = array();
if($numrows > 0)
{
while($rows = mysqli_fetch_assoc($patientquery))
{
$dbloginID = $rows['loginID'];
$dbname = $rows['name'];
$result['patient'][] = array('id' => $dbloginID, 'name' => $dbname);
}
}
else
{
$result[] = 'No Patients yet';
}
echo json_encode($result);
You should declare $result outside while loop like this
$result = array();

Categories