Right now when I run the following query SELECT * FROM table I get the following response
array(1) {
[0]=>
array(36) {
[0]=>
string(5) "31764"
["id"]=>
string(5) "31764"
...
}
...
}
As you can see I am getting 2 of the same data ("0" and "id"). Is there any way I can only get "id" and not "0"?
Check the documentation of mysql_fetch_array(), the second argument allows you to specify what kind of array to return:
The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.
Or you could just use mysql_fetch_assoc().
you can write the sql just like :
SELECT 'id' FROM table
it wil just return
Related
I have a PHP array, that I want to pass over to jQuery and update the rows on the page.
This PHP array is the 'name' of the checkboxes selected on the page. (So this array can be of any length, depending on what the user selects)
PHP Array:
var_dump($sr->conflict_return);
OUTPUT CHECK: array(5) { [0]=> string(33) "hours_9_7_reg_session_102_905_925" [1]=> string(33) "hours_9_7_reg_session_101_905_925" [2]=> string(33) "hours_9_7_reg_session_103_905_925" [4]=> string(33) "hours_9_7_reg_session_104_845_915" [13]=> string(33) "hours_9_7_reg_session_103_845_905" }
This this case... there are '5' elements in my [php] array.
Here is where my problem comes into play...
Sometimes it 'works'... sometimes it doesnt..
The 'key' seems to be what is IN the array:
OUTPUT CHECK: array(3) { [0]=> string(33) "hours_9_7_reg_session_102_845_905" [1]=> string(33) "hours_9_7_reg_session_101_845_905" [2]=> string(33) "hours_9_7_reg_session_104_845_915" }
this seems to work.. 3 items in array.. all 3 rows on stage get highlighted.
this:
OUTPUT CHECK: array(4) { [0]=> string(33) "hours_9_7_reg_session_102_845_905" [1]=> string(33) "hours_9_7_reg_session_101_845_905" [2]=> string(33) "hours_9_7_reg_session_103_845_905" [4]=> string(33) "hours_9_7_reg_session_104_845_915" }
doesnt work... and none of the rows are highlighted
(seems like if there is 4 items in the array it breaks??)
my jQuery to parse the data:
var conflictItems = <?=json_encode($sr->conflict_return); ?>;
//has a conflict list
if(conflictItems.length > 0){
alert("Has conflicts");
//loop through and highlight elements on stage
for(i=0; i<conflictItems.length; i++){
console.log(conflictItems[i]);
$("#sr_table_"+conflictItems[i]+"_row").addClass("conflict_border");
}
}
When I trace (console.log()) the data.... I get odd results.
console.log('CONFLICT ITEMS: ' + conflictItems);
console.log('CONFLICT COUNT: ' + conflictItems.length);
3 x items in array... the above shows:
CONFLICT ITEMS: hours_9_7_reg_session_103_845_905,hours_9_7_reg_session_102_845_905,hours_9_7_reg_session_104_845_915
CONFLICT COUNT: 3
which to me is correct. I have 3 items in my array passed over from PHP..jQuery runs through list and adds a class to each 'row'.
however, when I add a 4th item... the traced output is:
CONFLICT ITEMS: [object Object]
CONFLICT COUNT: undefined
So how? is my array turning into an object?.. and more so WHY??
and how can I fix this? I dont understand why having 3 items in the array works.. but not 4?
Javascript doesn't have a concept of non-sequential array keys (your example has keys 0,1,2 and 4), thus when running json_encode on the array, it converts it to JSON notation for a JS object.
As #Kenney says in the comments, a possible solution is to keep using arrays, but make use of array_values() function which takes your array, and basically regenerates the array dropping the existing keys and using sequential ones.
You're missing index 3 from your array, therefore js turns it into an object.
i am returned an array that looks like this:
array(1) { [0]=> object(stdClass)#176 (1) { ["COUNT(*)"]=> string(1) "1" } }
when var_dump() is executed.
So how would i acceess the count object?
Its
$result[0]->COUNT(*)
but this results in an error?
$result[0]->{COUNT(*)}
does not help either.
Use brackets:
echo $data->{'COUNT(*)'};
But it's about SQL and I recommend to give an alias to your field, like
SELECT COUNT(*) AS records_count FROM t
I have an array like this:
array(2) {
[0]=> array(1) { ["cate_id"]=> string(2) "14" }
[1]=> array(1) { ["cate_id"]=> string(2) "15" }
}
How can I check if the value 14 exists in the array without using a for loop?
I've tried this code:
var_dump(in_array('14',$categoriesId));exit;
but it returns false, and I do not know why.
I wonder why you don't need a for. Well a quickest way would be to serialize your array and do a strpos.
$yourarray = array('200','3012','14');
if(strpos(serialize($yourarray),14)!==false)
{
echo "value exists";
}
Warning : Without using looping structures you cannot guarantee the value existence inside an array. Even an in_array uses internal looping structures. So as the comments indicate you will get a false positive if there is 1414 inside the $yourarray variable. That's why I made it a point in the first place.
If you need to find a specific value in an array. You have to loop it.
Do this :
var_dump(in_array("14",array_map('current',$categoriesId))); //returns true
I've got a function that gets data from a NoSQL database, and if it's not available then it goes to a MySQL database to get the data but the issue is, the function is putting the data into the array twice and I can't figure out why.
Expected result
array(2) {
["id"]=> string(2) "30"
["username"]=> string(8) "Username" }
Actual result
array(4) {
[0]=> string(2) "30"
["id"]=> string(2) "30"
[1]=> string(8) "Username"
["username"]=> string(8) "Username" }
Code
Code that is irrelevant to the problem is removed and replaced by pseudo code comments.
$Connection = $this->Connect("MySQLi");
$Data = MySQLi_Fetch_Array(
MySQLi_Query($Connection["MySQLi"], $Options["Query"])
);
echo "Got array (MySQLi).";
It's worth noting that the string "Got array (MySQLi)." only appears once.
MySQLi_Fetch_Array gets duplicated data in one array - with numerical and associative indexes at the same time
Use mysqli_fetch_assoc instead to have only associative
mysqli_fetch_array() takes a parameter, resulttype, which by default is set to MYSQLI_BOTH.
By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.
mysqli_fetch_assoc fetches an associative array, while mysqli_fetch_row fetches an array with numeric indexes.
mysqli_fetch_array with the parameter MYSQLI_BOTH will fetch both named (associative) indexes and numeric indexes in the same array.
update
how can I retrieve this value? I need to do that if I will write the value to my database.
array(3) {
[1]=> NULL
[2]=> array(2) {
[123]=>
int(123)
[122]=>
int(0)
}
[3]=> NULL
}
There is something missing in your output. I assume it looks something like:
// var_dump($array);
array(1) {
[0]=>
string(2) "39"
}
so you can access the value with $array[0]. Simple array access.
As arrays are the most important data structure in PHP, you should learn how to deal with them.
Read PHP: Arrays.
Update:
Regarding your update, which value do you want? You have a multidimensional array. This is what you will get:
$array[1] // gives null
$array[2] // gives an array
$array[2][123] // gives the integer 123
$array[2][122] // gives the integer 0
$array[3] // gives null
Maybe you also want (have) to loop over the inner array to get all values:
foreach($array[2] as $key => $value) {
// do something with $key and $value
}
As I said, read the documentation, it contains everything you need to know. Accessing arrays in PHP is not much different than in other programming languages.
The PHP manual contains a lot of examples, it is a pretty could documentation. Use it!
If your array is referenced as $myArray, you can get the string 39 via $myArray[0], i.e., this zeroth item.