Jump to content

Need Help In Understanding Php Code


nitesh

Recommended Posts

Hello everyone i want to know about the php code highlighted in bold black text.Thanks in advanced.

if(isDuplicate($country, "Countries", "Country", "", "") == "No")   {	/* Building an SQL query that determines the next highest value for primary key generation */	 $query = "SELECT MAX(CountryNo) + 1 AS 'CountryNo' FROM Countries";	/* Executing the query using the ResultSet object */	 $rsQuery = dbQuery($query);	/* Fetching a result row as an associative array */	 $arrData = dbFetchArray($rsQuery);  [b]   if($arrData['CountryNo'])[/b][b]	   $countryno = $arrData['CountryNo'];[/b]	  	 $qryInsert= "INSERT INTO Countries (CountryNo, Country) VALUES ($countryno, '$country')";	 $rsInsert= dbQuery($qryInsert);	 echo $rsInsert;	 header("Location: " . DIRSERVERNAME . "php/adminManageCountries.php");   }

Link to comment
Share on other sites

When i use dbFetchArray it works but when i use dbFetchRow it doesn't work we are receiving one value then why dbFetchRow not working.$arrData = dbFetchRow($rsQuery);$arrData = dbFetchArray($rsQuery);Below is my function for dbFetchRow and dbFetchArray/* Function to return associative array corresponding to the fetched rows. */ function dbFetchArray($strResult) { /* Retrieving all the records in associative array by invoking mysqli_fetch_array() and returning the associative array. */ return mysqli_fetch_array($strResult, MYSQL_ASSOC); }/* Function to return a single row. */ function dbFetchRow($strResult) { /* Retrieving a row by invoking mysqli_fetch_row() and returning the record. */ return mysqli_fetch_row($strResult); }

Link to comment
Share on other sites

mysql_fetch_array() is returning both associative and numerical array where mysql_fetch_row() returns enumrated array means numeric indicies. where you are trying to use associative array$arrData['CountryNo']thats why it is not working

Link to comment
Share on other sites

When i use $arrData = dbFetchArray($rsQuery); then it works when i use $arrData = dbFetchRow($rsQuery); then i did't it should work with $arrData = dbFetchRow($rsQuery); because only 1 value is passing from(For code please check my first post on this topic)

Link to comment
Share on other sites

$arrData = mysqli_fetch_array($strResult, MYSQL_ASSOC);

when you use this you the data from the database is available in both as associative array eg $arrData['columnNameFromdb'] where

$arrData=mysqli_fetch_row($strResult);

will return enumareated array starting from 0. like $arrData[0] for first column $arrData[1] for the next..and so on so when you are trying to check in if sttrcuture ,using mysqli_fetch_row($strResult)

if($arrData['CountryNo'])

$arrData['CountryNo'] doess not even exist. You are just using actualy mysqli_fetch_array() and mysqli_fetch_row9) in both function so there is no need to wrap it in another function. it will confused you. more reference for those function herehttp://php.net/mysql-fetch-arrayhttp://php.net/mysql-fetch-row

Link to comment
Share on other sites

Still confusedonly 1 value is coming from$query = "SELECT MAX(CountryNo) + 1 AS 'CountryNo' FROM Countries"; $rsQuery = dbQuery($query); $arrData = dbFetchArray($rsQuery); then why $arrData = dbFetchArray($rsQuery); is working$arrData = dbFetchRow($rsQuery); not adding value in Array if we can fetch 1 Numeric array value from dbFetchRow($rsQuery) then why we should use dbFetchArray($rsQuery)

Link to comment
Share on other sites

No, dbFetchRow is not the function you should be using. Whether you're using one value or many, when you want a string as an identifier, you must use dbFetchArray. The comments in your code are wrong. Both mysqli_fetch_row() and mysql_fetch_array() return one single row from the database.

Link to comment
Share on other sites

I think you might be missing a critical word we mentioned a few times - key.When you use mysqli_fetch_row(), you get an array that has the column index number as a key and the value at that column as an array value.When you use mysql_fetch_array(), you get an array that has the column name as a key and the value at that column as an array value.

Array ( [CountryNo] => 4 ) 

means that you have an array with one key (column) called "CountryNo", and the value at that column is "4".

Link to comment
Share on other sites

Hi can u give me some example or may be some link forhen you want a string as an identifier, you must use dbFetchArray.
what is ingolme meant by identifier is the 'key'. key is that which is used to point some element in an array. it could be either numerical or string. when its numerical its called enumrated array and when its string its called associatve array. what is difference in both could be more understandable if you follow what ingolme already said use print_r($array) . it will let you see what we saying. use that function in both case after mysqil_fetch_array() and mysqil_fetch_row(). and see how it differes from eachother. you may like to look into array also...http://w3schools.com/php_array.asphttp://php.net/language.array
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...