Jump to content

Selecting and retrieving Data from a MySQL Database using PHP and AJAX


Recommended Posts

No, that is node.js, that won't run in a browser. That's totally unrelated to what you're doing. You need to understand what's going on, not add random pieces of code. You're trying to use a variable that doesn't exist. "xmlhttp" is not a reserved word in Javascript, it is a variable name in your code. The variable isn't defined. I couldn't guess why you don't have it defined, other than just the fact that it's not defined where you're trying to use it, but if you show all of your code then maybe I can point it out. In the code on the w3schools tutorial they define it here:

 

        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
Maybe you decided to remove that code, maybe you changed the variable name, maybe it's in a different scope than where you're trying to set the onreadystatechange handler. I can't really guess without seeing your code.

 

 

I was missing one letter in the code. That solved that error issue.

 

 

But now it's back to saying "could not connect" when I select a person from the list.

 

I was advised to use localhost by a staff member from my web host, so maybe this is the issue?

 

 

 

$con = mysqli_connect('localhost','user_Admin','password','my_DB');

if (!$con) {

die('Could not connect: ' . mysqli_error($con));

}

 

That is how I attempt to connect at present.

 

A different PHP file from earlier practice looks like this:

 

 

 

<?php

$servername = 'localhost';

$username = 'user_Admin';

$password = 'password';

$dbname = 'my_DB';

 

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

 

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?>

 

This code works, whereas the other one doesn't. Do you think localhost is the issue, as I had originally used www.servername.com instead of localhost when I wrote the code?

Edited by Coder2345
Link to post
Share on other sites

In your original posting I just noticed you have this

mysqli_select_db($con,"ajax_demo");

Which database has table user? And column names we discussed earlier, 'my_DB' or 'ajax_demo' as they could use different passwords for access.

 

 

I just used "ajax_demo" as an example. My actual database is called something different. My table is also called something different.

 

To illustrate:

 

database = my_DB

table = ajax_demo

column = id

other columns = FirstName, LastName, Age, Hometown, Job

Link to post
Share on other sites
xmlhttp.onreadystatechange = function()

 

 

From some digging around, it was suggested that xmlhttp needs to have a valid value (handle/object) before the line of code above can be executed properly.

 

Does anyone have any suggestions as to what I should include in the code to solve this?

Link to post
Share on other sites

Comment out original sql and use

 

//$sql="SELECT * FROM ajax_demo WHERE id = '".$q."'";

$sql="SELECT * FROM ajax_demo";

 

And add id to record listing

 

while($row = mysqli_fetch_array($result)) {

echo "<tr>";

echo "<td>" . $row['id'] . " - " . $row['FirstName'] . "</td>";

echo "<td>" . $row['LastName'] . "</td>";

echo "<td>" . $row['Age'] . "</td>";

echo "<td>" . $row['Hometown'] . "</td>";

echo "<td>" . $row['Job'] . "</td>";

echo "</tr>";

}

 

Just see if this shows all records

Link to post
Share on other sites

Comment out original sql and use

 

//$sql="SELECT * FROM ajax_demo WHERE id = '".$q."'";

$sql="SELECT * FROM ajax_demo";

 

And add id to record listing

 

while($row = mysqli_fetch_array($result)) {

echo "<tr>";

echo "<td>" . $row['id'] . " - " . $row['FirstName'] . "</td>";

echo "<td>" . $row['LastName'] . "</td>";

echo "<td>" . $row['Age'] . "</td>";

echo "<td>" . $row['Hometown'] . "</td>";

echo "<td>" . $row['Job'] . "</td>";

echo "</tr>";

}

 

Just see if this shows all records

 

 

I tried this but it still says it cannot connect. :(

 

Do you think the data type could be causing it not to work?

 

id = int(6)

FirstName = varchar(20)

LastName = varchar(20)

Age = int(3)

Hometown = varchar(100)

Job = archer(100)

 

Or could it be to do with the xmlhttp not having a valid object?

 

Thank for your help so far. It's appreciated.

Link to post
Share on other sites

If it's saying that it can't connect to the database then either the server, username, or password are wrong. It's nothing to do with the data types or Javascript, it means it can't even connect to the database at all. After the message that it can't connect you're printing the error message from MySQL, so what does that error say?

Link to post
Share on other sites

If it's saying that it can't connect to the database then either the server, username, or password are wrong. It's nothing to do with the data types or Javascript, it means it can't even connect to the database at all. After the message that it can't connect you're printing the error message from MySQL, so what does that error say?

 

 

I was able to locate the error logs in cPanel;

 

 

PHP Warning: mysqli_connect(): (42000/1044): Access denied for user 'user_Admin'@'111.222.333.444' to database 'my_DB' in /home/user/public_html/Example/getuser.php on line 17

 

PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /home/user/public_html/Example/getuser.php on line 19

Link to post
Share on other sites

'user_Admin'@'111.222.333.444' suggests you are using username 'user_Admin' to access server '111.222.333.444' when it should be 'localhost', which then cause and error, which leads to the second error showing.

 

111.222.333.444 will never go above 255.255.255.255

Edited by dsonesuk
Link to post
Share on other sites

'user_Admin'@'111.222.333.444' suggests you are using username 'user_Admin' to access server '111.222.333.444' when it should be 'localhost', which then cause and error, which leads to the second error showing

 

 

I've tried 'localhost' and the servername. I get the same error no matter what.

 

Should I try the IP or domain name?

Link to post
Share on other sites

Try' localhost' then the servername as servername with 'root' as username, you really should gathered this information from webhost as you set up the database.

 

 

You see, both localhost and the servername connect when I use this code in a file;

 

 

 

<html>

<head>

<title>Test Code</title>

</head>

 

<?php

$servername = 'localhost'; //www.example.com also works

$username = 'user_Admin';

$password = 'password'; //not showing real password

$dbname = 'my_DB'; //example DB

 

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

 

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?>

</html>

 

"Connected successfully" is the output. So I'm not sure why the same criteria isn't working in this case.

Link to post
Share on other sites

You keep swapping from one lot of code to another, then just say it works with this? WELL! did you try what I suggested with the problem we are trying to fix with the ajax method????????, cause I really can't tell. OR you know this code works swap with problem code, and change original $con to $conn, if it works problem solved.

Link to post
Share on other sites

You keep swapping from one lot of code to another, then just say it works with this? WELL! did you try what I suggested with the problem we are trying to fix with the ajax method????????, cause I really can't tell. OR you know this code works swap with problem code, and change original $con to $conn, if it works problem solved.

 

 

I'm not switching to any code? I'm letting you know that I have a different file on the same server that works if I use that code. That is the only code for that file. I used it when I first started coding (literally), in order to know if I was able to establish a connection with the server.

 

I will try what you suggested. I'm just very confused as to why that file works and the one we are discussing doesn't. It's the same database and the same user with same password that has access to it. So it should work. The only difference is the table.

 

Thank you for the suggestion. Trying it now.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...