/    Sign up×
Community /Pin to ProfileBookmark

Why IF Function Error Fails While IF $stmt Works ?

Hi,

Line 70 is:

[code]

if(mysqli_stmt_execute($stmt) === FALSE)

[/code]

Context:

[code]

if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$_SESSION[‘search_column’]);

if(mysqli_stmt_execute($stmt) === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
}

[/code]

I am getting this error:

Fatal error: Uncaught mysqli_sql_exception: No index used in query/prepared statement (null) in C:xampphtdocstestselect_adv.php:70 Stack trace: #0 C:xampphtdocstestselect_adv.php(70): mysqli_stmt_execute(Object(mysqli_stmt)) #1 {main} thrown in C:xampphtdocstestselect_adv.php on line 70

This removes the error:

[code]

if($stmt) === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
}

[/code]

I still want to know why this works:

[code]

if($stmt) === FALSE)
{

[/code]

but this doesn’t work:

[code]

if(mysqli_stmt_execute($stmt) === FALSE)

{

[/code]

Let me know.

Full Code in case you’re wondering just what on earth is going on ….

[code]

<?php
//include(‘error_reporting.php’);
error_reporting(E_ALL);
ini_set(‘error_reporting’,E_ALL);//Same as: error_reporting(E_ALL);
ini_set(‘display_errors’,’1′);
ini_set(‘display_startup_errors’,’1′);
require(‘conn.php’);
echo __LINE__;
?>

<form name = “search” method = “POST” action=””>
<label for=”keywords”>Keywords:*</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Input Keywords” required>
<br>
<label for=”search_column”>Search in … ?</label>
<select name=”search_column” id=”search_column”>
<option value=”page_url”>Page Url</option>
<option value=”link_anchor_text”>Link Anchor Text</option>
<option value=”page_description”>Page Description</option>
<option value=”keyphrases”>Keyphrase</option>
<option value=”keywords”>Keywords</option>
</select>
<br>
<label for=”tos_agreement”>Agree to TOS or not ? *</label>
<select name=”tos_agreement” id=”tos_agreement” required>
<option value=”Yes”>Yes</option>
<option value=”No”>No</option>
</select>
<br>
<button type=”submit”>Submit</button><br>
<button type=”submit” value=”submit”>Submit</button><br>
<input type=”submit” value=”submit”><br>
<button name=submit value=” “>Submit</button><br>
<br>
<input type=”reset”>
<br>
</form>

<?php
session_start();
echo __LINE__;
if($_SERVER[‘REQUEST_METHOD’] === ‘POST’)
{
echo __LINE__;
if(ISSET($_POST[‘submit’]))
{
echo __LINE__;
if(ISSET($_POST[‘search_column’]))
{
$_SESSION[‘search_column’] = $_POST[‘search_column’];
echo $_SESSION[‘search_column’];
echo __LINE__;
}

//Re-write the following 4 lines …
mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”test”);
$conn->set_charset(“utf8mb4”);

//$query = “SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE $_SESSION[‘search_column’] = ?”;
$query = “SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE {$_SESSION[‘search_column’]} = ?”;
//$query = “SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE ” . $_SESSION[‘search_column’] . ” = ?”;

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$_SESSION[‘search_column’]);

if(mysqli_stmt_execute($stmt) === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
}

$result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);

mysqli_stmt_fetch($stmt);

while(mysqli_stmt_fetch($stmt))
{
echo “url”; echo “<br>”;
echo “anchor_text”; echo “<br>”;
echo “description”; echo “<br>”;
echo “keyphrases”; echo “<br>”;
echo “keywords”; echo “<br>”;
echo “|”;
echo “<br>”;
}

mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else
{
echo “1. QUERY failed!”;
}

if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’sssss’,$_POST[‘page_url’],$_POST[‘link_anchor_text’],$_POST[‘page_description’],$_POST[‘keyphrases’],$_POST[‘keywords’]);

if(mysqli_stmt_execute($stmt) === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
}

$result = mysqli_stmt_get_result($stmt);

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$page_url = $row[‘page_url’]; echo $page_url; echo “<br>”;
$link_anchor_text = $row[‘link_anchor_text’]; echo $link_anchor_text; echo “<br>”;
$page_description = $row[‘page_description’]; echo $page_description; echo “<br>”;
$keyphrases = $row[‘keyphrases’]; echo $keyphrases; echo “<br>”;
$keywords = $row[‘keywords’]; echo $keywords; echo “<br>”;
echo “|”;
echo “<br>”;
}

mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else
{
die(“2. QUERY failed!”);
}
echo ‘<pre>’ . print_r($_POST, 1) . ‘</pre>’;
echo $_SESSION[‘search_column’];
}
}
echo __LINE__;

?>

