mysql_query doesn't show all query - php

I want to show all distinct id_did in cc_did_use
function getdidbycc($cccard_from_sipcard){
$result = mysql_query("SELECT id_did from cc_did_use where id_cc_card='$cccard_from_sipcard'");
if ($row = mysql_fetch_array($result))
{
$text = $row['id_did'];
}
return $text;
}
I have two id_cc_card="31" and one that value has id_dd="14" and another one = "13"
but result just show first one.
How I can show both of them?
and after that I have another function
function get_did ($p){
$result = mysql_query("SELECT did FROM cc_did WHERE id ='$p'");
while ($row = mysql_fetch_array($result))
{
echo $row['id_did'].'<br/>';
}
}
when I run getdidbycc function , it returns two value , 13 and 14 ,
How I can get did numbers from this two values?

You are fetching result and checking in an if condition which will execute only once.
if ($row = mysql_fetch_array($result))
{
$text = $row['id_did'];
}
You have to fetch the result until there is value in the resultant array. So try with while loop like,
while($row = mysql_fetch_array($result)) // while there are records
{
$text[] = $row['id_did']; // store the result in array $text
}
return $text; // return the array

First of all try to use mysqli or pdo instead of mysql otherwise you will face issues in updated version of php
As per your code $text value is gets over write due to loop so use an array something like this
if ($row = mysql_fetch_array($result)) {
$text[] = $row['id_did'];
}
return $text;
or you can just return complete data as return $row;

if ($row = mysql_fetch_array($result))
{
$text = $row['id_did'];
}
change the above line to this
while($row = mysql_fetch_array($result))
{
echo $row['id_did'].'<br/>';
}

Use a while loop like that : http://php.net/manual/en/function.mysql-fetch-array.php
Mysql fetch array returns the current element only

if ($row = mysql_fetch_array($result))
{
$text[] = $row['id_did'];
}
return $text; // returns both

Related

Why is the array is always empty at this point despite the fact that I added data there?

$result = mysqli_query($con, "SELECT * FROM users");
$usersArray=[];
tableArrayPushData($result, $usersArray);
function tableArrayPushData($result, $tableArray){
while ($row = $result->fetch_assoc()) {
$str = '';
foreach ($row as $value) {
$str = $str.$value.'|';
}
$newStr = rtrim($str, "| ");
array_push($tableArray,$newStr);
}
}
for ($i=0; $i<count($usersArray); $i++){//array is always empty at this point
echo "Ok";
echo "<br>";
}
I don't understand why, but usersArray is empty despite the fact that I added data there.
The MySQL table has rows with data, so it can't be empty.
You should use the & operator to allow the function to access the outer variable, like this:
function tableArrayPushData($result, &$tableArray) {}
Or use return.

Database Query into Associative Array using PHP

I am trying to grab data from my database and place in a format so I can call field1 and return field2.
In my mind I think this:
while($row = mysqli_fetch_assoc($result)) {
$aa = $row["field1"];
$bb = $row["field2"];
}
$cc = array(“$aa”=>”$bb”);
Will compute this:
$cc = array(
"Row1a"=>"Stuff in field2 row1b",
"Row2a"=>"Stuff in field2 Row2b",
"Row3a"=>"Stuff in field2 Row3b",
"Row4a"=>"Stuff in field2 Row4b",
);
After this I will be able to:
echo $cc('Row1a');
To display:
Stuff in field2 row1b
Please try this and let me know if it meets your requirements
<?php
$result=NULL;
while($row = mysqli_fetch_assoc($result)) {
$aa = $row["field1"];
$bb = $row["field2"];
$result[$aa]=$bb;
}
echo $result['Row1a'];
?>
Edited code
Then this should meet your requirement
<?php
$search=$_POST['userText'];
$query= "SELECT * FROM table WHERE field1='".$search."';";
$result=mysql_query($query,$con);
$output=NULL;
if(mysql_num_rows($result)>0) //to check if at least 1 match found
{
$array=mysql_fetch_array($result);
$output=$array['field2'];
}
if(isset($output))
echo $output; // can be returned as needed
else
echo 'No match found';
?>
Here's one way. If there aren't an even number of elements in $row then the odd last one will be set to Last. But obviously this overwrites $result each time so you probably want a multidimensional array using a counter and $result[$i] or some such:
foreach(array_chunk($row, 2) as $pair) {
if(isset($pair[0], $pair[1])) {
$result[$pair[0]] = $pair[1];
}
}
if(isset($pair[0])) {
$result['Last'] = $pair[0];
}

PHP Array not being delivered

