Problem getting results with php and mysqli - php

I've a problem by running this php script:
<?php
$link = mysqli_connect("localhost", "root", "*******", "adsb");
/* check connection */
if ($conn->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$query = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id='2414';";
$query .= "SELECT aircrafts.id, plane.reg, plane.hex, plane.typ, plane.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `plane` ON aircrafts.hex=plane.hex WHERE aircrafts.id='2414'";
$result = mysqli_multi_query($query);
/* execute multi query */
if ($result->num_rows > 0) {
echo "<h1>INFO ABOUT FLIGHT RECORD " . $id . "</h1>";
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
/* close connection */
mysqli_close($link);
When I run the 2 query's in phpmyadmin I get 1 result.
(When I run 1 query on my site I get a result too, see code at the bottom of this post)
But when I run it on my site it's shows "0 results".
So... There must be a fould in the $result section.
Who can help? :-)
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "adsb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id=2414";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();

This is probably your specific problem.
$result = mysqli_multi_query($query);
Here is the PHP Doc
You need to establish the connection.
$result = mysqli_multi_query($link, $query);
Like the others mentioned in the comments, I would refactor this code.

Related

How do i set up a remove button to delete a row from a table that is on MySQL?

I'm new to PHP and MySQL . I'm using PHP to display table contents and I added a button that will remove a row once it's clicked. I'm can't get the button to use delete.php to remove a row from the table and from the database. Any suggestions ?
Thank you
shopping-bag.php
<table>
<tr>
<th>ITEM</th>
<th>MODEL</th>
<th>QUANTITY</th>
<th>COLOR</th>
<th>PRICE</th>
</tr>
<?php
$conn = mysqli_connect(
"localhost",
"user",
"pass",
"db"
);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT productname, productmodel, productquantity,
productcolor, productprice FROM posts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["productname"] . "</td><td>" .
$row["productmodel"] . "</td><td>"
. $row["productquantity"] . "</td><td>" .
$row["productcolor"] . "</td><td>"
. "$" . $row["productprice"] . "</td><td>" . "<input type='button' name='removerow' action='delete.php' value='REMOVE'>" . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</table>
delete.php
<?php
$host = "localhost";
$dbusername = "user";
$dbpassword = "pass";
$dbname = "db";
//Connect
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);
//DELETE query
if (mysqli_connect_error()) {
die('Connect error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
} else {
$sql = "DELETE FROM posts WHERE ID = '$_GET[id]'";
if (mysqli_query($conn, $sql)) {
header("refresh:1; url=shopping-bag.php");
} else {
echo "NOT DELETED";
}
}
?>
In shopping-bag.php, you need to retrieve ID in your loop over the SELECT statement results, and put it as an argument to action='delete.php?id=$id'.
Also recommend reading cross-site scripting and SQL injection so you can avoid these common vulnerabilities in your code.

Multiple if else in mysql query

I have re-written this question because of all the down votes and it would seem that either no one understood the question or was unwilling to help a newbie. So I'll as it this way: Can someone tell/show me why this code does not work? (500 error)
OR... at the very least point me in the right direction? I AM WILLING TO LEARN, I just don't know where to begin or who/where to learn from as I am not sure what to even ask other than how do you run multiple "else" statements... that however left me more confused than I already am though!
Any "HELP" would be greatly appreciated!
<?php
$servername = "localhost";
$username = "****";
$password = "***";
$dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$bname = $_POST['bname'];
$baddress = $_POST['baddress'];
$bcity = $_POST['bcity'];
$bstate = $_POST['bstate'];
$zipcode = $_POST['zipcode'];
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Login FROM `USERS` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/realsite.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "SELECT Login FROM `DATA` WHERE Business_Name = '" . $_POST["bname"] . "' AND Business_Address = '" . $_POST["baddress"] . "' AND Business_State = '" . $_POST["bstate"] . "' AND Business_Zip = '" . $_POST["zipcode"] . "' LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc())
{
echo "https://www.***.com/demo.php?Login=" . $row["Login"] . "";
}
}
else
{
$sql = "INSERT INTO `DATA` (Business_Name, Business_Address, Business_City, Business_State, Business_Zip)
SELECT '$bname', '$baddress', '$bcity', '$bstate', '$zipcode' FROM (SELECT 1) t
WHERE NOT EXISTS (SELECT Login, Business_Name,Business_Address FROM `DATA` WHERE Business_Name='$bname' AND Business_Address='$baddress')";
if (mysqli_query($conn, $sql))
{
echo "<a href='https://www.servedwell.com/realsite.php?Login=" . $row["Login"] . "'>LINK</a>";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>
Credit goes to #Ancaron for properly formatting code and correcting a syntax error. Final posted code works fine.

PHP - Echo'ing something from a MySQL DB [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I think, I might need help with my PHP code...
I'm trying to echo the info in a MySQL database and it gives me the Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /usr/local/ampps/www/php/db.php on line 18
My code is:
<?php
$servername = "blabla"; //changed, connecting works
$username = "blabla";
$password = "blabla";
$database = "blabla";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = mysql_query("SELECT * FROM Schueler");
while($data = mysql_fetch_array($sql)) //This is line 18...
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
$conn->close();
?>
Would be nice if somebody could help me :)
Edit:
I fixed it using MySqli only, this is my working code now:
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = "SELECT ID, Vorname, Nachname FROM Schueler";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["ID"]. " - Name: " . $row["Vorname"]. " " . $row["Nachname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Thanks for the quick advice.
You have used mysqli for your connection setup, then you used simple mysql functions to get the resultset and the fetch_array logic. You need to be uniform in this case.
I have changed the mysql_query() call to mysqli_query() call, and similarly the mysql_fetch_array() call to mysqli_fetch_array() call.
The final code becomes:
$sql = mysqli_query("SELECT * FROM Schueler", $conn);
while($data = mysqli_fetch_array($sql))
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}

Printing MySQL data to textbox

I apologize before I speak English very little I want to write all of the data in textboxes, but I could not do it
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "vipser";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("SELECT * FROM `a_info` WHERE `pax`='" . $_GET['pax'] . "' and `nereden`='" . $_GET['durum'] . "' and `nereye`='" . $_GET['gdurum'] . "'");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
while ($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['pax'] . "</td>";
echo "<td>" . $row['a_cinsi'] . "</td>";
echo "<td>" . $row['nereden'] . "</td>";
echo "<td>" . $row['nereye'] . "</td>";
echo "<td>" . $row['saat'] . "</td>";
echo "<td>" . $row['km'] . "</td>";
echo "<td>" . $row['fiyat'] . "</td>";
echo '<td><img src="' . $row["a_resmi"] . '" width="75" height="75"/></td>';
echo "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$conn->close(); ?>
Store the data of the loop in a variable instead of echoing and then assign the variable into textbox's value. Done this here:
<?php
$str = "";
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$str .= . $row['pax'] . " " . $row['a_cinsi'] . " " . $row['nereden'] . " " . $row['nereye'] . " " . $row['saat'] . " " . $row['km'] . " " . $row['fiyat'] . " ";
}
}
?>
<input type="text" value="<?= $str ?>" />
You can put this input inside while if you want different textboxes for each row.

Why am I getting white spaces from this function?

I made a function that loops through all the posts in database and echo them. But there's a slight problem. I am getting whitespace between the name (title of the post) and the content, its like two lines or so. I want reason for this and solution. Thanks.
function read_all_posts(){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$query = "SELECT * FROM posts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<br>" . $row['content'] . "<br />" . "<i>" . $row['author'] . "</i>";
echo "<br>";
}
}
try,
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<p>" . $row['content'] . "</p>" . "<p><i>" . $row['author'] . "</i></p>";
}

Categories