PHP if SteamID is equal to the MYSQL Row - php

It will not work. I'm trying like below.
if (($row["steamid"]) == $steamprofile['steamid']){
Below is full code snippet.
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$eh = $steamprofile[steamid];
$sql = "SELECT steamid FROM Main";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if (($row["steamid"]) == $steamprofile['steamid']){
echo "SteamID Is Equal and Created!";
}
}
} else {
echo "Nope?";
}
$conn->close();
?>
Ive tried various methods.

// Create connection
$conn = new mysqli("localhost", "root", "", "stackoverflow");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//assume 1
$steamprofile['steamid']=1;
// changes here single quote
$eh = $steamprofile['steamid'];
$sql = "SELECT steamid FROM Main";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
if (($row["steamid"]) == $steamprofile['steamid']){
echo "SteamID Is Equal and Created!";
}
}
} else {
echo "Nope?";
}
$conn->close();
?>
output is
SteamID Is Equal and Created!
Mysql Database------------
CREATE TABLE IF NOT EXISTS main (
steamid int(11) NOT NULL,
name varchar(500) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table main
INSERT INTO main (steamid, name) VALUES
(1, 'test1'),
(2, 'test2');

I think your
$eh = $steamprofile[steamid];
should be changed to
$eh = $steamprofile['steamid'];

The comment by:
try echo $row["steamid"]) ."==". $steamprofile['steamid']; so you can print and check that. – Hardy Mathew
Worked. Thanks.

Related

Get rows from database

I have a connection where i want to get some data from my database.
I have inserted some data but now i want to retreive it but i get NULL.
I have no idea why.
<?php
require "connect.php";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM tbltemperature ORDER BY time DESC LIMIT 1";
$result = $conn->query($sql);
var_dump($results);
$t = 0;
while($row = $result->fetch_assoc()) {
$weather[] = array(
$row["time"],
$row["inside_temperature"]
);
echo $row["time"];
echo $row["inside_temperature"];
}
$conn->close();
?>
check var_dump($results); , it looks like it should be var_dump($result);
otherwise you should check $result->num_rows() first to know if there is any row available.
Firstly you have to check your query in phpmyadmin query is working or not. If working you should try to var_dump($result); and after
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> time: ". $row["time"]. " - inside_temperature: ". $row["inside_temperature"]."<br>";
}
} else {
echo "0 results";
}

MYSQL DELETE multiple row doesnt work

This is delete.php
<?php
if(isset($_GET['id']) && !empty($_GET['id']))
{
$id = $_GET['id'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM table WHERE memberid IN ('".$id."' )";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully" ;
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
}
?>
this is home.php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select memberid from table group by memberid having count(*) > 5";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Member ID: " . $row["memberid"]. " | <a href='javascript:void(0);' onClick='deleteId(".$row["memberid"].")'>reset</a><br>";
}
} else {
echo "0 results";
}
$conn->close();
<script>
function deleteId(id)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
alert(xmlhttp.responseText);
}
};
xmlhttp.open("GET", "delete.php?id=" +id, true);
xmlhttp.send();
//window.location.reload();
}
</script>
I'm trying to delete multiple row in a table by onclick from home.php unfortunately it doesnt work at all.
in the table row recorded many same memberid, so i'm trying to call by memberid and delete all of them in 1click
I'm new in php and mysql.
Thanks for the help.
$sql = "DELETE FROM table WHERE c ='$id'";
Try this:
$idArr = explode(",", $id);
$idStr = implode("','", $idArr);
$sql = "DELETE FROM table WHERE c IN ('$idStr' )";
Also, in your client side code,
Make sure that the id in deleteId() is alerting 012345 instead of 12345. Otherwise, convert the integer to string before calling deleteId().
You are sending in one id, so no need for using IN(), normal id = ? will delete all rows with that id. But you need to use bind parameters to stop someone from sending in something like 1 or 1=1 to delete all the rows in your table, don't trust the input even if it is sent from your javascript

Executing php code by clicking a link

Good people i am stuck with an issue that i know has been discussed here before. I am displaying mysql data as a link and want to load more data from the database when a user clicks the link, I looked at all the previous questions and answers but i can't sort this one out. Here is how far i have managed to drag myself:
index.php:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT topic FROM steps";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
$id = isset($_get['id']);
echo ''.$row['topic'].'<br/>';
}
}
$conn->close();
?>
moreinfo.php:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = isset($_get['id']);
$sql = "SELECT * FROM steps WHERE id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
echo "Description: " . $row['topic'] . "<br />";
}
}
else{
echo "123";
}
$conn->close();
?>
This doesn't give me any error, just that the hyperlinks on this line
echo ''.$row['topic'].'<br/>';
on index.php does not append an id ($id) to the hyperlink so that moreinfo.php can execute correctly.
I rily pray i'm making some sense here...
Use $_GET['id'] insted of $_get['id']
Correct usege of isset is looks like this
$id = isset($_GET['id'])?$_GET['id']:false
Always check if parameter which you will use next was really passed
$id = isset($_GET['id'])?$_GET['id']:false;
if(!$id)
die('id was not passed');
And last, you were overwriting variables from db with data from GET which should be on first page (probably copy and past )
$topic = isset($_get['topic']);
$id = isset($_get['id']);
here is your fixed code:
index.php
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
$sql = "SELECT * FROM steps";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo ''.$row['topic'].'<br/>';
}
}
$conn->close();
?>
moreinfo.php
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
$id = isset($_GET['id'])?$_GET['id']:false;
if(!$id)
die('id was not passed');
$sql = "SELECT * FROM steps WHERE id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$topic = isset($_get['topic']);
echo "Description: ".$row['topic']."<br />";
}
} else {
echo "123";
}
$conn->close();
?>
sql table create statement
CREATE TABLE `steps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`topic` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Ok first thing first you are overwriting your id in this line
$id = isset($_get['id']);
so when you try to use it it holds bool value (true or false) instead of actual $id. And you do it in both of your files.

how can I display all the data from database using an array

I'm trying to connect $countries to an array that will display all the data from my database, it only displays the last data I entered.Is there anyway I can display all of them?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries = $tom;
}
} else {
echo "0 results";
}
$conn->close();
because you have assinged data to $countries with wrong manner:
$countries = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries[] = $tom; // use []
}
} else {
echo "0 results";
}
$countries[] = $tom;
With [] at the end of inicialization of variable you add a row to that array. If you do it without [], it's just inicialization every time in while loop.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
$countries[] = " ". $row["quantity"] . $row["product_name"];
} else {
echo "0 results";
}
$conn->close();
// create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
// get the data
$sql = "SELECT product_id, product_name, quantity FROM inventory";
$countries = $conn->query($sql)->fetch_all();
Although it's only available with mysqlnd installations, you need one, because without mysqlnd mysqli is unusable anyway.

PHP SELECT * FROM not working

I am trying to query data from a table using the following script:
//connect file
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//connect file included above - ECHO tested and it is connected as $conn
$sql = "SELECT * FROM userInfo";
$results = $conn->query($sql);
if (!$results) {
printf("Errormessage: %s\n", $conn->error);
exit;
} else {
echo $row['username'];
}
UPDATE --
It now no longer tries to throw an error and seems to go to the else section; however, no echo - and the spelling is correct this time and the column is filled.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["username"];
}
} else {
echo "0 results";
}
This now returns results. Thank you #Fred -ii- especially for your help on this.
Also thanks #jjczopek for the error checking advice!

Categories