Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch only the contents of Database using PHP script

Tags:

php

mysql

I am trying to fetch three columns from the MySql database. I am able to fetch the contents but this is not what I need. I need only the contents as a string array so that I can use them to populate list view in my java code.

The current response from my script is:

{  ["UID"]=>  string(1) "1"  ["UserName"]=>  string(7) "abc@123"  ["Subject"]=>  string(15) "My Android Post"}object(stdClass)#4 (3) {  ["UID"]=>  string(1) "2"  ["UserName"]=>  string(7) "abc@123"  ["Subject"]=>  string(15) "My Android Post"}

I need some thing like as shown below in a String array or a list:

1 abc@123 My Android Post
2 abc@123 My Android Post

How can I get only the value stored in the column in PHP. I am new to PHP scripts, so please help me in solving this issue.

My current PHP script is as shown below:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "iFocusBlogs";



$obtainedUserName = 1;         

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql="SELECT UID, UserName, Subject FROM AndroidTable WHERE Status ='" .$obtainedUserName. "'";
$result=mysqli_query($conn,$sql);

while($obj = $result->fetch_object()){ 
var_dump($obj);

 } 

$conn->close();
?>

Please let me know what change do I need to make in the script to get only the contents of the columns. All suggestions are welcome. Thanks in Advance.

like image 293
keshav kowshik Avatar asked May 29 '15 06:05

keshav kowshik


3 Answers

You can also use mysqli_fetch_row()

/* fetch associative array */
    while ($row = mysqli_fetch_row($result)) {
        printf ("%s (%s)\n", $row[0], $row[1]).PHP_EOL;
    }

Try this Updated

$query="SELECT UID, UserName, Subject FROM AndroidTable WHERE Status ='" .$obtainedUserName. "'";

 if ($result = $conn->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["UID"], $row["UserName"],$row["Subject"]);
    }
}
like image 188
Saty Avatar answered Oct 09 '22 21:10

Saty


You can access object '->' symbol

$tCont="";
while($obj = $result->fetch_object()){ 
//var_dump($obj);
$tCont.='<tr><td>'.$obj->UID.'</td><td>'.$obj->UserName.'</td><td>'.$obj->Subject.'</td></tr>';

 } 
 echo $tCont;
like image 33
Yes Kay Selva Avatar answered Oct 09 '22 20:10

Yes Kay Selva


Use fetch_array instead of fetch_object. You might need some additional modifications to get the array exactly as you want, but at least it's a lot closer.

If you specify MYSQL_NUM as second parameter, you will get an array without keys and just numeric indexes. That array is clean enough to process further.

You can output the array using a for loop...

foreach($array as $item) {
  echo $item . '<br>';
}

or use implode()...

echo implode($array);

or use json_encode()...

echo json_encode($array);

It all depends how exactly you want to show your array.

Functions like print_r and var_dump are useful for inspecting the contents of arrays and other variables, but are unlikely to output that content in a format that is usable by external programs.

like image 22
GolezTrol Avatar answered Oct 09 '22 21:10

GolezTrol