Displaying data from database issue - php

I'm trying to display all the rows in my data from my data of my specific user but its only displaying 1 row and not updating.
the code is -
$user = $_SESSION["username"];
$sql = "SELECT * FROM transactions WHERE sender OR receiver = ?";
if ($sql = $conn->prepare($sql)) {
$sql->bind_param("s",$user);
if ($sql->execute()) {
$result = $sql->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<br> sender: ". $row["sender"]. " - receiver: ". $row["receiver"]. " -amount:" . $row["amount"] . "<br>";
}
}
$sql->close();
$conn->close();
}else{ echo "0 result"; }
}
?>

You are using wrong SQL formation & passing just receiver value. I suggest below changes:
Replaced
1) WHERE sender OR receiver = ? with WHERE sender=? OR receiver = ?
2) $sql->bind_param("s",$user); with $sql->bind_param("ss",$user,$user);
$user = $_SESSION["username"];
$sql = "SELECT * FROM transactions WHERE sender=? OR receiver = ?";
if ($sql = $conn->prepare($sql)) {
$sql->bind_param("ss",$user,$user);
if ($sql->execute()) {
$result = $sql->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<br> sender: ". $row["sender"]. " - receiver: ". $row["receiver"]. " -amount:" . $row["amount"] . "<br>";
}
}
$sql->close();
$conn->close();
}else{ echo "0 result"; }
}

Related

PHP + MySQL taking a 3rd variable

I have a C# code and a PHP that gets info I send from C#.
So I need to have such code:
<?php
$link = mysqli_connect('localhost','root','');
$database = mysqli_select_db($link,'serial');
$serial = $_GET['serial'];
$hwid = $_GET['hwidin'];
$sql = "SELECT * FROM serial WHERE serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
/*
0 = Wrong HWID
1 = HWID is correct
2 = HWID left empty
3 = No serial with that key
*/
if(strlen($hwid) < 1)
{
echo "2";
}
else
{
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
}
else
{
$sql = "UPDATE serial SET hwid='$hwid' WHERE serial='$serial'";
echo "1";
if(mysqli_query($link, $sql))
{
echo $row['hwid'];
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
}
}
else
{
echo "3";
}
}
So what I want to do is add some checking, for example:
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0";
}
else
{
echo "1";
}
If echo "1" {
$sql = "SELECT time FROM serial Where serial = '". mysqli_real_escape_string($link,$serial) ."'" ;
$result = $link->query($sql);
if $result > 1 {
echo "55";
}
I need somehow to combine it with hwid checker and serial checker.
I want to check "time" column variable and use > < 1 to decide what to echo.

Conditional Logic on MySQL Query with PHP

I am doinga MySQL query to retreive data using PHP. I need to have a logic that if the data set returned is empty it shows a warning message else it displays the results:
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
mysqli_close($conn);
You need num_rows on the object
$result = mysqli_query($conn,$sql);
if ($result->num_rows == 0) {
echo 'result empty';
} else {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
http://php.net/manual/en/mysqli-result.num-rows.php
Change mysql_escape_string to mysqli_escape_string here
$where = "";
if(isset($_POST['searchQuery']) && trim($_POST['searchQuery'])){
$searchQuery = mysqli_escape_string($conn,$_POST['searchQuery']);
$where =" WHERE column1 = '".$searchQuery."'";
}
$sql="SELECT * FROM db.tblname ".$where;
It's as simple as this:
if ($result) {
//Your code
} else {
echo "Error: ".mysqli_error($conn);
}
First check with mysqli_num_rows(). Here is how to
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
else {
echo "No records found";
}
mysqli_close($conn);
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
} else {
echo "No results found";
}
mysqli_close($conn);

Search database using array and then echo/print result in foreach loop using PHP

I need to get variable code from URL so I $codes = $_GET['code']; (url example website.com/update?code[]=7291&code[]=9274&code[]=8264&) then I SELECT firstname FROM guests WHERE invitecode = $codes" then I output data and set as $relatives = $row["firstname"] and then later on in the file I need to echo/print print $relative.
Why is this not working for me?
... connection made ...
$codes = $_GET['code'];
$sql = "SELECT firstname FROM guests WHERE invitecode = $codes";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$relatives[] = $row["firstname"];
}
}
foreach ($relatives as $relative) {
print $relative;
}
Update:
So now using:
<?php
$codes = $_GET['code'];
$thecodes = "";
foreach($codes as $vals)
$thecodes .= (int)$vals . ",";
if($thecodes != "")
{
$thecodes = trim($thecodes, ",");
$sql = "SELECT firstname FROM guests WHERE invitecode IN ($thecodes)";
$result = mysqli_query($conn, $sql);
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$relatives[] = $row["firstname"];
}
}
foreach ($relatives as $relative) {
print $relative;
}
}
else
{
}
?>
It works but I would like to enter the foreach ($relatives as $relative) { echo $relative; }; into a value like this $message = $firstname . " " . $lastname . " will be coming to your event. " . ;.
In the end it would turn out something like this: $message = $firstname . " " . $lastname . " will be coming to your event. " . foreach ($relatives as $relative) { echo $relative . " "; };.
For some reason it won't work when I combine them.
Use the IN operator for this.
<?php
$codes = $_GET['code'];
$thecodes = "";
foreach($codes as $vals)
$thecodes .= (int)$vals . ","; //Loop through making sure each is an int for security reasons (No sqli)
if($thecodes != "") //There is at least one code
{
$thecodes = trim($thecodes, ","); //Remove any additional commas
$sql = "SELECT firstname, lastname FROM guests WHERE invitecode IN ($thecodes)"; //Use the IN operator
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo $row["firstname"] . " " . $row["lastname"] . "is coming to your event";
}
}
}
else //No codes to be queried
{
}
?>
Can this be a solution for you?
$relatives = array(); // declare array
$codes = $_GET['code'];
$sql = "SELECT firstname FROM guests WHERE ";
foreach ($codes as $code) $sql .= "invitecode = " . intval($code) . " OR ";
$sql .= "1=2"; // simple way to remove last OR or to make sql valid if there are no codes
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
array_push($relatives, $row["firstname"]);
}
}
foreach ($relatives as $relative) {
print $relative;
}
I think this will work...
... connection made ...
$codes = $_GET['code'];
$sql = "SELECT firstname FROM guests WHERE invitecode = '$codes'";
$result = mysqli_query($conn, $sql) or die('-1' . mysqli_error());
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo ($row['firstname']);
}
}