I have some data in a database column called "gcmregid".
I access this data by calling:
public function getAllUsers() {
$r = mysql_query("select * FROM table");
while ($row = mysql_fetch_array($r)) {
$result = array(
'gcmregid' => $row["gcmregid"]
);
}
return $result;
}
Correct me if I'm wrong, but this should deliver an array, due to result = array?
I took this logic from here: Getting Resource id #3 Error in MySql
.Then I thaught using a loop would be helpful, such as:
$userCount = $db->getUserCount(); // counting said table
$registation_ids = array(); // again create array
for($i=0; $i < $userCount; $i++)
{
$gcmRegId = $db->getGCMRegID($selUsers[$i]);
$row = mysql_fetch_assoc($gcmRegId);
//Add RegIds retrieved from DB to $registration_ids
array_push($registation_ids, $row['gcmregid']); // this creates an array consisting of gcmregid s ?
}
It doesn't work either.
Any input would be really appreciated right now...
Thanks
I'm not sure what's going wrong, but if the problem is that it's returning a single item: that's because you keep making a new array with every iteration, discarding the old one. If you want to collect all the rows, make a new array outside of the loop and then add the results to it:
public function getAllUsers() {
$r = mysql_query("select * FROM table");
$result = array();
while ($row = mysql_fetch_array($r)) {
$result[] = array (
'gcmregid' => $row["gcmregid"]
);
}
return $result;
}
You should consider working more on how you name your functions. If you are trying to get gcmregid, the method should not say getAllUsers. Anyway, did you try the following:
public function get_gcmregid() {
$query_result = mysql_query("select * FROM table");
$row = mysql_fetch_array($query_result);
return $result[0]['gcmregid'];
}
OR if you are trying to get all gcmregid in one shot:
public function get_gcmregid() {
$query_result = mysql_query("select * FROM table");
$i=0;
while ($row = mysql_fetch_array($query_result)){
$result[$i++] = $row['gcmregid'];
}
return $result;
}

Rewrite mysqli_fetch_all() to get the same result

Since the servers version is older than 5.3.0, I need to rewrite the following piece of function to do the same as it does now:
else {
$res = mysqli_query($_con, "SELECT * FROM house");
$row = mysqli_fetch_all($res, MYSQLI_ASSOC);
return $row;
}
In html I call it like this:
$results = getResults();
foreach ($results as $value) {
echo $value['Title']." / "; echo $value['Version'];
}
How can I call the results in my html the same way but with different function?
EDIT: I want to get all of the results from table "house" but without the use of function mysqli_fetch_all()
It's just a simple loop that calls mysql_fetch_array() and collects all the rows in an array.
function mysqli_fetch_all($res, $mode) {
$array = array();
while ($row = mysql_fetch_array($res, $mode)) {
$array[] = $row;
}
return $array;
}

How to index the result of a mySql query as an array of array?

If I need to select and use information of every element of a table in a database the procedure would be this:
$query = "...mySql query...";
$query_result = mysql_query($query) or die (mysql_error());
Then if I wished to access the fields of the result I would use the function mysql_fetch_array() and access them like this:
$query_result_array = mysql_fetch_array($query_result);
echo $query_result_array['field_1'];
....
echo $query_result_array['field_i'];
....
But since more elements could be returned by the query I would like to access every single of them with an array indexed from 0 to mysql_num_rows($query_result).
As an example:
echo $query_result_array['field_i'][0];
....
echo $query_result_array['field_i'][mysql_num_rows($query_result)];
should print for every selected element of the table the value of field i.
Is there a function that will do the job for me?
If not, any suggestions on how to do it?
Thanks in advance for help.
This may be an alternative
$res = mysql_query("..SQL...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$arr[] = $row;
}
var_dump($arr);
Or
$res = mysql_query("..SQL...");
for
(
$arr = array();
$row = mysql_fetch_assoc($res);
$arr[] = $row
);
var_dump($arr);
I don't think there is such a method; you have to do it yourself.
try with something like:
$res = mysql_query("..mySql query...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$query_result_array[] = $row;
}
then you access your data like:
echo $query_result_array[0]['field_i'];
based on 2 previous answers, those authors assuming that usual SO author is familiar with such a thing as creating a function
function sqlArr($sql) { return an array consists of
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
if ($res) {
while ($row = mysql_fetch_assoc($res)) {
$ret[] = $row;
}
}
return $ret;
}
$array = sqlArr("SELECT * FROM table");
foreach ($array as $row) {
echo $row['name'],$row['sex'];
}
this resulting array have different structure from what you asked, but it is way more convenient too.
if you still need yours unusual one, you have to tell how you gonna use it

Categories