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);
Like above I should do or like below ?
[code]
$_SESSION[‘conn’] = $conn = $conn = mysqli_connect($db_server,$db_user,””,$db_database);
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);
?>
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();
?>
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>
Shall I keep the form method = POST or switch to GET ?