2

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Tarık Baysal
  • 41
  • 1
  • 5
  • `$query = mysqli_query($baglanti, "SELECT link FROM diziler"); $link_array = array(); if ( $query){ while ( $row = mysqli_fetch_assoc($query) ){ $link_array[] = $row['link']; } } echo "
    ";print_r($link_array);`
    – Alive to die - Anant Sep 23 '16 at 22:33
  • `$row['link']` is a string. When you put an index after a string, it accesses that character in the string. Why did you write `[1]`? – Barmar Sep 23 '16 at 23:32
  • @MartavisGriffin Why shouldn't he use `foreach`? `PDOStatement` implements `Iterable`, which allows you to use `foreach`. – Barmar Sep 23 '16 at 23:33
  • The first one should have worked, since it doesn't have `[1]` in it. – Barmar Sep 23 '16 at 23:33

1 Answers1

0

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)

Community
  • 1
  • 1
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98