I've been reading in every thread in here that is related to this but I always get it wrong.
Please help cause I always get the error
"Notice: Array to string conversion" in line "$address[] =
mysql_result($row, 0 );"
below. Please help.
if ($p_address=mysql_query($email))
{
$address = array();
while($row = mysql_fetch_assoc($p_address))
{
$address[] = mysql_result($row, 0 );
}
$all_address = implode(',', $address);
Change this line
$address[] = mysql_result($row, 0 );
To this:
$address[] = $row;
And then to see the keys and values available in the new $address array, you can do something like this:
print_r($address);
In order to keep implode() functional, do something like this:
for ($i = 0; $i < count($address); $i++) {
$all_address[] = implode(',', $address[$i]);
}
Final output:
if ($p_address=mysql_query($email))
{
$address = array();
while($row = mysql_fetch_assoc($p_address))
{
$address[] = $row;
}
for ($i = 0; $i < count($address); $i++) {
$all_address[] = implode(',', $address[$i]);
}
// Example for outputting on screen:
foreach ($all_address as $aa) {
print $aa . "<br/>\n";
}
}
Hope that helps...
$row is set in every iteration of the while loop. every time it contains a new table record. So you just need to add each record in the address array.
while($row = mysql_fetch_assoc($p_address))
{
$address[] = $row;
}
Related
I need to split the string 12345 into an array of characters. Using the following code I am getting:
undefined offset error
Else I am having complete array echoed using print_r() function.
$arr = str_split('12345');
echo $arr[1];
EDIT:
Full code:
$id = 4;
$sql = "SELECT * FROM `time_table` WHERE user_info_iduser_info = $id OR tutor_detail_idtutor_detail = $id";
$result = mysqli_query($conn,$sql);// 5 rows selected
while($row = mysqli_fetch_row($result)){
echo $row[3]; // outputs 12345
$arr = str_split($row[3]);
echo $arr[1]; // should output 2 but not working :(
}
Use the below code instead of str_split
$str="12345";
$str_len = strlen( $str );
$arr = array();
for ( $i = 0; $i < $str_len; $i++ )
{
$arr[] = $str[$i];
echo "<br>".$str[$i]."<br>";
}
print_r( $arr );
I'm using array_unique() to remove a duplicate value but it gives me an error when the value comes from a string then converted using explode, and values not displaying correctly
I'm using http://phptester.net/ to test
$email = 'general#t.com,info#t.com,info#t.com,jaa#t.com';
$emailList = array_unique(array_filter(array_map('trim',explode(',',$email))));
for($i = 0; $i < count($emailList); $i++){
echo $emailList[$i];
}
I would do it like this :
$email = 'general#t.com,info#t.com,info#t.com,jaa#t.com';
$emailList = (array_map('trim',explode(',',$email)));
$result = array_unique($emailList);
var_dump($result);
In case you want to print the array values, using for-loop, you can do like this:
$email = 'general#t.com,info#t.com,info#t.com,jaa#t.com';
$emailList = (array_map('trim',explode(',',$email)));
$result = array_unique($emailList);
for($i = 0; $i < count($emailList); $i++){
if( $emailList[$i]!=null)
echo $emailList[$i];
}
I've got the following sql query:
$sql = "SELECT lat, lang
FROM users";
I then use the following code to put the results of the array into two arrays, one for lat and one for lang.
$i = 0;
foreach($results as $row) {
$latArray = array();
$langArray = array();
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
However, it seems that only the last value that is passed to the array is stored. When I echo out each value of the array I get the following error: Undefined offset: 0 which I believe means theres nothing at latArray[0].
I'm sure I've missed something obvious here but why aren't all the values copied to the new array?
$i = 0;
$latArray = array(); //Declare once, do not redeclare in the loop
$langArray = array();
foreach($results as $row) {
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
Declare your array before the loop
$latArray = array();
$langArray = array();
foreach($results as $row) {
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
You should put
$latArray = array();
$langArray = array();
Before foreach cycle (like you do with your counter $i), 'cause with every new value from $result it resets thous values..
so your code will look like:
$i = 0;
$latArray = array();
$langArray = array();
foreach($results as $row) {
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i = ($i + 1);
}
use fetch_assoc instead:
$latArray = array();
$langArray = array();
while($row = mysql_fetch_assoc($your_query)){
$latArray[$i] = $row['lat'];
$langArray[$i] = $row['lang'];
$i++;
}
if u are using msqli us mysqli_fetch_assoc
I have the following code which is meant to cycle through names submitted on a form:
$row_count = count($_POST['name']);
if ($row_count > 0) {
mysql_select_db($database, $connection);
$name = array();
$workshop = array();
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name[i] = mysql_real_escape_string(ucwords($_POST['name'][$i]));
$workshop[i] = mysql_real_escape_string($_POST['workshop'][$i]);
}
$names = "('".implode("','",$name)."')";
.....etc
For some reason $names is only returning the last name submitted on the form, rather than all of the names. Could someone help me get this working correctly?
Thanks,
Nick
problem is here
$name[i] =
$workshop[i] =
solution:
$name[$i] =
$workshop[$i] =
now your code is working in this way:
$name["i"] =
$workshop["i"] =
so you have only one element in $name, $workshop arrays. (last from loop)
$result = mysql_query($query);
$filter = array();
while($r = mysql_fetch_array($result))
{
for ( $i = 0; $i<20; $i++)
{
$filter[] = $r["name"][$i];
}
$name = implode(",", $filter);
}
Above is a portion of main code. I want to restrict loop to run only 20 times. if above 20 it should omit...but this gives me some odd result...I know some where I made a mistake but where?
mysql_fetch_array only fetches a single row of data from the query results. It looks like you're trying to fetch only a single field from the results, so you'd want something like this:
$i = 0;
while($row = $mysql_fetch_assoc($result)) {
$i++
if ($i >= 20) {
break;
}
$filter[] = $row['name'];
}
$name = implode(",", $filter);
But this is very inefficient. Why not have MySQL do the row-limiting itself?
SELECT your,fields,here
FROM yourtable
WHERE ...
ORDER BY ...
LIMIT 20
and then you only get 20 rows to start with, without forcing mysql to fetch all however-many-there-are-beyond-20.
You are not advancing to the next row
$result = mysql_query($query);
$filter = array();
$i = 0;
while($r = mysql_fetch_array($result))
{
$filter[] = $r["name"][$i]; }
$name = implode(",", $filter);
if(++$i == 20)
{
break;
}
}