I'm trying to return the first row of a table in a MySQL database using a php function called on click (with AJAX).
I have been experimenting with the PHP list function to return the row as an array.
At the moment it isn't doing anything and I'm wondering what I've done wrong.
Here's the PHP function called getfacebook.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$bname = $_REQUEST["bname"];
$link = mysqli_connect('localhost', 'root', '12345');
$servername = "localhost";
$username = "root";
$password = "12345";
$dbname = "success";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// PHP for execution
$sql = "SELECT id, bname, bicon, rafrica, rasia, roceania, reurope, rsouthamerica, rnorthamerica, traffic, revenue, profit FROM business LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$b3name = $row["bname"]. "<br>";
$b3icon = $row["bicon"]. "";
$b3rafrica = $row["rafrica"]. "<br>";
$b3rasia = $row["rasia"]. "<br>";
$b3roceania = $row["roceania"]. "<br>";
$b3reurope = $row["reurope"]. "<br>";
$b3rsouthamerica = $row["rsouthamerica"]. "<br>";
$b3rnorthamerica = $row["rnorthamerica"]. "<br>";
$b3traffic = $row["traffic"]. "<br>";
$b3revenue = $row["revenue"]. "<br>";
$b3profit = $row["profit"]. "<br>";
}
} else {
echo "0 results";
}
list($b3name,$b3icon,$b3traffic);
?>
</body>
</html>
Here's the button that executes the AJAX function to call getfacebook.php:
function loadfacebook1()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("b1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getfacebook.php",true);
xmlhttp.send();
}
And finally, this is what the AJAX is changing on the page:
<center><h3><strong><u>Business 1</u></strong>: <span id="b1"></span></h3></center>
The purpose of all this isn't to just change a heading, I'm just using it to test that the returning values are correct.
After it successfully returns the row from the table I want to assign each of them to a PHP variable so that I can manipulate the styling of an SVG (which thankfully I've already got working).
I am totally new to all of this as of yesterday (I'm a designer not a developer, as you have probably guessed by my awful knowledge) so if I'm over complicating things massively please put me out of my misery! I have a feeling I'm making things more difficult than they need to be.
Cheers,
Will
mysqli_connectandnew mysqli()? They do the same thinglist()construct does, but it's not right.list()can be used to "decompose" an array, assigning multiple variables at the same time, e.g.$my_array = [1, 2, 3]; list($foo, $bar, $baz) = $my_array; echo $foo;Without anything to assign from, it does nothing.whileloop is all wrong. You're overwriting the variable with each row, not outputting all the rows.