This shows all the elements from an array in a string but without a separator. I used ',' as a separator in this code. And it does not work. How can i separate them with separator.
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("Database Connection failed.".mysql_error());
}
$db = mysql_select_db("test1",$con);
if(!$db)
{
die("Database selection failed.".mysql_error());
}
$query = "select * from menu limit 10";
$result = mysql_query($query,$con);
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
//$menu_name = $row['m_name'];
//$menu_image = $row['m_image'];
$menu_name = array($row['m_name']);
$menu_image = array($row['m_image']);
echo implode(',',$menu_name);
//echo "<img src='images/$menu_image' style='height:200px;width:200px;'>";
}
if(!$result)
{
echo mysql_error;
}
?>
you are storing array into variable . so if you will use imploade on an variable it will not show the result so best way is to store your result into an array and then use implode .
Don't use depricated mysql_* function use mysqli_* or pdo
instead
$menu_image=array(); // start array to store
$menu_name=array();
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$menu_name[] = $row['m_name']; // store menu name
$menu_image[] = $row['m_image']; // store menu image
}
echo implode(',',$menu_name); // finaly use implode
You can try this:
$menu_name[];
$menu_image[];
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$menu_name[] = $row['m_name']; // I change your $menu_name into an array so that when you fetch $row['m_name'] it returns an array
$menu_image[] = $row['m_image']; //same thing
}
echo implode(',',$menu_name);
//echo "<img src='images/$menu_image' style='height:200px;width:200px;'>";
try this in place of your while loop
$menu_name = array();
$menu_image = array();
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$menu_name[] = $row['m_name'];
$menu_image[] = $row['m_image'];
}
echo implode(',',$menu_name);
Related
I had a script for this that worked great in PHP 5.3. New host only supports 5.5 so I've done some modifying. So far, I can only get one row to be returned. I've been agonising for hours and I just can't see why.
Is this possibly because I'm not using array_push() after the while loop? In my original script I had it but found out with this one I got a 500 Internal Server Error with it which disappeared if I took it out.
<?php
$mysqli = mysqli_connect("localhost", "user", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$row_array = array();
$query = "SELECT title,year,author,journal,keywords,abstract,url FROM entries";
if ($result = mysqli_query($mysqli, $query)) {
/* fetch object array */
while ($row = mysqli_fetch_assoc($result)) {
$row_array['title'] = $row['title'];
$row_array['year'] = $row['year'];
$row_array['author'] = $row['author'];
$row_array['journal'] = $row['journal'];
$row_array['keywords'] = $row['keywords'];
$row_array['abstract'] = $row['abstract'];
$row_array['url'] = $row['url'];
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
What get's returned is this:
{"data":{"title":"ERGOT The Genus Claviceps","year":"1999","author":null,"journal":null,"keywords":"LSD","abstract":null,"url":null}}
Which is fine and dandy and all except there are about 9500 other rows that should be coming back with it.
You need to have a multi-dimensional array and you have taken a single dimensional array.
So, every time record comes, it overwrites older one.
Hence, you are getting the last record only.
Modify your code to:
$i=0;
$row_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
++$i;
}
$row_array= array();
while ($row = mysqli_fetch_assoc($result)) {
$tmp = array();
$tmp['title'] = $row['title'];
$tmp['year'] = $row['year'];
$tmp['author'] = $row['author'];
$tmp['journal'] = $row['journal'];
$tmp['keywords'] = $row['keywords'];
$tmp['abstract'] = $row['abstract'];
$tmp['url'] = $row['url'];
$row_array[] = $tmp;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
Your while loop keeps overwriting the same array elements in the $row_array array.
To do what you're trying to do:
while($row = mysqli_fetch_assoc($result)) {
$row_array[] = $row;
}
Your $row_array replaces every time and you get the last one. change your code to:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$row_array[$i]['title'] = $row['title'];
$row_array[$i]['year'] = $row['year'];
$row_array[$i]['author'] = $row['author'];
$row_array[$i]['journal'] = $row['journal'];
$row_array[$i]['keywords'] = $row['keywords'];
$row_array[$i]['abstract'] = $row['abstract'];
$row_array[$i]['url'] = $row['url'];
$i++;
}
echo '{"data":';
echo json_encode($row_array);
echo '}';
What I want is to convert an array to a string so I can display it on my websites. I made a query to select the values id, title, date, auteur. When i use my function I made for this, I get the following error:
Notice: Array to string conversion in /Applications/MAMP/htdocs/beheer/index.php on line 444
Array.
The function is as following:
<?php
include ('connect/connect.php');
function getListContentMain() {
global $con;
$dbname = 'content';
mysqli_select_db($con,$dbname);
$q = "SELECT * FROM main LIMIT 5";
$result = mysqli_query($con,$q);
$ListContentMainData = array();
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['title'];
echo $row['date'];
echo $row['auteur'];
}
$ListContentMainData[] = $row;
return $ListContentMainData;
}
?>
implode it up
return implode(' ',$ListContentMainData);
You are returning an array but you are trying to print it as a normal string , so in that case you need to implode it (array) and convert it to a string.
Rewrite like..
$ListContentMainData = array();
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['title'];
echo $row['date'];
echo $row['auteur'];
$ListContentMainData[]=$row['id'];
$ListContentMainData[]=$row['title'];
$ListContentMainData[]=$row['date'];
$ListContentMainData[]=$row['auteur'];
}
return implode(' ',$ListContentMainData);
This will make an array of strings, one for each row in the table with columns separated by a space (you could change that in the implode statement).
function getListContentMain() {
global $con;
$dbname = 'content';
mysqli_select_db($con,$dbname);
$q = "SELECT * FROM main LIMIT 5";
$result = mysqli_query($con,$q);
$ListContentMainData = array();
while($row = mysqli_fetch_array($result)) {
echo $row['id'];
echo $row['title'];
echo $row['date'];
echo $row['auteur'];
array_push($ListContentMainData, implode(' ', $row));
}
return $ListContentMainData;
}
I'm trying to store the title(summary) and date(created) of an event in an array. But I think im missing something in my loop.
<?php
$summary = array();
$date = array();
mysql_connect('mysql.server', 'myUsername', 'myPass') or die('Could not connect: ' . mysql_error());
mysql_select_db("mxgsite") or die(mysql_error());
$query_summary = mysql_query('SELECT summary FROM event_info') or die(mysql_error());
$query_date = mysql_query('SELECT created FROM event_details') or die(mysql_error());
$row_summary = mysql_fetch_array($query_summary);
$row_date = mysql_fetch_array($query_date);
$i = 0;
while(($row1 = mysql_fetch_array($query_summary))) {
$row2 = mysql_fetch_array($query_date);
$summary[] = $row['summary'];
$date[] = $row['created'];
echo $summary[$i] . " " . $date[$i] . "<br ?>";
$i++;
}
I know i'm getting values because I can echo out 1 value, but if I want to put all the values in an array and try to echo out that array I keep getting blank values?
It seems to me like you are trying to do too many things here. Since the 2 sets of values are not being stored in a way where they are related/linked to each other, you might as well deal with them in separate while loops. Try something like this:
while ($row = mysql_fetch_array($query_summary)){
$summary[] = $row[0];
}
while ($row = mysql_fetch_array($query_date)){
$date[] = $row[0];
}
If you want to relate the tables, per the comments above, you can try something more like:
$result = mysql_query('SELECT a.eventid, a.summary, b.created
FROM event_info a
join event_details b
on a.eventid = b.eventid');
$events = array();
while ($row = mysql_fetch_array($result)){
$event = array();
foreach ($row as $key=>$value){
$event[$key]=$value;
}
$events[] = $event;
}
I have the code below and it's working as it should however instead of echoing the results to the screen, I need to store the results in a php variable called $var. How would I go about doing that?
<?php
$sql = "SELECT id_member FROM smf_members WHERE FIND_IN_SET(24,additional_groups)";
$con = mysql_connect('localhost', 'sqluser', 'sqlpass');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test_db");
$result = mysql_query($sql, $con);
while ($row = mysql_fetch_array($result)) {
echo $row['id_member'];
}
mysql_close($con);
?>
Depending on what you want to achieve here is a few possible ways of doing this
$var = "";
while ($row = mysql_fetch_array($result)) {
$var .= $row['id_member'] . "\n";
}
$var = array();
while ($row = mysql_fetch_array($result)) {
$var[] = $row['id_member'];
}
replace echo with $var[].
That will push each result onto the end of the array. It would probably be good to define the variable first.
$var = array();
while ($row = mysql_fetch_array($result)) {
$var[] = $row['id_member'];
}
<?php
$sql = "SELECT id_member FROM smf_members WHERE FIND_IN_SET(24,additional_groups)";
$con = mysql_connect('localhost', 'sqluser', 'sqlpass');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test_db");
$result = mysql_query($sql, $con);
$v = array(); // $v instead of $var, since var is a keyword and may cause troubles
while ($row = mysql_fetch_array($result)) {
array_push($v, $row['id_member']);
// or
//$v[] = $row['id_member'];
}
mysql_close($con);
?>
If the select statement will return more than one result, then you need to store the values in an array:
$member_ids = array();
while ($row = mysql_fetch_array($result)) {
$member_ids[] = $row['id_member'];
}
If the select statement will return a single result (you can make sure by appending a LIMIT 1 to the value of the $sql variable).
$row = mysql_fetch_array($result);
$member_id = $row['id_member'];
I want to push key and value in array , but I can't
$con = mysqli_connect('localhost','root','','wp') or die (mysqli_error('Error:'));
$query = mysqli_query($con,'set names utf8')or die (mysql_error());
$qy = mysqli_query($con,"SELECT ID,post_title FROM wp_posts WHERE post_type='page' AND post_status='publish'")or die (mysql_error());
$arr = array();
while ($row = mysqli_fetch_array($qy)){
$id = "?page_id=".$row['ID'];
$title = $row['post_title'];
$arr[] = $id . "=>" . $title;
array_push($arr, "$id" => "$title");
}
plz help me ..
thanks ^_^
Here's what I would do instead:
$arr = array();
while ($row = mysqli_fetch_assoc($qy)){
$id = $row['ID'];
$arr[$id] = $row['post_title'];
}
And then when you need to print them:
foreach ($arr as $id => $title) {
echo "?page_id={$id}'>{$title}</a>";
// or whatever, depends on how you want to print it
}
Don't store unnecessary information (ie: ?page_id=) in arrays.
Do you want to do $arr[$id] = $title? Or do you want this:
if (!isSet($arr[$id])) {
$arr[$id] = array();
}
$arr[$id][] = $title;
The former will make it so that $arr contains $id=>$title. The latter will make it so that $arr contains $id=>array($title1,$title2,$title3) etc if there are multiples.