Is it possible to assign sql table value to a variable in php?

Basically what I want is to assign the value of a field in my database to an variable. Can it be done in an effective way?
I was thinking something like:
$sql2 = mysql_query("SELECT * FROM rom WHERE idrom = 101");
while ($row = mysql_fetch_array($sql2)) {
$rom1 = $row['idrom'];
$status = $row['status'];
echo $rom1;
echo $status;
}
But this doesn't echo anything.
Edit:
I have gotten a bit longer on the way, now I am looking for a simpler way to assign the values to variables. As we speak I only need 4 values, but this still doesn't look like a very good way to accomplish what I want. Any better suggestions?
Heres what I got now:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM rom WHERE idrom = 101";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$room101 = $row["idrom"];
$status101 = $row["status"];
echo "This is roomnumber ". $room101 . "!<br >";
echo "And the status of roomnumber ". $room101 ." is ". $status101 ."<br><br>";
}
} else {
echo "0 results";
}
$sql2 = "SELECT * FROM rom WHERE idrom = 102";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$room102 = $row["idrom"];
$status102 = $row["status"];
echo "This is roomnumber ". $room102 . "!<br >";
echo "And the status of roomnumber ". $room102 ." is ". $status102 ."<br><br>";
}
} else {
echo "0 results";
}
You can use bind_result to do that.
Please try this simple example, hope it run well :
$mysqli = mysqli_connect('host', 'user', 'pass','dbase')or die('Could not connect: ' . mysqli_error());
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Because you provide an Id in where clause, you can use it for the new variable
$output = array();
$id = 101;
$sql = "select idrom,status from rom where idrom=?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i',$id);
$stmt->execute();
if ($stmt->errno == 0) {
$stmt->store_result();
// $stmt->bind_result($idrom,$status); // way 1
$stmt->bind_result($output[$id],$output["status".$id]); // way 2
while ($stmt->fetch()) {
echo $output[$id]." -> ".$output["status".$id]."<br />"; // So, your output variable will be like $output[101] for way 2
// or you defined it here like :
// $output[$idrom] = $idrom;
// $output["status".$idrom] = $status; // For way 1
}
} else {
return "Error: " . $sql . "<br>" . $stmt->error;
}
$stmt->close();
$mysqli->close();

If 0 items in DB table, return error

I am looking for a way to display an error message if there is nothing listed in the table.
I have a photos table.
If this tables is empty, id like to echo something.
else, show the pictures.
inside of that table I have
id, name, url
id = id
name = name of image
url = url of image.
If there are no rows, we have an error.
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
mysql_fetch_array($query1);
if(empty($query1)) {
echo "nothing";
} else {
echo "good";
}
Try this,
$query = "SELECT * FROM photos";
$result= mysql_query($query);
$length= mysql_num_rows($result);
if($length>0)
{
while($rows = mysql_fetch_array($result))
{
echo $rows['name'];
echo "<img src='$rows[url]' />";
}
}
else
{
echo "Nothing to display";
}
Hope this will work
What about something like...
$sql = "SELECT COUNT(*) AS amountPhotos FROM photos";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row["amountPhotos"] == 0) {
echo "There are no photos in the photo table.";
}
or
$sql = "SELECT * FROM photos LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "There are no photos in the photo table.";
}
Try this
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
$result = mysql_fetch_array($query1);
if(empty($result)) {
echo "nothing";
} else {
echo "good";
}
This pretty much sums up the answer for this question: http://www.w3schools.com/php/php_mysql_select.asp
They even provided a sample 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 id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>
Just modify this and you'll be good to go.

Categories