Count number of values when using AJAX

Ye its there, I just didn’t add it in sorry.

Ye am using var_dump and its showing the correct value to the variable $username and all looks fine there.

This is it now

<?php
if (isset($_POST["username"]))
{	
require_once('config.php');
$connectionInfo = array( "Database"=>$databaseName);
$conn = sqlsrv_connect($hostName , $connectionInfo);
$username =  $_POST["username"]; 
var_dump ("SELECT COUNT(*) AS Counting from Users where Username='$username'");
$query = "SELECT COUNT(*) AS Counting from Users where Username='$username'";

$fathom = $query['Counting'];
echo $fathom;

$res = sqlsrv_query($conn, $query) or die(sqlsrv_errors());
echo $res."lui";
$username_exist = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);	
$rowID=$username_exist[0];

//if returned value is more than 0, username is not available
if($rowID) {
die('<img src="images/not-available.png" />');
} else { ?>
<script>
$('#submitNew').css('visibility', 'visible');
</script>
<?php die('<img src="images/available.png" />');
}
}
?>

$fathom is echoing the letter ‘s’
$res is echoing nothing

it should also give you an “invalid string offset” warning.

$res is a resource (if it makes it past the die()). and sqlsrv_errors() either returns NULL (no error) or an array (of errors), not a string.

I don’t understand what you expect to see in $fathom as you haven’t executed the query at that point. $query is just a string, so trying to access it as an indexed array doesn’t seem correct to me.

var_dump($username_exist) would be my next suggestion, see why $rowID is blank.

Well this is my problem, I don’t seem to be able to get anything to echo out after

$res = sqlsrv_query($conn, $query) or die(sqlsrv_errors());

With $fathom, shouldn’t I be able to get the value of count, or am I miles off.

you have to query the database for that. a simple string cannot tell you that.

Have changed it a bit and made a bit of progress but its still not working and am trying to echo out $rowID and again nothing.

<?php
if (isset($_POST["username"]))
{	
require_once('config.php');
$connectionInfo = array("UID" => $username, "PWD" => $password, "Database" => $databaseName);
$conn = sqlsrv_connect($hostName, $connectionInfo);

$username =  $_POST["username"]; 
$query = sqlsrv_query($conn,"SELECT COUNT(*) AS Upturn from Users where Username=".$username."");

while ($username_exist = sqlsrv_fetch_array($query)){
$rowID=$username_exist['Upturn'];
echo $rowID;

}
//if returned value is more than 0, username is not available
if($rowID) {
die('<img src="images/not-available.png" />');
} else { echo $rowID."house"; ?>

<script>
$('#submitNew').css('visibility', 'visible');
</script>
 <?php die('<img src="images/available.png" />');
 }
}
?>

Bing looks like I got it finally

<?php
if (isset($_POST["username"]))
{	
require_once('config.php');
$connectionInfo = array("UID" => $username, "PWD" => $password, "Database" => $databaseName);
$conn = sqlsrv_connect($hostName, $connectionInfo);
$username =  $_POST["username"]; 
$query = sqlsrv_query($conn,"SELECT COUNT(*) AS Upturn from Users where Username='".$username."'") or die(sqlsrv_errors());
while ($username_exist = sqlsrv_fetch_array($query)){
$rowID=$username_exist['Upturn'];
}
if($rowID) {
die('<img src="images/not-available.png" />');
} else { 
?>   
<script>
$('#submitNew').css('visibility', 'visible');
</script>
<?php die('<img src="images/available.png" />');
}
}
?>

Thanks for the help guys

notes:

  • sqlsrv_errors() does not return you a string like mysql_error() does.
  • the while() loop is not necessary, an aggregate function always makes the query return at least one result
  • you’re highly susceptible to SQL Injection
  • I still think that you should not return all those HTML/JS code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.