0

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

17
  • Why do you use both mysqli_connect and new mysqli()? They do the same thing Commented Apr 3, 2015 at 22:43
  • I think I accidentally forgot to remove one or the other when I decided to change my approach. I'll edit that now but it won't change anything as I don't think the connection to MySQL is the problem. I think the problem is that I can't return multiple variables from one PHP function. Thanks Commented Apr 3, 2015 at 22:46
  • You should first open your browser Developer tools and check for JS errors and do you send a request at all. If you send a request check what parameters you send etc. Commented Apr 3, 2015 at 22:46
  • I'm not clear what you think the list() 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. Commented Apr 3, 2015 at 22:47
  • 1
    Your while loop is all wrong. You're overwriting the variable with each row, not outputting all the rows. Commented Apr 3, 2015 at 22:48

1 Answer 1

1

The list() function is not what you are looking for. You want to use JSON or XML.

To produce JSON output you put all your values in an array and then output it with json_encode():

$output = array(
    'name' => $b3name,
    'icon' => $b3icon,
    'traffic' => $b3traffic
);
echo json_encode($output);

You may need to make sure you have the headers set appropriately as well.

Sign up to request clarification or add additional context in comments.

4 Comments

Perfect, thank you! I thought I was doing something massively wrong. How would I then display the three variables from the $output array as independent variables in HTML? Thanks again
Take a look at your xmlhttp.responseText with console.log.
See examples on this page - stackoverflow.com/questions/12359047/…
you are a savior. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.