/    Sign up×
Community /Pin to ProfileBookmark

Error: Warning: Undefined array key 2 in …

Hi,

I can’t get my head over these errors …

**ATTEMPT 1:**

[code]

$conn = mysqli_connect(“localhost”,”root”,””,”gulf”); //mysqli_connect(“server”,”user”,”password”,”db”);

$input_1 = ‘where’; //keyword.

$sql = “SELECT id,domain from links WHERE keyword = ?”;

$stmt = mysqli_stmt_init($conn);

mysqli_stmt_prepare($stmt,$sql);

mysqli_stmt_bind_param($stmt,”s”,$input_1);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$row=mysqli_fetch_array($result,MYSQLI_NUM);
$id = $row[‘0’]; //id
$domain = $row[‘2’]; //domain

echo “$id<br>”;
echo “$domain<br>”;

mysqli_stmt_close($stmt);
mysqli_close($conn);
[/code]

**Warning: Undefined array key 2 in C:xampphtdocsWorkgulfTest.php on line 20**

**ATTEMPT 2:**

[code]

$conn = mysqli_connect(“localhost”,”root”,””,”gulf”); //mysqli_connect(“server”,”user”,”password”,”db”);

$input_1 = ‘where’; //keyword.

$sql = “SELECT id,domain from links WHERE keyword = ?”;

$stmt = mysqli_stmt_init($conn);

mysqli_stmt_prepare($stmt,$sql);

mysqli_stmt_bind_param($stmt,”s”,$input_1);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

while($row=mysqli_fetch_array($result,MYSQLI_NUM))
{
$id = $row[‘0’]; //id
$domain = $row[‘2’]; //domain
}

echo “$id<br>”;
echo “$domain<br>”;

mysqli_stmt_close($stmt);
mysqli_close($conn);

[/code]

Warning: Undefined array key 2 in C:xampphtdocsWorkgulfTest.php on line 20

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — > @novice2022#1644078 Warning: Undefined array key 2 in C:xampphtdocsWorkgulfTest.php on line 20

Don't quote the numeric array keys. They are integers in this case, not strings (i.e.: $row[1], not $row['1']). That being said, I'd personally prefer to use mysqli_fetch_assoc() and use the column names as the keys, making it much easier when maintaining your code to know what each element is (e.g. $row['id'] instead of $row[0]).
Copy linkTweet thisAlerts:
@novice2022authorMay 31.2022 — @NogDog

You may close this thread.
Copy linkTweet thisAlerts:
@Steve_R_JonesmoderatorMay 31.2022 — {"locked":true}
×

Success!

Help @novice2022 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.26,
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,
)...