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.

4
  • $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); Commented Sep 23, 2016 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]? Commented Sep 23, 2016 at 23:32
  • @MartavisGriffin Why shouldn't he use foreach? PDOStatement implements Iterable, which allows you to use foreach. Commented Sep 23, 2016 at 23:33
  • The first one should have worked, since it doesn't have [1] in it. Commented Sep 23, 2016 at 23:33

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

god save u bro :)
Thanks @TarıkBaysal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.