Jump to content

Using MySQL and PHP Query


will71110

Recommended Posts

Hello. I am experimenting with PHP and MYSQL because I am new at it. I am trying to use AJAX to create a query into a database that I have. It isn't working. I'm sure I've made a mistake but I cant seem to find it. Someone please look over my code and see if there is something I am missing. I know my database is working because I use it to log in. It has to be in my code. The results I get from this is blank. The AJAX does call on the PHP correctly because I do get an echo back, but only because the $results isn't working. query/users.php

<?phpinclude('includes/config.inc');$sql = "SELECT * FROM user";$result = mysql_query($sql,$con);if($result){echo "Username <br>";}else{echo "Nothing Found: ". mysql_error();}while($row = mysql_fetch_array($result)){echo $row['username'];echo "<br>";}mysql_close($con);?>

/includes/config.ini //Real info has been removed for security :)

<?php$hostname = 'hostname';$dbname = 'databank';$username = 'username';$password = 'password';$con = mysql_connect($hostname, $username, $password) or DIE('Connection failed');mysql_select_db($dbname, $con) or DIE("Database isnt available");?>

admin.php

<?phpsession_start();include('includes/config.inc');if (!isset($_SESSION['username'])) {header('Location: index.php');}?><script type="text/javascript">function q(url){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("q").innerHTML=xmlhttp.responseText;	 }   }xmlhttp.open("GET",url,true);xmlhttp.send();}</script><HTML><head><link rel="stylesheet" type="text/css" href="../css/secure.css"><title>Admin Page</title></head><body><table>  <tr>   <td class='seven'></td>   <td class='fi'>Welcome: <?php echo $_SESSION['username']?>   </td>   <td class='fi'><a href="logout.php">Logout</a></td>  </tr></table>This is the ADMIN page<br><input value="Get Users" type="submit"  onclick="q('query/users.php')"><div id="q"></div></body></HTML>

Edited by will71110
Link to comment
Share on other sites

Check you php first. Make sure it's working (use dummy inputs). Then, use alerts to check the javascript. Then, report back if needed.

Edited by niche
Link to comment
Share on other sites

You don't even need to use dummy inputs. Just try loading query/users.php directly into your browser. If you get a blank screen, it's broken. You can test the ajax side by connecting to a script that echoes "hello" and nothing else. EDIT: This is probably not your problem, but you should move your javascript into the head element of your document. Add a doctype, too.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Thank you for your replies. There are dummy inputs. I get the statement "Nothing found" returned from the users.php page through the AJAX because of the echo "Nothing Found: ". mysql_error();. For some reason $results isn't getting the SQL query. I'm also suppose to see the what the SQL error, but that doesn't seem to output anything. It seems like mysql functions aren't doing what they are suppose to.

Link to comment
Share on other sites

WOW.... OK, figured it out. My problem was that I wasn't pulling the config file from the parent folder. I needed to do ../includes/config.inc. I was missing the ../ . Good to go now

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...