/    Sign up×
Community /Pin to ProfileBookmark

mysqli_fetch_assoc() AND mysqli_fetch_array()

Hi,

What is the difference between the 2 ?
What different results would they produce ?
to me they are the same. If the same, then why 2 different ways you can write this ?

[code]
while($rows = mysqli_fetch_assoc($result))
[/code]

[code]
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
[/code]

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 19.2020 — Did you try the manual?

https://php.net/mysqli_fetch_array

https://php.net/mysqli_fetch_assoc

> @developer_web#1620911 why 2 different ways you can write this ?

Because, similar to Perl: [u][TMTOWTDI](https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it)[/u]
Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — ``<i>
</i>INSERT DATA&lt;br&gt;
&lt;form method="POST"&gt;
link&lt;input name=data[]&gt;&lt;br&gt;
anchor&lt;input name=data[]&gt;&lt;br&gt;
url&lt;input name=data[]&gt;&lt;br&gt;
dscrp&lt;input name=data[]&gt;&lt;br&gt;
&lt;input type="reset"&gt;&lt;br&gt;
&lt;button name=insert&gt;send&lt;/button&gt;&lt;/form&gt;
SEARCH&lt;br&gt;
&lt;form method="POST"&gt;
term&lt;input name=term&gt;&lt;br&gt;
col&lt;select name=col&gt;
&lt;option&gt;link
&lt;option&gt;anchor
&lt;option&gt;url
&lt;option&gt;dscrp
&lt;/select&gt;&lt;br&gt;
&lt;input type="reset"&gt;&lt;br&gt;
&lt;button name=search&gt;send&lt;/button&gt;&lt;/form&gt;
&lt;?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$servername = "localhost";
$username = "x-x-x-x-x-x-x-x";
$password = "x-x-x-x-x-x-x-";
$dbname = "x-x-x-x-x-x-x-";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "CREATE TABLE IF NOT EXISTS TestA (
link VARCHAR(255),
url VARCHAR(255),
anchor VARCHAR(255),
dscrp VARCHAR(255))";
$result=mysqli_query($conn, $sql);
$stmt = mysqli_stmt_init($conn);
if (array_key_exists('insert',$_POST)){
$data = $_POST[data];
$sql ="insert into TestA values (?,?,?,?)";
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt, "ssss", $data[0],$data[1],$data[2],$data[3]);
if(!mysqli_stmt_execute($stmt)){echo "exe Failed";}
}
if (array_key_exists('search',$_POST)){
$term=$_POST[term]; $col=$_POST[col];
if (in_array($col,array('link','anchor','url','dscrp'))){
$sql = "select * from TestA where ".$col." = ?";}
else{echo "bad column name";}
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt, "s", $term);
if(!mysqli_stmt_execute($stmt)){echo "exe Failed";}
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)){
foreach ($row as $r){print "$r ";}
print "&lt;br&gt;";
}
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@developer_webauthorJul 20.2020 — @DaveyErwin#1620916

Mmm. Thanks for the sample. Was it an old sample from past or did you go through all that length and write a new code up just for me ? :)

I do not understand this part as the $_POST is not named ...
<i>
</i>if (array_key_exists('insert',$_POST))


Care to explain where php is checking for the key ? Which input of the user ?
Copy linkTweet thisAlerts:
@developer_webauthorJul 20.2020 — @NogDog#1620912

Ok. I checked out your php manual link.

These samples I see:

1.

https://www.php.net/mysqli_fetch_array
<i>
</i>$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
printf ("%s (%s)n", $row["Name"], $row["CountryCode"]);

