I currently collect data like this :
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['id'].$row['name'].$row['surname'].$row['email'].$row['dob'];
echo "<br />";
}
It outputs all the data in one line, like this
1maxpaynemax#hat.com24/07/1950
I want to build the data into a Array rather so it looks like this :
$fields = array(
'id' => '21890',
'name' => 'nick',
'surname' => 'moppy',
'email' => 'nick#moppy.com',
'dob' => '11-01-1965',
),
You already have your array:
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
var_dump($row);
}
So I'm going to make an assumption here. That is that you only want id, name, surname, email and dob. If you want all the columns returned from the table and in the array, just return the SELECT to what it was.
$query = "SELECT id, name, surname, email, dob FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
// $row is now what your example array looks like
}
So there are 2 differences, first, the specified columns from the table. If you're actually wanting all of the columns returned (back to using * for example), but don't want all of the columns returned in your array, this won't work (but you haven't said either way) but #b0s3 first example will.
Second, the addition of the MYSQL_ASSOC parameter. This tells PHP to return an array with only the column name indicies as opposed to them AND numeric keys which doubles up the number of items in the array.
You should use this way.
$query = "SELECT * FROM applicants";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}
echo "<pre>"; print_r($res); echo "</pre>";
Related
Right now all the columns from the MySQL result are converted to JSON and printed out on the screen. I would like to print only two column $row['name'] and $row['gender'] to the screen. Any ideas guys
include('connect-db.php');
$result = mysql_query("SELECT * FROM patientvaccinedetail")
while($row = mysql_fetch_assoc( $result)) {
print_r(json_encode($row));}
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail")
Here is my solution for getting all from the table and display only two values....
include('connect-db.php');
$result = mysql_query("SELECT * FROM patientvaccinedetail") //getting all the fields
while($row = mysql_fetch_assoc( $result)) {
$data = array('name' => $row['name'] , 'gender' => $row['gender']); //get the two values into one array
print_r(json_encode($data)); //printing the name and gender
}
If you want to get only that two fields here is another query...
include('connect-db.php');
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail") //getting two the fields
while($row = mysql_fetch_assoc( $result)) {
print_r(json_encode($row)); //printing the name and gender
}
If you want all data but you have to print only name & gender then ..Create one separate array & allocate name & gender...
$data_array = array();
while($row = mysql_fetch_assoc( $result)) {
array_push($data_array,array("name"=>$row['name'],"gender"=>$row['gender']);
}
print_r(json_encode($data_array));
include('connect-db.php');
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail")
while($row = mysql_fetch_assoc( $result)) {
print_r(json_encode($row));}
I want to display all the values present in ftext field i want to
display all values into single variable like $spd.
When i print this variable $spd it displays only one (ftext)value.but ftext
contains multiple values.
so please help me how to display all values using $spd variable
Thanks in advance...!
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query))
{
$spd = $row['ftext'];
}
Every time variable updates so it contains only last values. Below approaches resolve it:-
#Either print all values
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query)){
echo $spd = $row['ftext'];
}
or
# or hold ftext in array and then use this
$spd = array();
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query)){
$spd[] = $row['ftext'];
}
foreach($spd as $val){
echo $val;
}
you can use id as index
$spd = array();
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query)){
$spd[$row['id']] = $row['ftext'];
}
foreach($spd as $key =>$value){
echo $key." ".$value;
}
You are getting a single value because you are running a loop on the row that came out in the query. and saving it in the $spd. so only the last ftext is saved as all other are rewritten every time the loop runs.
What you can do is append the data to $spd with a delimiter instead of assigning it.
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row =mysqli_fetch_array($query)){
$spd .= $row['ftext'].",";
}
and echo it.
I am fetching records as follows:
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'] = $row;
}
This returns only the last record in the table. I need to return all records from the sql statement.
change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;
You've override the result each time, so you just get one.
It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:
while($row = mysqli_fetch_array($result))
{
$aResult[] = $row;
}
so that each new row gets appended to your array
Try with following code, currently You are initiating the values to the same array key :
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'][] = $row;
}
for more Detail On Array
$sql = "SELECT name FROM students";
$result = $conn->query($sql);
According to code above the $result is an object which contains the values from database.
suppose there are two names in the database under the column "name" like name1 and name2
now what i want is to convert the object $result into an array which will contain name1 and name2 as array element like
$name_array = array('name1' , 'name2')
how can i do that ??
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// acess the each colum elements using $row["<col name>"]
//this will loop through all the rows
}
} else {
// no rows where fetched
}
That is $row = $result->fetch(); will store the values of a row
in the array $row with key as column names
Answering my own question
got a solution for my problem and realized that it's pretty basic and shouldn't have posted this question. this type of problem should be solved by oneself.
$query = "SELECT name FROM students";
if($query_run = mysql_query($query)) {
$name_array = array();
while($query_row = mysql_fetch_assoc($query_run)) {
array_push($name_array, $query_row['name']);
}
the array_push function will do the trick.
thank you.
I have the following code that fetches a single row:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$row = mysqli_fetch_array($result);
I know this is useful in a while loop, where you can just loop through the results.
But I need to be able to grab specific rows that meet this condition. Something following this pseudo code:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_arrayofrows($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
and so on...
How can I do this?
You can try something like this
$arrayofrows = array();
while($row = mysqli_fetch_array($result))
{
$arrayofrows = $row;
}
You can now have
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
I think the function you are looking for is mysqli_fetch_all as shown below. This avoids the need to create an extra looping structure or a function to do something already provided.
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1;";
$result = mysqli_query($query);
$arrayofrows = mysqli_fetch_all($result);
$arrayofrows[0] //(to get the first row)
$arrayofrows[1] //(2nd row)
It depends on whether you require the entire result set back or not but I think the LIMIT could be used like:
$query = "SELECT *
FROM translations
WHERE iddoc = '$id'
AND submitted = 1 LIMIT 200,200;";
Otherwise as others say you will need to convert to an array (which is what fetch_all does) and then get the element from that array.