[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJul 17.2020 — so maybe explain why you are using

$_SESSION['search_column'] = $_POST['search_column'];

what is the purpose of this ?

.

.

.

.
``<i>
</i>$query = "SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE
{$_SESSION['search_column']} = ?";
mysqli_stmt_bind_param($stmt,'s',$_SESSION['search_column']);<i>
</i>
``


so you get

FROM links WHERE {$_SESSION['search_column']} = $_SESSION['search_column']

https://www.tutorialspoint.com/sql/sql-where-clause.htm
Copy linkTweet thisAlerts:
@developer_webauthorJul 17.2020 — Ignore my above code. Check this out and let me know if I got any bugs in it or not ....
<i>
</i>&lt;?php
//include('error_reporting.php');
error_reporting(E_ALL);
ini_set('error_reporting',E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
?&gt;

&lt;form name = "search" method = "POST" action=""&gt;
&lt;label for="keywords"&gt;Keywords:*&lt;/label&gt;
&lt;input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required&gt;
&lt;br&gt;
&lt;button type="submit"&gt;Submit&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" value="submit"&gt;Submit&lt;/button&gt;&lt;br&gt;
&lt;input type="submit" value="submit"&gt;&lt;br&gt;
&lt;button name=submit value=" "&gt;Search&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" name="submit" value="submit"&gt;Search&lt;/button&gt;
&lt;br&gt;
&lt;input type="reset"&gt;
&lt;br&gt;
&lt;/form&gt;

&lt;?php

if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(ISSET($_POST['submit']))
{
if(ISSET($_POST['keywords']))
{
$keywords = $_POST['keywords'];
}
<br/>
<i> </i> //Re-write the following 4 lines ...
<i> </i> mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
<i> </i> $conn = mysqli_connect("localhost","root","","test");
<i> </i> $conn-&gt;set_charset("utf8mb4");
<i> </i>
<i> </i> if(mysqli_connect_error())
<i> </i> {
<i> </i> echo "Could not connect!" . mysqli_connect_error();
<i> </i> }

<i> </i> $query = "SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ?";

<i> </i> $stmt = mysqli_stmt_init($conn);

<i> </i> //FIRST ATTEMPT TO DISPLAY TBL RESULTS USING mysqli_stmt_bind_result() FUNCTION. TEST RESULT: ATTEMPT A FAILURE!
<i> </i> if(mysqli_stmt_prepare($stmt,$query))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt,'s',$keywords);

<i> </i> //if(mysqli_stmt_execute($stmt) === FALSE)//WHY THIS LINE SHOWS ERROR ? Fatal error: Uncaught mysqli_sql_exception: No index used in query/prepared statement (null) in C:xampphtdocsebruteselect.php:53 Stack trace: #0 C:xampphtdocsebruteselect.php(53): mysqli_stmt_execute(Object(mysqli_stmt)) #1 {main} thrown in C:xampphtdocstestselect.php on line 53
<i> </i> $stmt_execution = mysqli_stmt_execute($stmt);
<i> </i> if($stmt_execution === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i>
<i> </i> $bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);
<i> </i> if($bind_result === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i>
<i> </i> $stmt_fetch = mysqli_stmt_fetch($stmt);
<i> </i> if($stmt_fetch === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i>
<i> </i> while(mysqli_stmt_fetch($stmt))
<i> </i> {
<i> </i> echo "$page_url"; echo "&lt;br&gt;";
<i> </i> echo "$link_anchor_text"; echo "&lt;br&gt;";
<i> </i> echo "$page_description"; echo "&lt;br&gt;";
<i> </i> echo "$keyphrase"; echo "&lt;br&gt;";
<i> </i> echo "$keywords"; echo "&lt;br&gt;";
<i> </i> echo "|";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i>
<i> </i> mysqli_stmt_close($stmt);
<i> </i> mysqli_close($conn);
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "1. QUERY failed!";
<i> </i> }
<i> </i> die;



Is anything wrong or buggy with my code or what ?
Copy linkTweet thisAlerts:
@developer_webauthorJul 20.2020 — Php Folks,

This is my latest update. Let me know if it is buggy or not.

<i>
</i>&lt;?php
include 'error_reporting.php';
error_reporting(E_ALL);
ini_set('error_reporting',E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');


if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(ISSET($_POST['submit']))
{
if(ISSET($_POST['keywords']))
{
$keywords = $_POST['keywords'];
}

<i> </i> $query = "SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ?";

<i> </i> $stmt = mysqli_stmt_init($conn);

<i> </i> if(mysqli_stmt_prepare($stmt,$query))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt,'s',$keywords);

<i> </i> $stmt_execution = mysqli_stmt_execute($stmt);
<i> </i> if($stmt_execution === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i>
<i> </i> $bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);
<i> </i> /*RIDDING THIS BASED ON mac_guyver's ADVICE.
<i> </i> if($bind_result === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i> */
<i> </i>
<i> </i> //$stmt_fetch = mysqli_stmt_fetch($stmt);//I SUSPECT I DO NOT NEED THIS SINCE THERE IS A WHILE LOOP AFTERWARDS.
<i> </i>
<i> </i> /*RIDDING THIS BASED ON ADVICE.
<i> </i> if($stmt_fetch === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i> */
<i> </i>
<i> </i> while(mysqli_stmt_fetch($stmt))
<i> </i> {
<i> </i> echo "$page_url"; echo "&lt;br&gt;";
<i> </i> echo "$link_anchor_text"; echo "&lt;br&gt;";
<i> </i> echo "$page_description"; echo "&lt;br&gt;";
<i> </i> echo "$keyphrase"; echo "&lt;br&gt;";
<i> </i> echo "$keywords"; echo "&lt;br&gt;";
<i> </i> echo "|";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i> /*RIDDING THIS BASED ON ADVICE.
<i> </i> mysqli_stmt_close($stmt);
<i> </i> mysqli_close($conn);
<i> </i> */
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> die("QUERY failed!");
<i> </i> }
<i> </i> //die;

<i> </i> mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
<i> </i> $conn = mysqli_connect("localhost","root","","test");
<i> </i> $conn-&gt;set_charset("utf8mb4");
<i> </i>
<i> </i> $query = "SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ?";

<i> </i> $stmt = mysqli_stmt_init($conn);
<i> </i>
<i> </i> if(mysqli_stmt_prepare($stmt,$query))
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt,'s',$keywords);
<i> </i>
<i> </i> $stmt_execution = mysqli_stmt_execute($stmt);
<i> </i> if($stmt_execution === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i>
<i> </i> $get_result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> if($get_result === FALSE)
<i> </i> {
<i> </i> printf("Error: %s.n", mysqli_stmt_error($stmt));
<i> </i> printf("Error: %d.n", mysqli_stmt_errno($stmt));
<i> </i> die;
<i> </i> }
<i> </i>
<i> </i> while($row = mysqli_fetch_array($get_result,MYSQLI_ASSOC))
<i> </i> {
<i> </i> $page_url = $row['page_url']; echo $page_url; echo "&lt;br&gt;";
<i> </i> $link_anchor_text = $row['link_anchor_text']; echo $link_anchor_text; echo "&lt;br&gt;";
<i> </i> $page_description = $row['page_description']; echo $page_description; echo "&lt;br&gt;";
<i> </i> $keyphrases = $row['keyphrases']; echo $keyphrases; echo "&lt;br&gt;";
<i> </i> $keywords = $row['keywords']; echo $keywords; echo "&lt;br&gt;";
<i> </i> echo "|";
<i> </i> echo "&lt;br&gt;";
<i> </i> }
<i> </i> mysqli_stmt_close($stmt);
<i> </i> mysqli_close($conn);
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> die("QUERY failed!");
<i> </i> }
<i> </i>}
}

?&gt;

&lt;form name = "search" method = "GET"&gt;
&lt;label for="keywords"&gt;Keywords:*&lt;/label&gt;
&lt;input type="text" name="keywords" id="keywords" placeholder="Input Keywords" required&gt;
&lt;br&gt;
&lt;button type="submit"&gt;Submit&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" value="submit"&gt;Submit&lt;/button&gt;&lt;br&gt;
&lt;input type="submit" value="submit"&gt;&lt;br&gt;
&lt;button name=submit value=" "&gt;Search&lt;/button&gt;&lt;br&gt;
&lt;button type="submit" name="submit" value="submit"&gt;Search&lt;/button&gt;
&lt;br&gt;
&lt;input type="reset"&gt;
&lt;br&gt;
&lt;/form&gt;


I was told to switch form method = GET. Originally, it was POST. I think it should be POST since I am passing no variables through the url. no params in the url. Am not making use of $_GET to extract params from url. What you say ?
Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — @developer_web#1620955 said ...

I was told to switch form method = GET. Originally, it was POST. I think it should be POST since I am passing no variables through the url. no params in the url. Am not making use of $_GET to extract params from url. What you say ?

when using POST special precautions must be addressed

in your code you do not address the special circumstance

I would say you should use GET or else change your code

to allow for proper use of POST

there is a lot to learn about submitting data to server
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1620965

You mean I did not VALIDATE user inputs ? Ok, switching to GET.

I notice, if I switch the GET method and click submit button then php auto creates params in the url that I did not ask for. Nevermind. Switching to GET. Php is just taking name="whatever" and adding the whatever in the url as params:

url.com/?whatever=
Copy linkTweet thisAlerts:
@NogDogJul 21.2020 — Basically, use GET if the request is read-only, i.e. it won't change anything in the database, create an order, etc. Use POST if it will, in fact, potentially change something. In other words, if it's read-only, it doesn't matter if a user bookmarks the link with all those parameters in the URL; but if it changes something, then you do not want it to be bookmarked or otherwise re-used.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2020 — @NogDog#1621065

I did not understand this part:

"but if it changes something, then you do not want it to be bookmarked or otherwise re-used."
Copy linkTweet thisAlerts:
@daveyerwinJul 30.2020 — @developer_web#1621503

instead of worrying about the subtleties

of a POST request just stick with GET

till you are comfortable with submitting

data to a server
×

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