PHP MySQL Undefined index when displaying on web page - php

I have a fairly simple PHP page which displays some fields from a database.
For some reason I get:
Notice: Undefined index: entries.id in /var/www/html/originalprices.php on line 24
I can't see whats wrong, could anyone help? Thanks
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "zxdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT entries.id, entries.title, entrytypes.text,entries.original_price, entries.budget_price,labels.name FROM entries,publishers, labels, entrytypes
where entries.entrytype_id = entrytypes.id
and publishers.entry_id = entries.id
and publishers.label_id = labels.id
and labels.id = '1371'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["entries.id"]. " - Name: " . $row["entries.title"]. " " . $row["entrytypes.text"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

The Name of the returned field will be "id" not entries.id.
You can check that by using var_dump()

Related

php script not sending query to mysql db?

The below code I got from the w3 schools website and adapted it a little to my db but no matter what when I click on my search button I only get the first line printed and nothing else. It doesn't even print if the connection to my db is successfully made or not.
I used chrome's dev tool to check my network traffic and I can see my POST request made successfully:
name: bahamas
submit: Search
I enabled logging for both error and general on my mysql instance, and did a grep for bahamas and got no hits. So this would seem to indicate that the script didn't even query my db?
IE this is what I get: https://imgur.com/a/PHKBmbU
<?php echo("PHP Search Page Loaded Successfully");
if(isset($_POST['submit'])){
if(preg_match("^/[A-Za-z]+/", $_POST['name'])){
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo("We are not dead");
}
$sql = "SELECT boatname, date, price FROM liveaboards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo("<br> boatname: ". $row["boatname"]. " - date: ". $row["date"]. " " . $row["price"] . "<br>");
}
} else {
echo(" 0 results");
}
$conn->close();
}
}else{
echo("<p>Please enter a search query</p>");
}
?>
In following the theme of W3 Schools (one of my favorite resources), here is the correct way to sanitize data: https://www.w3schools.com/php/php_filter.asp
Applied to your code:
<?php echo("PHP Search Page Loaded Successfully");
// test variables.
$_POST['submit'] = true;
$_POST['name'] = "bahamas";
if(isset($_POST['submit'])){
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo("We are not dead");
}
$sql = "SELECT boatname, date, price FROM liveaboards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo("<br> boatname: ". $row["boatname"]. " - date: ". $row["date"]. " " . $row["price"] . "<br>");
}
} else {
echo(" 0 results");
}
$conn->close();
}else{
echo("<p>Please enter a search query</p>");
}
?>
According to your POST result, you are receiving name: bahamas submit: Search , if so then Correct this
if(isset($_POST['submit'])){
TO
if(isset($_POST['Search'])){

SQL database connection in PHP successful, but I can't query it [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords

Mysql output displaying into XCODE? With webview?

I have a PHP script that outputs my events from my ICAgenda calendar on the website.
It outputs "name" "date" and "location" / event.
How can I import this into XCODE to show the events?
I thought styling the php script, and then add it as a webview in XCODE ? Or I'll guess there is another workaround to get the data in XCODE ?
This is the code that gathers the events:
<?php
$servername = "myservername";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT next, title, city FROM jos_icagenda_events ORDER BY next";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Datum & tijd: " . $row["next"]. "<br>";
echo "Plaats: " . $row["title"]. "<br>";
echo "Locatie: " . $row["city"]. "<br><br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

not able to show all records in my database

i am testing my database by executing some MySQLi statements
in this case : i want to display all the records of 2 specefic rows (name,score)
i checked how to do such thing in PHP , and i did it
problem is , the page is not showing anything at all , (blank empty page)
My code :
<?php
$servername = "sql3.freesqldatabase.com";
$username = "MY USERNAME";
$password = "MY PASSWORD";
$dbname = "MY DBNAME";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,score FROM Scores");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " " . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
and , i executed the same query in phpMyAdmin Control Panel , and it worked
What have i done wrong ?
This line
$sql = "SELECT name,score FROM Scores");
Should be
$sql = "SELECT name,score FROM Scores";
This syntax error will cause an error and your environment is likely suppressing errors/warnings.

Connecting PHP to mySQL and retrieving data

i am new to PHP, and i am attempting to create a simple connection from html to mySQL using php. I met with some problems when running my codes.
this is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["userid"]. ;
}
} else {
echo "0 results";
}
$conn->close();
?>
after running on a browser, this is displayed:
connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT username FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "
id: ". $row["userid"]. ; } } else { echo "0 results"; } $conn->close(); ?>
Do you have mysql running on your localhost machine? You must verify that it is working first before you can connect via php. Also, make sure you have TCP/IP sockets open in mysql and to make sure it isn't just listening via unix sockets.
echo "<br> id: ". $row["userid"]. ;
this is a syntax error, no need to end it with a full stop.also the correct syntax to connect to sql server is
mysqli_connect("localhost","my_user","my_password","my_db") or die("");
Debug the code before posting it here.
maybe try testing it with a try catch statement. Its what I've done. this way you can display your error messages a little more nicely. As for the cause, PressingOnAlways is probably right
If you are using wampserver or any other server you need to put your php files into c:\wamp\www folder and run them in browser by typing localhost/nameofyourfile.php (change c:\wapm\www for your server installation path or type)
you have the syntax error in blow line
echo "<br> id: ". $row["userid"]. ;
. (dot) should not be there.
second you fetch usernamestrong text by following query
$sql = "SELECT username FROM users";
but you try to get userid
echo "<br> id: ". $row["userid"]. ;
try below code.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "databasename";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT usersname FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["usersname"] ;
}
} else {
echo "0 results";
}
$conn->close();
?>

Categories