/* associative and numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ("%s (%s)n", $row[0], $row[1]);

NOTE: No loop.

2.

https://www.php.net/mysqli_fetch_assoc
<i>
</i> /* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)n", $row["Name"], $row["CountryCode"]);
}

NOTE: Yes loop.

The 1st one got no loop.

The 2nd one got a loop.

Now, can we turn things round so 1st one got the loop but not the 2nd one ?

If so, then to me it appears them 2 functions do exactly the same thing:
<i>
</i>mysqli_fetch_assoc($result)


<i>
</i>mysqli_fetch_array($result, MYSQLI_ASSOC);


Q1. Am I right ?

Q2. Now, since we got the twin pairs this way ....
<i>
</i>mysqli_fetch_array($result, MYSQLI_ASSOC);

https://www.php.net/mysqli_fetch_array

And the ....
<i>
</i>mysqli_fetch_array($result, MYSQLI_NUM);

https://www.php.net/mysqli_fetch_array

Then, where is the twin pair of these 2 ?

<i>
</i>mysqli_fetch_assoc($result)

https://www.php.net/mysqli_fetch_assoc

And the ....
<i>
</i>mysqli_fetch_num($result)

https://www.php.net/mysqli_fetch_assoc

NOTE: This last one doesn't exist in the manual.

So, what is the equivalent/substitution to this one for this pair of "mysqli_fetch_".

I mean, we already go the pair for the "mysqli_fetch_array($result,"")":

mysqli_fetch_array($result, MYSQLI_ASSOC);

mysqli_fetch_array($result, MYSQLI_NUM);


Q3. Now do you people understand why it's taken me about 3 yrs for me to finish learning procedural style and mysqli ?

You can do the same thing with one pair. 2 different ways of coding to do the same thing.

And yet again, on another one the twin is missing from the pair.

It's taken me 3 yrs to grasp this and figure out that there is a missing twin because I've gotten used to using these functions or coming across them time and time again in tutorials. Gotten familiar with them due to reptitive typings of these functions when copying from tutorials. Otherwise, I never would have noticed the missing twin.

Also, it's taken me 3yrs to notice that them 2 functions do the same thing.
<i>
</i>mysqli_fetch_assoc($result)


<i>
</i>mysqli_fetch_array($result, MYSQLI_ASSOC);


Some tutorials show to use the 1st one while others show to use the 2nd one. I wasn't aware that 2 functions exist to perform the same action and so I always got confused to why programmers coded differently to do the same thing.

One tutorial showed me the code. i sort of memorise. Then come across another tutorial that shows to use the other function. I then used to get confused thinking the first one was not a valid function from the first tutorial and my typo or the tutorial's typo. This confusion prevented me from learning properly. And this is just one example in php's mess. There are countless others. To many ways to achieve the same thing gets you confused which is which.

Silly tutorials should have made it clear the 2 functions do the same thing then confusion never would have risen. I would have grasped right from the beginning there are 2 functions achieving the same thing. And by now I would have already finished learning the mysqli family.

that's how I been learning things the hard way. I have started hating php. Wait till i finish mysqli then I might or might not jump into pdo. But I'm jumping to the Python boat. I picked php over Pything because php has a broad market. Now, strugglign with it has made me come to the conclusion I made a mistake and should have started with python.

One thing I learnt: Never jump on the old boat. Always go for the new one!

Anyway, don;t forget to answer my 3 questions by labeling your answers:

A1: Blah

A2: Blah blah

A3: Blah blah blah
Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — @developer_web#1620941 said ...

I do not understand this part as the $POST is not named ...

if (array_key_exists('insert',$_POST))

Care to explain where php is checking for the key ? Which input of the user ?

``<i>
</i>&lt;form method="POST"&gt;
&lt;button name=insert&gt;send&lt;/button&gt;
&lt;/form&gt;
&lt;?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
echo var_dump($_POST);
}
?&gt;<i>

</i>
``

this is displayed in browser ...

array(1) { ["insert"]=> string(0) "" }

so 'insert' is a key in $_POST
Copy linkTweet thisAlerts:
@developer_webauthorJul 20.2020 — @DaveyErwin#1620916

Dave, do you mind seeing if you can correct my mistakes on this thread by shortening the code as much as possible ? Thanks!

https://www.webdeveloper.com/d/390667-why-php-fails-to-find-function
Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — @developer_web#1620961 ... said

Dave, do you mind seeing if you can correct my mistakes on this thread by shortening the code as much as possible ? Thanks!

This code is based on the code you have posted

in various threads, it allows you to insert data

into a table then search for matching data in

a particular column and display all rows that

contain that data in the selected column


``<i>
</i>INSERT DATA&lt;br&gt;
&lt;form method="POST"&gt;
link&lt;input name=data[]&gt;&lt;br&gt;
anchor&lt;input name=data[]&gt;&lt;br&gt;
url&lt;input name=data[]&gt;&lt;br&gt;
dscrp&lt;input name=data[]&gt;&lt;br&gt;
&lt;input type="reset"&gt;&lt;br&gt;
&lt;button name=insert&gt;send&lt;/button&gt;&lt;/form&gt;
SEARCH&lt;br&gt;
&lt;form method="POST"&gt;
term&lt;input name=term&gt;&lt;br&gt;
col&lt;select name=col&gt;
&lt;option&gt;link
&lt;option&gt;anchor
&lt;option&gt;url
&lt;option&gt;dscrp
&lt;/select&gt;&lt;br&gt;
&lt;input type="reset"&gt;&lt;br&gt;
&lt;button name=search&gt;send&lt;/button&gt;&lt;/form&gt;
&lt;?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$servername = "localhost";
$username = "x-x-x-x-x-x-x-x";
$password = "x-x-x-x-x-x-x-";
$dbname = "x-x-x-x-x-x-x-";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "CREATE TABLE IF NOT EXISTS TestA (
link VARCHAR(255),
url VARCHAR(255),
anchor VARCHAR(255),
dscrp VARCHAR(255))";
$result=mysqli_query($conn, $sql);
$stmt = mysqli_stmt_init($conn);
if (array_key_exists('insert',$_POST)){
$data = $_POST[data];
$sql ="insert into TestA values (?,?,?,?)";
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt, "ssss", $data[0],$data[1],$data[2],$data[3]);
if(!mysqli_stmt_execute($stmt)){echo "exe Failed";}
}
if (array_key_exists('search',$_POST)){
$term=$_POST[term]; $col=$_POST[col];
if (in_array($col,array('link','anchor','url','dscrp'))){
$sql = "select * from TestA where ".$col." = ?";}
else{echo "bad column name";}
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt, "s", $term);
if(!mysqli_stmt_execute($stmt)){echo "exe Failed";}
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)){
foreach ($row as $r){print "$r ";}
print "&lt;br&gt;";
}
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1620954

Thanks.

I'm still not at intermediate level and so your code seems a bit hard for me. But nevertheless thanks for it. One day it should become handy. ;)
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1620997

Ok dave, let's do a combing operation and try to learn your code bit by bit. Otherwise, I won't know how to make use of your code and you'd be wondering why I never make use of it espacially after you gave everything or nearly everything ona plateu.

I have never seen anything like this before 😀 <i>
</i>&lt;form method="POST"&gt;
link&lt;input name=data[]&gt;&lt;br&gt;
anchor&lt;input name=data[]&gt;&lt;br&gt;
url&lt;input name=data[]&gt;&lt;br&gt;
dscrp&lt;input name=data[]&gt;&lt;br&gt;
&lt;input type="reset"&gt;&lt;br&gt;
&lt;button name=insert&gt;send&lt;/button&gt;&lt;/form&gt;


I always see like this:
<i>
</i>name="something" id="something"


And so this is very new to me:
<i>
</i>&lt;input name=data[]&gt;


What is replacing the data[] here since I see no array values in your code something like this:
<i>
</i>array(first_name,surname);


Where is the data of the data array ?

Aren't arrays like this in procedural style ?
<i>
</i>array(first_name,surname);


And like this in oop:
<i>
</i>array(something=&gt;first_name,something=&gt;surname);


You have keys and values.

I don't see any values here nor keys:
<i>
</i>&lt;input name=data[]&gt;


Best stick with me with procedural. I don't know oop. I will get confused.

I think I should learn this. have a hunch it will be handy on one of my projects on hold:
<i>
</i>&lt;input name=data[]&gt;


Thanks!
Copy linkTweet thisAlerts:
@daveyerwinJul 21.2020 — @developer_web#1621059 said ...

I have never seen anything like this before 😀

<form method="POST">

link<input name=data[]><br>

anchor<input name=data[]><br>

url<input name=data[]><br>

dscrp<input name=data[]><br>

<input type="reset"><br>

<button name=insert>send</button></form>

html forms have not changed since day one

learning html forms is essential
Copy linkTweet thisAlerts:
@daveyerwinJul 21.2020 — @developer_web#1621058 said ...

I'm still not at intermediate level and so your code seems

a bit hard for me. One day it should become handy.

to the best of my understanding

that code is exactly what you are attempting

search a table column for the search term

then display all rows where the chosen column

contains the search term

have I misunderstood your requirements ?
Copy linkTweet thisAlerts:
@daveyerwinJul 21.2020 — @developer_web#1621059 said ...

What is replacing the data[] here since I see no array values in your code something like this:

the array is created automatically by the HTML Form

@developer_web#1621059 said ...

Where is the data of the data array ?

it is right here ...
``<i>
</i>$data = $_POST[data];
$sql ="insert into TestA values (?,?,?,?)";
mysqli_stmt_prepare($stmt,$sql);
mysqli_stmt_bind_param($stmt, "ssss", $data[0],$data[1],$data[2],$data[3]);<i>
</i>
``
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1621074 I meant I have never seen input field's names are named by array values. Lol!
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1621077

No you haven't misunderstood.

Thanks for the code and do keep samples and snippets like this coming.
Copy linkTweet thisAlerts:
@daveyerwinJul 21.2020 — @developer_web#1621082 said ...

I meant I have never seen input field's names are named by array values.

well , they are not

all four inputs are named "data[]"

data[] is a string not an array

there are no arrays in HTML

the square brackets are just a convention in HTML

the HTML Form produces an array named data

data[] without the square brackets

(data can be any string eg columnNames[])

you can see that here ...

$data = $_POST[data];

don't be distracted by the square brackets
Copy linkTweet thisAlerts:
@developer_webauthorAug 03.2020 — @DaveyErwin#1621086

I need to research more on this. As this is still puzzling to me and I don;t know how to proceed:

data[]

**the HTML Form produces an array named data**

What should I google for to get html tutorial on the above (bold text subject) ?

Best to checkout a few tutorials. That way, I'll get the picture and won;t need to drain your energy asking questions which will seem sill at the end.
Copy linkTweet thisAlerts:
@daveyerwinAug 03.2020 — @developer_web#1621674

do a google for ...

html form names with square brackets
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...