PHP FETCH data from db on row by row not column - php

I need to fetch data from my db. I've done it but when I say fetch array [1], the output is the all second letters in all rows. Here is the code:
include "baglan2.php";
$query = $db->query("SELECT * FROM diziler", PDO::FETCH_ASSOC);
if ( $query->rowCount() ){
foreach( $query as $row ){
echo ($row['link']. "<br />");
}
I tried it with msqli but saw the same result; here is the mysqli code:
$query = mysqli_query($baglanti, "SELECT * FROM diziler");
if ( mysqli_affected_rows($baglanti) ){
while ( $row = mysqli_fetch_assoc($query) ){
print $row['link'][1]."<br />";
}
}
For instance all my data are links. When I run print $row['link'][1], it gives "t" letter from "http:" in all rows. I need to fetch my data by row not column. I have tried every method possible. However I couldn't find any method that worked.
for instance I want to make this codes output "http://**.com" in each element.

I am giving solution based on second attempt:-
$query = mysqli_query($baglanti, "SELECT link FROM diziler");
$link_array = array();
if ( $query){
while ( $row = mysqli_fetch_assoc($query) ){
$link_array[] = $row['link'];
}
}
echo "<pre/>";print_r($link_array); // it have all the links
Reason:- https://eval.in/649070
It's specification is given here:- https://stackoverflow.com/a/17193651/4248328
That is:- $string[int] is syntactic sugar for substr($string, int, 1)

Related

Create PHP array out of table column

I am trying to create a loop that displays each row in a table. I think I need to make an array out of the PK, but I can't figure out how to do that. Here is my code so far:
$conn = dbConnect('read');
$getData = 'SELECT * FROM table';
$allData = $conn->query($getdata);
if (!$allData) {
$error = $conn->error;
} else {
$data = $allData->fetch_assoc();
$rowId = array($data['PK']);
} while ($rowId <= count($rowId)) {
// code to be run for each row
$rowId++;
}
EDIT: Sorry my question is confusing, I'm new to PHP.
Your question is a bit confusing but I think this is what you are trying to do:
$sql = 'SELECT * FROM table';
$query = $conn->query($sql);
while ($row = $query->fetch_assoc()) {
$data[$row['PK']] = $row;
}
That would iterate over each row, creating an array and using the row's value for column PK as an associative array key.
fetch_assoc() (I assume mysqli here now) doesn't fetch all data from a result, but one row after each other. So you don't need to make an array of $row['PK'], but need to loop over the results.
$conn = dbConnect('read');
$getData = 'SELECT * FROM `table`'; // you would need backticks here, if the table really is called "table" (what you shouldn't do...)
$result = $conn->query($getData); // it's not 'allData', it is a result_set. And be carefull about Case! $getData!=$getdata
if (!$result) {
$error = $conn->error;
} else {
$cnt=0;
while($row = $result->fetch_assoc()) {
// code to be run for each row
// you can display $row['PK'] now:
echo $row['PK'];
// or add that value to something else, whatever you need
$cnt = $cnt+$row['PK'];
// or to have a new array with the values of one table-column:
$columnRows[] = $row['PK'];
}
// now you can use the created array
foreach($columnRows as $PK) {
echo $PK;
}
}

output one instance of a data set that could have multiple occurrences

I am having problems sifting through a MySQL query I am trying to strip. I am trying to basically take a column, pull every single entry for that column in a table and then strip it to only show one occurrence even if it happens more than once. So if the value of "bob" shows up more than once I only want it to output once, Everything I have tried either makes it so nothing outputs, or it still outputs ever occurrence of it.
My Code:
$query = mysqli_query($con,"SELECT org FROM accounts");
while ($row = mysqli_fetch_array($query)) {
$holder = array();
$validate = false;
array_push($holder, $row['org']);
for ($i=0; $i <= count($holder); $i++) {
if($holder[$i]!=$row['org']){
$validate = true;
}
}
if ($validate){
?>
<option><?php echo $row['org']?></option>
<?php
}
}
There's a php function called in_array() which lets you search through an array for a specific value without having to write your own loop. So you could replace your entire loop with something like this:
if ( ! in_array( $row['org'], $holder ) )
// your code here
However, your problem is that you're re-initialising the $holder variable to a blank array for every row. You need to store that variable outside the while loop if you wanted to use the above method. For example:
$query = mysqli_query($con,"SELECT org FROM accounts");
$holder = array();
while ($row = mysqli_fetch_array($query)) {
if ( ! in_array( $row['org'], $holder ) ) {
array_push($holder, $row['org']);
?>
<option><?php echo $row['org']?></option>
<?php
}
}
However, you can also select DISTINCT values from a MySQL query, which is probably what you want to do:
$query = mysqli_query($con,"SELECT DISTINCT org FROM accounts");
while ($row = mysqli_fetch_array($query)) {
?>
<option><?php echo $row['org']?></option>
<?php
}

PHP - MySQL gets value of out parameter from a stored procedure

I have called a MySQL stored procedure from PHP using mysqli. This has one out parameter.
$rs = $mysqli->query("CALL addNewUser($name,$age,#id)");
Here, #id is the out parameter. Next, I fire the following query to get the value of the out parameter:
$rs2 = $mysqli->query("SELECT #id");
while($row = $rs->fetch_object()){
echo var_dump($row);
}
The output of var_dump is as follows.
object(stdClass)#5 (1) { ["#id"]=> string(6) "100026" }
So, now I want to retrieve the value of #id, which I am unable to. I tried $row[0]->{#id} but this gave following error:
PHP Fatal error: Cannot use object of type stdClass as array
Or even just do a "SELECT #id AS id" then $row->id will work fine. I always rename select columns to keep the name meaningful when necessary :-)
BTW, you can simply concatenate the call and select #... (with a ; statement delimiter) and the RS will be the returned value. Unfortunately this returns a mutli-resultset and you need to flush the full set otherwise the subsequent queries will stall. See following examples:
$db->multi_query( "CALL addNewUser($name,$age,#id);SELECT #id as id" );
$db->next_result(); // flush the null RS from the call
$rs=$db->store_result(); // get the RS containing the id
echo $rs->fetch_object()->id, "\n";
$rs->free();
Alternatively add the select into the addNewUser and return a RS instead of out param
$rs = $db->query( "CALL addNewUser($name,$age)" );
echo $rs->fetch_object()->id, "\n";
$rs->close();
$db->next_result(); // flush the null RS from the call
The first returns a multiquery (NULL, RS) set and the second a (RS, NULL) set, hence you can use a simple query() call which embeds the first fetch_object(), but you still need to flush the RS stack.
Just $row->{"#id"} would work here. You can't use an stdClass as an array ($row[0]...).
Alternatively, you can just fetch the data as an array using mysqli::fetch_assoc() and access the data using $row['#id'].
Another correct methods its working fine: Cheers!!
$procedureName = 'VALIDATE_USER';
$procedure = "CALL $procedureName('$username','$pwd',#p_userid)";
$results1 = $dbconnection->query($procedure);
$results2 = $dbconnection->query("SELECT #p_userid");
$num_rows = $results2->num_rows;
if ($num_rows > 0) {
while($row = $results2->fetch_object())
{
echo $row->{"#p_userid"};
}
}
Here is the working solution:
enter code $res = $conn->multi_query( "CALL PROCNAME(#x);SELECT #x" );
if( $res ) {
$results = 0;
do {
if ($result = $conn->store_result()) {
printf( "<b>Result #%u</b>:<br/>", ++$results );
while( $row = $result->fetch_row() ) {
foreach( $row as $cell ) echo $cell, " ";
}
$result->close();
if( $conn->more_results() ) echo "<br/>";
}
} while( $conn->next_result() );
}
$conn->close();

