Say I have a database which has the following layout:
Fields: |Business_Name| |Business_ID|
Data : |business_1A | |ABC_1 |
I want to query the database and retrieve the business name and business ID at the same time and then encode this result to JSON for further usage.
How do I go about doing this?
Here's some code as requested:
$sql = "SELECT Business_Name,Business_ID FROM biz_table";
$businessArray = array();
$bizResult = mysql_query($sql);
while($row = mysql_fetch_assoc($bizResult)) {
$businessArray[][] = $row['Business_Name']$row['Business_ID'];
}
$result = json_encode($businessArray);
echo $result;
Thanks in advance!
Based on your question - your question was "how do i do that?" without really giving an example of the output you want :
$sql = "SELECT Business_Name,Business_ID FROM biz_table";
$businessArray = array();
$bizResult = mysql_query($sql);
while($row = mysql_fetch_assoc($bizResult)) {
$businessArray[$row['Business_Name']] = $row['Business_ID'];
}
$result = json_encode($businessArray);
echo $result;
$result will be
{"business_1A" : "ABC_1" }
Is that what you want ?
Related
I have few data in mysql(link,description,date,del(delete status))
now Im taking this data from mysql with my code. But I wanna show my data's like this photo
as you can see all data's looks clear and separate.
Im using this sql code for take these data's
$sql = "SELECT `vLink` ,`vDescription`, `vDate` FROM `video_table`
But when I use my code output looks like
https://www.youtube.com/watch?v=D8f9IiUXA4EMYSQL | How to auto date time with MYSQL?2021-01-17 02:32:55
now I can use 3 sql code(for link,description,date) to take my all data's and I can put list each by each but Im wondering have we easy way?
thats my code
$sql = "SELECT `vLink` ,`vDescription`, `vDate` FROM `video_table` WHERE `vDel` = 0 ";
$result = mysqli_query($conn,$sql);
$Vdatas = array();
if (mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result)) {
$Vdatas[] = $row;
}
}
//print_r($Vdatas);
foreach ($Vdatas[0] as $data) {
echo $data;
}
I made a script to display data from online SQL database into JSON format.
The problem is, I don't have the format i was looking for, I get 2 [ more while i wanted only one:
A part of my script:
$sql = "select pseudo, dixsec from user;";
$result = mysqli_query($conn,$sql);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
$arrray = array("server_response" => array($rows));
print json_encode($arrray);
What i get (You can see here that i have 2 "["):
Json i get
How can i solve it and get only one "[" ?
$rows is already an array.
Try with: $arrray = array("server_response" => $rows);
I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}
I would want to create an array that will hold records retrieved from a database using a query of SELECT statement.
The records to be retrieved have multiple fields such as lastname, firstname, mi and 20 more fields. What would be the best approach on coding this function?
alright i have followed what prisoner have given below.. the next question is how do i search through this kind of array using queries? for example i want to search for a username..
<?php
// run query
$query = mysql_query("SELECT * FROM table");
// set array
$array = array();
// look through query
while($row = mysql_fetch_assoc($query)){
// add each row returned into an array
$array[] = $row;
// OR just echo the data:
echo $row['username']; // etc
}
// debug:
print_r($array); // show all array data
echo $array[0]['username']; // print the first rows username
You shouldn't search through that array, but use database capabilities for this
Suppose you're passing username through GET form:
if (isset($_GET['search'])) {
$search = mysql_real_escape_string($_GET['search']);
$sql = "SELECT * FROM users WHERE username = '$search'";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_assoc($res);
if ($row){
print_r($row); //do whatever you want with found info
}
}
$mysearch="Your Search Name";
$query = mysql_query("SELECT * FROM table");
$c=0;
// set array
$array = array();
// look through query
while($row = mysql_fetch_assoc($query)){
// add each row returned into an array
$array[] = $row;
$c++;
}
for($i=0;$i=$c;$i++)
{
if($array[i]['username']==$mysearch)
{
// name found
}
}
$memberId =$_SESSION['TWILLO']['Id'];
$QueryServer=mysql_query("select * from smtp_server where memberId='".$memberId."'");
$data = array();
while($ser=mysql_fetch_assoc($QueryServer))
{
$data[$ser['Id']] =array('ServerName','ServerPort','Server_limit','email','password','status');
}
I am looking to create an array of data to be pass to another function that is populated from a database query and am not sure how to do this.
$dataArray[0][1];
$qry = mysql_query("SELECT Id, name FROM users");
while($res = mysql_fetch_array($qry)) {
$dataArray[$res['Id']][$res['name']]
}
Thanks in advance.
This would look better
$dataArray = array();
$qry = mysql_query("SELECT Id, name FROM users");
while($res = mysql_fetch_array($qry)) {
$dataArray[$res['Id']] = $res['name'];
}
you can take a look at the PHP manual how to declare and manipulate arrays.
The below code sniper is very handy...
$select=" WRITE YOUR SELECT QUERY ";
$queryResult= mysql_query($select);
//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();
//STORE ALL THE RECORD SETS IN THAT ARRAY
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC))
{
array_push($data_array,$row);
}
mysql_free_result($queryResult);
//TEST TO SEE THE RESULT OF THE ARRAY
echo '<pre>';
print_r($data_array);
echo '</pre>';
Thanks