Mysql File:
<?php
function query($sql, $array) {
$servername = "localhost";
$username = "verifyUser";
$password = "test";
$dbname = "verify";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query($sql);
$stack = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($stack, $row);
if(!$array) {
return $row;
}
}
return $stack;
}
$conn->close();
}
?>
My php file:
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Serial</th>
</tr>
<?php
include_once("mysql.php");
$result = query("SELECT ProductId, Name, SerialId FROM product", true);
while ($row = mysqli_query($result)) {
echo "<tr><td>" . $row['ProductId'] . "</td><td>" . $row['Name'] . "</td><td>" . $row['SerialId'] . "</td></tr>";
}
?>
</table>
Output:
Id Name Serial
This is my first day trying to do anything with php. So go easy on me :)
I've been following different tutorials and looking at the php documentation, but I have not be able to get my table to print anything besides the table headers.
I don't think my query is wrong, because I return results in workbench
https://gyazo.com/ab588fa7b7498444a5e8dcd1b172f315
<?php
$table='<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Serial</th>
</tr>';
$servername = "localhost";
$username = "";
$password = "";
$dbname = "verify";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query($sql);
$stack = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($stack, $row);
if(!$array) {
return $row;
}
}
}
$conn->close();
$table_content='';
foreach($stack as $item)
{
$table_content.= "<tr>
<td>" . $row['ProductId'] . "</td>
<td>" . $row['Name'] . " </td>.
<td>" . $row['SerialId'] . "</td>
</tr>";
}
echo $table.$table_content.'</table>';
?>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Serial</th>
</tr>
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "verify";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query($sql);
$stack = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($stack, $row);
if(!$array) {
return $row;
}
}
}
$conn->close();
foreach($stack as $item)
{
echo "<tr><td>" . $row['ProductId'] . "</td><td>" . $row['Name'] . "</td>. <td>" . $row['SerialId'] . "</td></tr>";
}
?>
Related
I'm a bit new to MySQL and I am trying to create a search system. I am using some simple PHP code from w3schools.
I connect to my database:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "img";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then echo the results.
$sql = "SELECT ID, Name, Description, Path FROM system";
$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["Name"]. " " . $row["Description"]. " " . $row["Path"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My database contains one entry, but when I run this code, "0 Results" is echoed to the screen. Is there something I am doing wrong here?
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.
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.
I have a mysql database. There are categories in Db for my articles. I want to fetch these categories and display them as a list (side navigation). I was able to establish connection and print the categories but can't put them into the list.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voltage";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$sql = "SELECT ID, NAME FROM categs";
$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["NAME"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I know it's a dumb question, but I'm PHP newbie.
<ul>
<li>Categ 1</li>
<li>Categ 2</li>
<li>Categ 3</li>
</ul>
Thanks in advance.
you nearly got it by yourself... try this one:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voltage";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$sql = "SELECT ID, NAME FROM categs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<ul>";
while($row = $result->fetch_assoc()) {
echo "<li>" .$row["NAME"]. "</li>";
}
echo "</ul>";
} else {
echo "0 results";
}
$conn->close();
?>
replace
echo "ID: " . $row["ID"]. " " . "Name: " . $row["NAME"]. "<br>";
with
echo "<li>" . $row["NAME"]. "</li>";
I have a foreach to elaborate some values from my web site and I'd like to insert them into a mysql database.
My problem is where I have an array, because I don't know how to treat them in this case.
This is my code:
...
$titles = $html->find("a[class=in-match]"); // 1 per match
$result = $html->find("td[class=h-text-center]/a"); // 1
$best_bets = $html->find("td[class=table-matches__odds colored]/span/span/span"); // 1
$odds = $html->find("td[class=table-matches__odds]"); // 2
function print_odd($odd) {
if (array_key_exists('data-odd', $odd->attr)) {
return $odd->attr['data-odd'];
}
return $odd->children(0)->children(0)->children(0)->attr['data-odd'];
}
$c=0; $b=0; $o=0; $z=0; // two counters
foreach ($titles as $match) {
list($num1, $num2) = explode(':', $result[$c++]->innertext); // <- explode
$num1 = intval($num1);
$num2 = intval($num2);
$num3 = ($num1 + $num2);
if ($num3 > 1) {
$over15 = "OK";
} else {
$over15 = "NO";
}
echo "<tr><td class='rtitle'>".
"<td class='first-cell'>".$match->innertext."</td> ".
"<td>".$match->innertext."</td><td> ".$num1.'</td><td> : </td><td>'.$num2 . " / " . // <- example use
"<td class='first-cell'>".$num3 ."</td> " .
"<td class='first-cell'>".$over15 ."</td> " .
"<td class='odds'>".print_odd($odds[$b++]) . ";" .
"".print_odd($odds[$b++]) . ";" .
"".print_odd($odds[$b++]) . "</td>" .
"</td></tr><br/>";
$servername = "xx.xxx.xxx.xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO risultati (titles, scorehome, scoreaway, best_bets)
VALUES ('$match', '$num1', '$num2', '$odds');";
if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
When I run this, I don't get that 3 "$odds"
Thank you for your attention
EDIT So, I saved almost all data with my code but I have a problem with $odds (You can see my function print_odd). How could to save them?
Try the concept... Thanks
$columns = implode(", ",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values = implode(", ", $escaped_values);
$sql = "INSERT INTO `fbdata`($columns) VALUES ($values)";