/    Sign up×
Community /Pin to ProfileBookmark

Why Code Fail ?

Why my code fails ?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 09.2022 — What code?
Copy linkTweet thisAlerts:
@novice2022authorMay 09.2022 — I keep getting message:

You do not have permission to do that.

When I try pasting my code. Why ?
Copy linkTweet thisAlerts:
@novice2022authorMay 09.2022 — @ginerjm#1643903

I keep getting message:

You do not have permission to do that.

When I try pasting my code. Why ?
Copy linkTweet thisAlerts:
@novice2022authorMay 09.2022 — @NogDog

I keep getting message:

You do not have permission to do that.

When I try pasting my code. Why ?
Copy linkTweet thisAlerts:
@ginerjmMay 09.2022 — The forum does not like php tags. Remove the tags and we'll just assume that you are in php mode.
Copy linkTweet thisAlerts:
@Steve_R_JonesmoderatorMay 09.2022 — It's a known glitch in the software. In theory, it'll be fixed during the current upgrade.
Copy linkTweet thisAlerts:
@NogDogMay 09.2022 — If you find it too irritating/problematic, since it's a PHP question, you could post it on the sister forum, https://board.phpbuilder.com/ :)
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — Hi,

QUESTION:

If we use prepared statements then can we not use mysqli_fetch_array() function ?

Following does not work:

<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

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

$input_1 = 'studenbuzz';

$sql = "SELECT email from users WHERE password = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input_1);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_bind_result($stmt,$output_1);
<br/>
<i> </i>while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
<i> </i>{
<i> </i> $id = $row["id"];
<i> </i> echo "$id&lt;br&gt;";
<i> </i>}
<i> </i>mysqli_stmt_close($stmt);
<i> </i>mysqli_close($conn);
<i> </i>
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}


I get error:

**Fatal error: Uncaught TypeError: mysqli_fetch_array(): Argument #1 ($result) must be of type mysqli_result, bool given in C:xampphtdocsWorktestTest.php:19 Stack trace: #0 C:xampphtdocsWorktestTest.php(19): mysqli_fetch_array(true, 1) #1 {main} thrown in C:xampphtdocsWorktestTest.php on line 19**

NOTE:

I need to find two row matches atleast. I only got two rows in the mysql tbl. And no matches apart from the passwords and so using the password column for my tests.

Above codes are in DEV mode. Will remove the mysqli_stmt_error() from Production Modes.

The following works when I use the mysqli_stmt_fetch() function.
<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

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

$input_1 = 'studentbuzz';

$sql = "SELECT email from users WHERE password = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input_1);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$output_1);
<br/>
<i> </i>while(mysqli_stmt_fetch($stmt))
<i> </i>{
<i> </i> echo "$output_1&lt;br&gt;";
<i> </i>}
<i> </i>mysqli_stmt_close($stmt);
<i> </i>mysqli_close($conn);
<i> </i>
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}


But I want to make use of the mysqli_fetch_array() function when using prepared statements and so how to do it ? I need help on my first code.
Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — > @novice2022#1644061 Argument #1 ($result) must be of type mysqli_result, bool given

This usually means that the query failed for some reason, so the call to mysqli_stmt_execute() returned false instead of a mysqli_result object. To debug, try adding something like this (for now) after that call to find out what MySQL didn't like:
[code=php]
$result = mysqli_stmt_bind_result($stmt,$output_1);
if($result === false) {
die("<pre>".$stmt->error."</pre>");
}
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 16.2022 — > @novice2022#1644061 The following works when I use the mysqli_stmt_fetch() function.

Which is what you use if you choose to use bound params.

If you want to be able to use the mysqli_fetch_array() approach, you need to first create the mysqli_result object via [mysql_stmt_get_result()](https://www.php.net/manual/en/mysqli-stmt.get-result.php), and then pass that result to the call to mysqli_fetch_array(). See example at https://www.php.net/manual/en/mysqli-stmt.get-result.php

(I'll qualify all of that with an "I think", as I've only used PDO for the past several years. ;) )
Copy linkTweet thisAlerts:
@ginerjmMay 16.2022 — This statement

"If we use prepared statements then can we not use mysqli_fetch_array() function ?"

is not true. Prepared statements are the recommended way to go.

The problem is (and I agree with Nogdog) the stuff you are apparently forced to do with mysqli. PDO is a much simpler interface for database access.

Look at this: sample code
<i>
</i>if (!$pdo = my_pdo_connect_function($dbname))
{
(handle the error here and exit?)
}
$q = "select stuff from where idcolumn = :idcol order by"; // put query statement into a var
$qst = $pdo-&gt;prepare($q);
$parms = array(
'idcol'=&gt;$id_value
);
if (!$qst-&gt;execute($parms))
{
(handle the failure of the query)
}
else
{
while($row = $qst-&gt;fetch())
{
(output your query results?)
}
}
Copy linkTweet thisAlerts:
@novice2022authorMay 16.2022 — @NogDog#1644064

