Trying to put all results from oracle table into php variable using implode with this code but is not working.
What is wrong in here?
$sql = oci_parse($ora_con, "SELECT * FROM TABLE");
oci_execute($sql);
while (($row = oci_fetch_row($sql)) != false) {
echo $result = implode(',',(array)$row[0]);
}
And the result is: resul1result2result3result4
Instead of: result1, result2, result3
The problem here is that you're fetching a row at a time, then imploding a single-item, which ends up with no comma (as there's only one). You output that, then loop to the next result - which puts it in the output.
Instead, perhaps try the following, which constructs an array of the results, and then echos the imploded results:
$results = [];
while (($row = oci_fetch_row($sql)) != false) {
$results[] = $row[0];
}
echo implode(',', $results);
An alternative to choult's answer is to fetch all rows in the resultset directly using oci_fetch_all:
$sql = oci_parse($ora_con, 'SELECT * FROM TABLE');
oci_execute($sql);
oci_fetch_all($sql, $result, 0, -1, OCI_NUM);
echo implode(',', $result[0]);
Related
Hey guys i have microsoft sql management studio 18, where I have a database. I'm doing a select statement through php like this:
$conn = OpenCon();
$query = "SELECT id, name, picture, description, numberOfEngines FROM planes";
$result = sqlsrv_query($conn, $query);
if ($result === false) {
$status['status'] = "0";
echo json_encode($status);
}
else{
while($row = sqlsrv_fetch_array($result)) {
$theRows[] = $row;
}
echo json_encode($theRows);
}
CloseCon($conn);
and this is the output:
[{"0":1,"id":1,"1":"114","name":"114","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":2,"id":2,"1":"115","name":"115","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":3,"id":3,"1":"124","name":"124","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1},
{"0":4,"id":4,"1":"125","name":"125","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1}]
How can i remove that leading these duplicates that are showing up twice like the "0":1, or the ariplane.png.
So my output will be like this:
[{"id":1, "name":"114", "picture":"airplane1.png", "description":"Cessna C525", "numberOfEngines":1}]
To return only associative keys in your array, pass SQLSRV_FETCH_ASSOC as the fetchType parameter to sqlsrv_fetch_array:
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
This will give you an array with entries like:
{"id":1,"name":"114","picture":"airplane1.png","description":"Cessna C525","numberOfEngines":1}
If you really want the other numeric keys, keep your code as is and add
unset($row[0])
before
$theRows[] = $row;
Here is the code
$exec = "EXEC RPT_TEST_2resultSet";
$resultSet = \DB::select($exec);
SP is returning 3 result sets. But in php it prints first set of result set only. How to fetch the other 2 set of result sets? Tried the solution suggested by others. Was getting some other errors.
Make a foreach
check example:
foreach ($resultSet as $result) {
echo $result['someDataFromResult'];
}
This will repeat for the amount of data is in the $resultSet array
Found the simple solution for this problem.
$pdo = \DB::connection()->getPdo();
$sql = 'EXEC Test_SP_MultiResultSet';
$stmt = $pdo->query($sql);
do {
$rows = $stmt->fetchAll(\PDO::FETCH_NUM); // Keys will be start from zero , one, two
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); // Column names will be assigned for each value
if ($rows) {
$sheetData[] = $rows;
}
} while ($stmt->nextRowset());
Source Link
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 am doing this select:
$result = $dbh->query("SELECT clicks FROM table WHERE click_date = '".$current_date."'");
$result->execute();
$array = array();
while ($user = $result->fetch(PDO::FETCH_ASSOC)) {
array_push($array, $user['clicks'].",");
}
But this returns:
49572940
But it should be:
4,9,5,7,2,9,4,0
Anybody could help me to fix this problem?
Greetings!
Try this way:
<?php
$sql = "SELECT clicks FROM table WHERE click_date = '$current_date'";
foreach ($dbh->query($sql) as $row) {
$array[] = $row['clicks'];
}
//now echo and use implode
echo implode(", ", $array);
?>
because according to PHP Manual - PDO::query:
PDO::query — Executes an SQL statement, returning a result set as a
PDOStatement object
You should use fetchAll to get all the values and also if you want to execute a prepared statement you have to use prepare instead of query.
$sth = $dbh->prepare("SELECT clicks FROM table WHERE click_date = :current_date");
$sth->bindParam(':current_date', $current_date);
$sth->execute();
$result = $sth->fetchAll();
and after if you want a string with values separate by comma you can use implode as #jason suggested.
$str = implode(",", $result );
I am having a problem. The following code works fine in my local, but on the live server, it's not working properly..I was supposed to get two rows, but on the live server I am getting only 1 result.
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$result = mysql_fetch_object($query);
print json_encode($result);
What could possibly be the error ?...
I can't believe you get two rows with this, to get all rows you have to do like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
print json_encode($result);
}
mysql_fetch_object/array/row always returns only one row and moves the pointer to the next row, if there is no next row it returns false.
Your code is only getting one row. The mysql_fetch_object() function only returns one row. You need to try something like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$json = array();
while ($result = mysql_fetch_object($query))
$json[] = $result;
print json_encode($json);
I think this is because mysql_fetch_object only returns a single row result. You need something like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($row = mysql_fetch_array($query))
{
\\access each result here
}
I can't see how you can have two rows in local
$array = array();
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
$array[] = $result;
}
print json_encode($array);