/    Sign up×
Community /Pin to ProfileBookmark

Why php fails To Find Function ?

Folks,

I get this error:
Notice: Undefined variable: _SESSION in C:xampphtdocstestselect.php on line 16

Line 16 is this:
**$stmt = mysqli_stmt_init($conn);**

Note following code is from file insert.php
It references to file conn.php which defines the variable $conn.
Note on line 3 below, I am referencing to conn.php and so php has seen the 4conndefinition from the conn.php and so it should not give the error. or is the difinition losing it’s value because the $conn has been defined in a function ? How to fix this ? Turn the $conn variable into GLOBAL or a global session ?

[code]
GLOBAL $conn = $conn = mysqli_connect($db_server,$db_user,””,$db_database);
[/code]

Like above I should do or like below ?

[code]
$_SESSION[‘conn’] = $conn = $conn = mysqli_connect($db_server,$db_user,””,$db_database);
[/code]

Look at my error_reporting.php.
I dumped all the error reporting codes there instead of repeat these lines in the main “insert.php” file.

[code]
<?php
ini_set(‘error_reporting’,’E_ALL’);
ini_set(‘display_errors’,’1′);
ini_set(‘display_startup_errors’,’1′);
error_reporting(E_ALL);
?>
[/code]

Look at my conn.php where $conn is defined:

[code]
<?php

function conn()
{
$db_server = ‘localhost’;
$db_user = ‘root’;
$db_password = ”;
$db_database = ‘test’;

//mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($db_server,$db_user,””,$db_database);
$SESSION[‘conn’] = $conn;

mysqli_set_charset($conn,’utf8mb4′);

$conn = mysqli_connect(“localhost”,”root”,””,”test”);
$conn->set_charset(“utf8mb4”);
if(mysqli_connect_error())
{
echo “Could not connect!” . mysqli_connect_error();
}
}

conn();

?>
[/code]

Here’s the file that is giving error on line 16.
I need my questions answered that you see in CAPITALS in my code comments. Kindly, answer them.

insert.php

[code]
<?php
include ‘error_reporting.php’;
require ‘conn.php’;

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

$query = “SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ?”;

$conn = $_SESSION[‘conn’];
$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$keywords);

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

$bind_result = mysqli_stmt_bind_result($stmt,$page_url,$link_anchor_text,$page_description,$keyphrase,$keywords);
/*RIDDING THIS BASED ON mac_guyver’s ADVICE.
if($bind_result === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
*/

//$stmt_fetch = mysqli_stmt_fetch($stmt);//I SUSPECT I DO NOT NEED THIS SINCE THERE IS A WHILE LOOP AFTERWARDS.

//I AM TOLD I SHOULD NOT CHECK $STMT WITH THIS IF HERE. SHOULD I CHECK OR NOT ?
if($stmt_fetch === FALSE)
{
printf(“Error: %s.n”, mysqli_stmt_error($stmt));
printf(“Error: %d.n”, mysqli_stmt_errno($stmt));
die;
}
*/

while(mysqli_stmt_fetch($stmt))
{
echo “$page_url”; echo “<br>”;
echo “$link_anchor_text”; echo “<br>”;
echo “$page_description”; echo “<br>”;
echo “$keyphrase”; echo “<br>”;
echo “$keywords”; echo “<br>”;
echo “|”;
echo “<br>”;
}

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

}
else
{
die(“QUERY failed!”);
}

conn();

$query = “SELECT page_url,link_anchor_text,page_description,keyphrases,keywords FROM links WHERE keywords = ?”;

$stmt = mysqli_stmt_init($conn);

if(mysqli_stmt_prepare($stmt,$query))
{
mysqli_stmt_bind_param($stmt,’s’,$keywords);

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

$get_result = mysqli_stmt_get_result($stmt);

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

while($row = mysqli_fetch_array($get_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(“QUERY failed!”);
}
}
}

?>

<form name = “search” method = “POST”>
<label for=”keywords”>Keywords:*</label>
<input type=”text” name=”keywords” id=”keywords” placeholder=”Input Keywords” required>
<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=” “>Search</button><br>
<button type=”submit” name=”submit” value=”submit”>Search</button>
<br>
<input type=”reset”>
<br>
</form>

[/code]

Shall I keep the form method = POST or switch to GET ?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — ``<i>
</i>{
die("QUERY failed!");
}

conn();

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

</i>
``

where is conn() defined ?
Copy linkTweet thisAlerts:
@NogDogJul 20.2020 — > @developer_web#1620958 Notice: Undefined variable: _SESSION in C:xampphtdocstestselect.php on line 16

Once again: where do you do the session_start()? THERE IS NO $_SESSION IF THERE IS NO session_start().
Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — Can you explain why you want to ...

$SESSION['conn'] = $conn;

,it seems like a very bad idea ?
Copy linkTweet thisAlerts:
@NogDogJul 20.2020 — > @DaveyErwin#1620972 it seems like a very bad idea

Besides the fact that it won't actually be usable on any subsequent pages (assuming that's a mysqli connection?).
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — I think I fixed it now. Have a look.
<i>
</i>


<i>
</i>
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1620972

conn() was defined in conn.php. Look above on my post.

I think I fixed it now. Have a look.

@NogDog, @Sempervivum, @DaveyErwin, @Ginerjm,

I need answers to QA, QB & QC.

Also, on the final code, notice the comments on the code in CAPITALs. There are 4 questions there: Q1, Q2, Q3 & Q4. I need them answered.

Thanks folks!

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

QA. Shall I keep the above as is or change it ? Change it to what ? Show me sample code.

conn.php (required file)
<i>
</i>&lt;?php

$db_server = 'localhost';
$db_user = 'root';
$db_password = '';
$db_database = 'test';

$conn = mysqli_connect($db_server,$db_user,"",$db_database);
mysqli_set_charset($conn,'utf8mb4');

mysqli_set_charset($conn,'utf8mb4');

$conn = mysqli_connect("localhost","root","","test");
$conn-&gt;set_charset("utf8mb4");
if(mysqli_connect_error())
{
echo "Could not connect!" . mysqli_connect_error();
}

?&gt;

QB. Shall I keep the above as is or change it ? Change it to what ? Show me sample code.

select.php (main file)
<i>
</i>&lt;?php
include 'error_reporting.php';
require 'conn.php';

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> /* Q1. RIDDING THIS BASED ON mac_guyver's ADVICE. SHALL I RID THIS OR NOT ?
<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);// Q2. I SUSPECT I DO NOT NEED THIS SINCE THERE IS A WHILE LOOP AFTERWARDS. SHALL I KEEP THIS INTACT OIR DELETE IT ?
<i> </i>
<i> </i> /*RIDDING THIS BASED ON mac_guyver's 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)) //Q4. WHY TUTORIALS SHOW TO printf INSTEAD OF ECHO THE FOLLOWING ?
<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> /* Q3. RIDDING THIS BASED ON mac_guyver's ADVICE. SHALL I RID THIS OR NOT ?
<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> }
}

QC. Shall I keep the above as is or change it ? Change it to what ? Show me sample code.
Copy linkTweet thisAlerts:
@developer_webauthorJul 21.2020 — @DaveyErwin#1620963

That was defined in conn.php.
Copy linkTweet thisAlerts:
@VITSUSAJul 22.2020 — @developer_web#1621056 You can :), have you run this code?
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2020 — @VITSUSA#1621094

Ocourse. I ran the code then got errors and that is why I opened this thread.

How-about you run the code and see for yourself too ?
×

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