Thanks.

Following are working as I tested them experimenting. If you don't like any then let me know why so I remove them from my notes.

1.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'fat'; //keyword.

$sql = "SELECT id,domain from users 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_BOTH); //Same as: $row=mysqli_fetch_array($result);
$id = $row['0'];
$email = $row['domain'];

echo "$id&lt;br&gt;";
echo "$domain&lt;br&gt;";

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


2.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'fat'; //keyword.

$sql = "SELECT id,domain from users 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); //Same as: $row=mysqli_fetch_array($result,MYSQLI_BOTH);
$id = $row['0'];
$domain = $row['domain'];

echo "$id&lt;br&gt;";
echo "$domain&lt;br&gt;";

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


3.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'fat'; //keyword.

$sql = "SELECT id,domain from users 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_ASSOC); //Same as: $row=mysqli_fetch_assoc(%result);
$id = $row['id'];
$domain = $row['domain'];

echo "$id&lt;br&gt;";
echo "$domain&lt;br&gt;";

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


4.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'fat'; //keyword.

$sql = "SELECT id,domain from users WHERE password = ?";

$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_assoc($result); //Same as: $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$id = $row['id'];
$domain = $row['domain'];

echo "$id&lt;br&gt;";
echo "$domain&lt;br&gt;";

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


5.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'fat'; //keyword.

$sql = "SELECT id,email from users 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'];
$email = $row['3'];

echo "$id&lt;br&gt;";
echo "$email&lt;br&gt;";

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


6.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$input_1 = 'fat'; //keyword
$input_2 = 'heavy weight'; //keyphrase

$sql = "SELECT id,email from users WHERE keyword = ? OR keyphrase = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input_1,$input_2);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

<i> </i>while($row=mysqli_fetch_array($result,MYSQLI_BOTH))
<i> </i>{
<i> </i> $id = $row['0'];
<i> </i> $email = $row['email'];
<i> </i>
<i> </i> echo "$id&lt;br&gt;";
<i> </i> echo "$email&lt;br&gt;";
<i> </i>}
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}

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


7.
<i>
</i>
$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$inputs = array('gulp','ebrute');

$sql = "SELECT id,email from users WHERE username = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input);

<i> </i>foreach($inputs AS $input)
<i> </i>{
<i> </i> mysqli_stmt_execute($stmt);
<i> </i> $result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> while($row=mysqli_fetch_array($result,MYSQLI_BOTH))
<i> </i> {
<i> </i> $id = $row['0'];
<i> </i> $email = $row['email'];
<i> </i>
<i> </i> echo "$id&lt;br&gt;";
<i> </i> echo "$email&lt;br&gt;";
<i> </i> }
<i> </i>}
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}

mysqli_stmt_close($stmt);
mysqli_close($conn);
?&gt;


8.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$inputs = array('gulp','ebrute');

$sql = "SELECT id,email from users WHERE username = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input);

<i> </i>foreach($inputs AS $input)
<i> </i>{
<i> </i> mysqli_stmt_execute($stmt);
<i> </i> $result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> while($row=mysqli_fetch_array($result,MYSQLI_NUM)) //Do not do: while($row=mysqli_fetch_array($result,MYSQLI_BOTH)) as it outputs each result twice.
<i> </i> {
<i> </i> foreach($row AS $r)
<i> </i> {
<i> </i> echo "$r&lt;br&gt;";
<i> </i> }
<i> </i> }
<i> </i>}
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}

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


9.
<i>
</i>$conn = mysqli_connect("localhost","root","","gulf"); //mysqli_connect("server","user","password","db");

$inputs = array('gulp','ebrute');

$sql = "SELECT id,email from users WHERE username = ?";

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$sql))
{
mysqli_stmt_bind_param($stmt,"s",$input);

<i> </i>foreach($inputs AS $input)
<i> </i>{
<i> </i> mysqli_stmt_execute($stmt);
<i> </i> $result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> $row=mysqli_fetch_array($result,MYSQLI_NUM); //Do not do: while($row=mysqli_fetch_array($result,MYSQLI_BOTH)) as it outputs each result twice.
<i> </i> foreach($row AS $r)
<i> </i> {
<i> </i> echo "$r&lt;br&gt;";
<i> </i> }
<i> </i>}
}
else
{
echo 'Mysqli Error: ' .mysqli_stmt_error();
echo '&lt;br&gt;';
echo 'Mysqli Error No: ' .mysqli_stmt_errno();
}

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

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.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,
)...