This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
Am using PDO method to get data from SQL But am unable to get data from SQL database since am new to PDO its little hard to understand
but i did the following code but Doesn't work can someone help me
CODE
<?php
$servername = "localhost";
$username = "sanoj";
$password = "123456";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM members");
$stmt->execute();
// set the resulting array to associative
$membid=($_GET['memberID']);
$email=($_GET['email']);
$state=($_GET['username']);
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
echo $membid;
echo $email;
echo $state;
$conn = null;
echo "</table>";
?>
ERROR I GET
Notice: Undefined index: memberID in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 14
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
( ! ) Notice: Undefined index: email in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 15
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
( ! ) Notice: Undefined index: username in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 16
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
Using $_GET variable won't make you get your data from your database using PDO. You were doing right up until $stmt->execute();. What you need to do next is:
while($result = $stmt->fetch(PDO::FETCH_ASSOC))
{
foreach($result as $key => $value)
echo $key.': '.$value.'<br/>';
echo '<hr/>';
}
To get your data.
$_GET array is to fetch your data from the URL or from a GET Ajax call.
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
I am trying to fetch the data from database for a an input field product code and i need to use its value to update the rest of the column values in the database but instead it is creating a different record and in the value field of the input box 'named' code, it shows and undefined variable error, please help.
HTML code:
<div class="small-8 columns">
<input type="text" id="right-label" placeholder="Product_code"
value="<?php echo "$pcode"?>" name="code">
</div>
PHP Script:
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="bolt";
try{
$conn = new
PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST["submit"])){
$pcode = ($_POST["code"]);
$pname = ($_POST["Pname"]);
$pdesc = ($_POST["desc"]);
$pimg = $_FILES["Img_name"]["temp_name"];
$imgExt = strtolower(pathinfo($pimg,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg','jpg','png','gif','pdf');
$pqty = ($_POST["Pqty"]);
$pprice = ($_POST["Pprice"]);
$sql="UPDATE products SET product_name=$pname,product_desc=$pdesc,
product_img_name=$pimg,qty=$pqty,price
=$pprice) WHERE product_code=$pcode";
$stmt = $conn->exec($sql);
$stmt->execute();
echo $stmt->rowCount() . "new records added succesfully";
}
}
catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
$sql is declared within the if condition, if if(isset($_POST["submit"])){
is false, you will get this error because $sql is not within the scope. Declare it on above condition and initialize it.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
The purpose of my lab is to have the user search through a database called "pageVistis" from what they enter in the field. They can search through the remote host or page name. I have Been having trouble fixing an error that i am recieving saying "Notice: Undefined variable: searchtype in C:\xampp\htdocs\pageVisitsLab\DataBC.php on line 23
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\pageVisitsLab\DataBC.php:25 Stack trace: #0 {main} thrown in C:\xampp\htdocs\pageVisitsLab\DataBC.php on line 25". Also the code is form a file called "DataBC.php". If any extra information is needed please let me know and i will edit this post and include it thank you.
include "interface.php";
if(isset($_POST['submit'])) {
$sName = $_POST['sN'];
$sHost = trim($_POST['sH']);
if(!$sName || ! $sHost) {
echo "<p> Please enter a search </p>";
exit;
}
/*switch ($sName) {
case 'sName':
case 'sHost':
break;
default:
echo "<p> Invalid search type and please try again </p>";
}
*/
$query = "SELECT page_name, visit_date, previous_page, request_method, remote_host, remote_port FROM visitInfo
WHERE $searchtype= ?";
$stmt = $databse -> prepare($query);
$stmt-> bind_param('s',$sName);
$stmt-> execute();
$stmt->store_result();
$stmt->bind_result($pageName,$visitDate,$previouPage,$requestMethod,$remoteHost,$remotePort);
echo "<p> The number of items found: .$stmt->num_rows</p>";
while ($stmt->fetch()) {
echo "<p><strong>page Name: ".$pageName."</strong></p>";
echo "Visit Date: ".$visitDate."<br>";
echo "Previous Page: ".$previouPage. "<br>";
echo " Request Method: ".$requestMethod. "<br>";
echo "Remote Host: ".$remoteHost. "<br>";
echo "Remote Port: ".$remotePort. "<br>";
}
$stmt->free_result();
$databse->close();
}
?>
I think your problem is in your query, is $searchtype a variable you define in php? maybe in that interface.php.
If you want to use a php variable in a mysql query then that is not the correct way of doing it.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have used a prepared statement to define the id index however, it is telling me that it is undefined for some reason, where and what do i change for this to work?
<?php
$db_username='student';
$db_password='student';
$db = new PDO ('mysql:host=192.168.56.2;dbname=Assessment', $db_username, $db_password);
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname='".$_GET['id']."' ");
$result->execute();
Try this:
<?php
$db_username='student';
$db_password='student';
$db = new PDO ('mysql:host=192.168.56.2;dbname=Assessment', $db_username, $db_password);
if(isset($_GET['id'])) {
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname=?");
$result->execute(array($_GET['id']);`enter code here
}
else {echo('$_GET["id"] not set');}
First, verify that $_GET['id'] has a value. Second, for security, change some lines:
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname=:id");
$result->execute(array(':id' => $_GET['id']));
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
UPD: CLOSED. Duplicate found and typos in my original question
I am using this code and I want to get a message if there is an error with my SQL query :
$Db = mysqli_init();
$Db->options(MYSQLI_OPT_LOCAL_INFILE, true);
$Db->real_connect($servername, $username, $password, $dbname, 3306);
// Creation of first SQL query
$sql = ('select sum('.$metric1.') as t1metric from '.$table1.' WHERE '.$date1.' between "'.$start_date.'" AND "'.$end_date.'"');
$query = $Db->query($sql);
if ($Db->error)
{
printf("Errormessage: %s\n", $Db->error);
}
and I receive this error when I run the php file :
Call to a member function query() on a non-object
use the following
$Db->query($sql);
instead of
$mysqli->query($query);
$mysqli->query($query);
Replace with:
$Db->query($query);
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have simple inventory DB. When trying to access the products table and display the data in a table, i continually get "Unidentified index" and nothing i have found thus far can assist me. I am just starting PHP
conn.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "slfs_storesb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_errno) {
printf("Connection failed: %s\n" . $conn->connect_error);
}
?>
index.php
<?php
$sql = "select * from tbl_station";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)) {
var_dump($row);
// print $row[4];
//echo '<table id="t01">';
echo $row["Station_id"]." ".$row["Station_name"]." ". $row["Station_email"];
// echo '</table>';
}
// } else {
// echo "0 results";
// }
?>
THis is the error
Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
Gros Islet Fire Station
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
GFL Fire Hall
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
HeadQuarters
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
Dennery class footer { This is footer }
Instead of using SELECT * just spell out the (three) fields you want to access.
This way the query will fail if one of the fields is misspelled (or simply doesn't exist in that table).
<?php
$sql = '
SELECT
Station_id, Station_name, Station_email
FROM
tbl_station
';
$result = $conn->query($sql)
or trigger_error('query failed: '.join(',', $conn->error_list));
while( $row=$result->fetch_assoc() ) {
echo $row['Station_id'], ' ', $row['Station_name'],' ', $row["Station_email"], "<br />\r\n";
}