why can't I see the full array of my mysql data for php?

I have a question about this ....
$query = 'SELECT *
FROM EXAMPLE';
$result = mysql_query($query);
$row = mysql_fetch_array ($result);
print_r($row);
This has the data of the first row [0] => 1 [id]=> 1... blah, blah but now I'm just wondering, what about the other table data? Is it a php safety to not display the "full array" of all the data inside EXAMPLE table? Just the first row of the data?
This is just out of curiosity.
I know if I want to see specific part of the entire data I can do a while loop.
while ($row = mysql_fetch_array ($result)) {
echo $row['text'].'<br>';
}
mysql_fetch_array only returns one row of data. In your second example you are calling mysql_fetch_array over and over for every row.
To output all your data, do the following:
$query = 'SELECT * FROM EXAMPLE';
$result = mysql_query( $query );
print( '<pre>' ); // Preserve Whitespace/Newlines
while ( $row = mysql_fetch_array( $result ) )
print_r($row);
print( '</pre>' );
If you want to see a specific part of the entire data, do the following:
while ( $row = mysql_fetch_assoc( $result ) )
echo $row['text'].'<br>';

mysql query only returning a single row

the following mysql query is only returning a single row when it should be returning 4.
$query = "SELECT * FROM questions";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// if records are present
if (mysql_num_rows($result) > 0) {
while ( $row = mysql_fetch_object($result) ){
// get question ID and title
$qid = $row->qid;
echo '<div id=ques>';
echo '<h2>'.$row->qtitle .'</h2>';
echo '</div>';
print_r ($row);
the print_r function displays this:
stdClass Object ( [qtitle] => dummy text here [qid] => 1 )
mysql_fetch_*() only pulls a single row at a time. Without seeing the rest of the loop it's impossible to tell if something else is going on down there.
You don't have closing brackets for while-loop and if

Categories