Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have problem i am using below script for merge tables and get data from mysql
$myboxexi=mysql_query('select box.id,box.page_name,box.title,box.connect,box.type,box.uid,box.description,box.image,box.url,box.status,box.date,box.time
from boxes as box left join page_boxes as pages on box.uid=pages.uid and pages.uid="'.$session_id.'"');
while($my_boxi=mysql_fetch_array($myboxexi)){
$title=$my_boxi['title'];
$bid=$my_boxi['id'];
$ptitle=$my_boxi['page_name'];
$connect=$my_boxi['connect'];
$type=$my_boxi['type'];
$iuid=$my_boxi['uid'];
$description=$my_boxi['description'];
$image=$my_boxi['image'];
$url=$my_boxi['url'];
$appr=$my_boxi['status'];
$date=$my_boxi['date'];
$time=$my_boxi['time'];
$myinfo=mysql_query('select * from users where id="'.$iuid.'"');
$my_boxi_info=mysql_fetch_array($myinfo);
$user_id=$my_boxi_info['id'];
$user_name=$my_boxi_info['user_name'];
$thmb=$my_boxi_info['thumb'];
if(strpos($thmb,'https://') !== false) {
$thmbs=$thmb;
}else
{
$thmbs='images/thumbs/'.$thmb;
}
}
its working fine but problem is i am getting double item like if i have 1 item then i get 2 item how can i solve this issue ?
I think you get 2 values because you have for example,element [0] and same element with key=name of column , so use only associatif result :
Change this :
while($my_boxi=mysql_fetch_array($myboxexi)){
//data here.
}
To this :
while($my_boxi=mysql_fetch_array($myboxexi, MYSQL_ASSOC)){
//data here.
}
Or to this
while($my_boxi=mysql_fetch_assoc($myboxexi)){
//data here.
}
mysql_fetch_array() returns a dual-keyed array. e.g.
SELECT foo FROM bar
$row = mysql_fetch_array($result);
will give you the array:
$row = array(
0 => 'value from foo',
'foo' => 'value from foo'
);
Since you're blindly looping on the returned array, you're looking up each of those "duplicate" results. If you only want ONE of the field types, try the dedicated functions, or the optional type argument,e .g
$row = mysql_fetch_assoc($result); // return only string keys
$row = mysql_fetch_array($result, MYSQL_ASSOC); // ditto
Presumably you are getting two items because two pages have the same user/session id that is being passed in:
select box.id, box.page_name, box.title, box.connect,box.type, box.uid, box.description,
box.image, box.url, box.status,b ox.date,box.time
from boxes box left join
page_boxes pages
on box.uid = pages.uid and pages.uid = "'.$session_id.'"'
(To me, it is suspicious that you are setting uid -- which often means "user id" -- to a variable called $session_id, but that is another matter.)
If you only want one, add limit 1 to the end of the query:
select box.id, box.page_name, box.title, box.connect,box.type, box.uid, box.description,
box.image, box.url, box.status,b ox.date,box.time
from boxes box left join
page_boxes pages
on box.uid = pages.uid and pages.uid = "'.$session_id.'"'
limit 1;
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a database of 100,000 contacts.
I have a multiple select box which I would like to use to select contacts to add to a specific mailing list.
My problem is that I am currently storing the data for the mailing list in a serialized array format (PHP) in MySQL.
When I select over a certain number of contacts, something seems to break (I assume not enough memory) and does not update the array.
Is there a best way to store a large array in MySQL and is there a best way to keep memory usage in a PHP array() low?
Code Example
if(isset($_POST['add'])) {
$name = $core->EscapeString($_POST['name']);
$desc = $core->EscapeString($_POST['desc']);
foreach($_POST['addSelect'] as $null => $id) {
if(!in_array($id, $recipientArray)) {
$recipientArray[] = $id;
}
}
$contacts->updateML($lid, $name, $desc, serialize($recipientArray));
}
else if(isset($_POST['rm'])) {
$name = $core->EscapeString($_POST['name']);
$desc = $core->EscapeString($_POST['desc']);
foreach($recipientArray as $null => $id) {
foreach($_POST['rmSelect'] as $null1 => $id1) {
if($id == $id1) {
unset($recipientArray[$null]);
}
}
}
$contacts->updateML($lid, $name, $desc, serialize($recipientArray));
}
UpdateML Function
//Class 3, Function 16
function updateML($lid = '', $name = '', $desc = '', $recip = '') {
global $MySQLi;
$query = "UPDATE `mailing_lists` SET `name` = '".$name."', `desc` = '".$desc."', `recipients` = '".$recip."' WHERE `list_id` = '".$lid."' LIMIT 1";
$commit = $MySQLi->query($query);
if($commit == false) {
die("Issues with the database were detected. Please email peter#domain.com quoting error code: <strong>CLASS3/16.1</strong>.");
}
else
{
return true;
}
}
You need 2 tables. One for Mailing List Definition, one for recipients. In recipients table you'd need to have a foreign key relating the record to appropriate mailing list.
so mailing_lists table looks like this:
ml_id
name
desc
and recipients looks like this:
r_id
email
ml_id
First add the mailing list to the database:
INSERT INTO mailing_lists SET name = 'my first list', desc = 'mailsareus'
When you add new recipients just add a new row to recipients table:
INSERT INTO recipients SET email = 'xxx#mail.com', ml_id = 1
If you need to get all the recipients and definition from a mailing list just use a join
SELECT * FROM mailing_lists JOIN recipients ON recipients.ml_id = mailing_lists.ml_id
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have already seen examples for string manipulation using "explode" but none of them does my job..
I have an array of exploded strings:
$grand_result = 12,5,7,3,2,1 ;
$grandparent_id = explode(",",$grand_result);
i want to insert some values in db having id = $grand_result except first value returned (12) in this case
$grand_result can have many values i want to fetch only first 7 values..
how can i do that ...thanks in advance
you can use array_slice
$output = array_slice($grandparent_id, 0, 7);
$grand_result = "12,5,7,3,2,1" ;
$grandparent_id = explode(",",$grand_result);
first update
$avoid = grandparent_id[0];
foreach ($grandparent_id as $id) {
if ($avoid != $id) {
//run your update query
}
}
second fetch
$query = "select * from `table_name` where id in (".$grand_result.") limit 7";
Set Limit of explode
$grand_result = 12,5,7,3,2,1 ;
$grandparent_id = explode(",",$grand_result,8);
unset($grandparent_id[7]); or $grandparent_id = array_slice($grandparent_id, 0, 7);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know php at all so sorry for a simple question - I've got the project started by another person so now I'm trying to finish it.
The problem is - I'm making an android app for which I can't make a change without changing a php and this language I don't know.
here's this part :
function getPlaces(){
$result = array();
$sql = "SELECT id, name, short_description, photo_list, selected, recommended, isTOP FROM place
WHERE id IN(SELECT id_place FROM rubric_place WHERE id_rubric IN(SELECT id FROM rubric WHERE name = '".$_REQUEST["rubric"]."')) ORDER BY isTOP DESC";
getConnect();
$query = mysql_query($sql);
if(!$query){
error100();
}else {
$result['code'] = 200;
for ($i = 0; $i < mysql_num_rows($query); $i++) {
$row = mysql_fetch_assoc($query);
$result["places"][$i] = $row;
$sql = "SELECT * FROM rubric WHERE id IN(SELECT id_rubric FROM rubric_place WHERE id_place = ".$row[id].")";
$queryModule = mysql_query($sql);
if($queryModule){
for ($k = 0; $k < mysql_num_rows($queryModule); $k++) {
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rubrics"][$k] = $rowModule;
}
}
$sql = "SELECT SUM(rating)/COUNT(rating) AS rating FROM comment WHERE id_place = ".$row[id];
$queryModule = mysql_query($sql);
if($queryModule){
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rating"] = $rowModule[rating];
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
}
what I need is to make another function that returns in $result a value of "length" of places. I know I can find out length on the other end - in Android's app result, but this particular function will be changed so it will return by 20 results only so I need another function that returns length so plz help
To count the number of characters in a json you can do:
$string = json_encode($result, JSON_UNESCAPED_UNICODE);
$length = strlen($string);
You have to first put the json into a variable to count it, before outputting it.
To count the items in an array before you turn it into a json, you can do:
$length = count($array);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to check if I have an hour in the database but If I have more than hour in the database it outputs twice, if I have 3 three times...
In this specific case this gives me: 9 (in blue color) and then 9 (in red color)
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
while ($row = mysqli_fetch_array($result)) {
if ($row['hora']=='09:00:00') { echo '<p class="blue">9</p>'; }
else { echo '<p class="red">9</p>'; }
}
I have a MySql database with columns: id, professional and hour. I have stored 2 hours and this is what I get if I echo $row['hora'] 09:00:00 and 10:00:00
In this case the if should give me just 9 in blue because it is in the database. I do not understand why it happens. How can I solve this?
This is the structure of the MySql database:
Presumably you are building up a calendar or the like.
The query you are using is returning all rows (and you are looping over each row, regardless of the hour). If you want to check if specific hours exist you could build up an array of hours:
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
$hours = array();
while ($row = mysqli_fetch_array($result)) {
$hours[$row['hora']] = $row;
}
Now to check a specific hour we can do:
if (!empty($hours['09:00:00'])) {
echo '<p class="blue">9</p>';
}else {
echo '<p class="red">9</p>';
}
Edit: Explanation
In the first loop we are building up an array. This will look like:
$hours = array(
'08:00:00' => $dataForThisRow,
'09:00:00' => $dataForThisRow
);
In the above '08:00:00' is called a key. Each key is a time that was found in the database.
To check if there is an entry in the database for our time we can check if there is a non empty key. Hence we use:
if(!empty($hours['09:00:00'])
This if will run if $hours has a key with the value '09:00:00'. If there is no key then the other part of the if will run.
You are comparing with this value: 09:00:00, but the database seems to have stored this other: 9:00:00
Change your sql query like below:
$query = "SELECT TIME_FORMAT(hora,'%H:%i:%S') FROM consulta
WHERE professional=1
AND client=0";
$result = mysqli_query($con, $query);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I work on a page(.php) that show some information from databases(like article name,article author).I also have a pagination script that work good(the ideea of pagination script is when I press a link the url of the site goes to something like ?page=2)Now I try to numbering the records that I show form databases.But how can I do that?
Pagination script:
$per_pages = 6;
$nrofarticles_query = mysql_query("SELECT COUNT('id') FROM tbl_articles");
$nrdearticole = mysql_result($nrofarticles_query, 0);
$totalpages = ceil($nrdearticole/$per_pages);
$currentpagedisplay = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$firstpair = ($currentpagedisplay - 1) * $per_pages;
I echo record from database like so:
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair, $per_pages");
while($article = mysql_fetch_array(article_set)){
echo $article['article_name'];
}
So I show 6 records from database on page change show the next 6 records or if is less than 6 show how many remained .How can I numbering the records like this:
?page=1
1 article_name1
2 article_name2
3 article_name3
....
6 article_name6
?page=2
keep going
7 article_name7
8 article_name8
.....
EDIT
I was thinking to something like
<?php
$nrofarticles = 0;
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair,$per_pages");
confirm($articles_set);
while($article = mysql_fetch_array($articles_set)){
$nrofarticles ++;
}
echo "<ul id=\"id\">";
for($i = $firstpair+1; $i<=$nrofarticles * $currentpagedisplay; $i++){
echo '<li class="nr_id">' .$i. '<sub>#</sub></li>';
}
echo "</ul>";
?>
It's works but only on first 2 page of 3 because I have 16 articles in total - so on last page $firstpair + 1 will be 13 and $nrofarticles * $currentpagedisplay will be 12(4(articles) * 3(page=3)).
Go use a framework. You'll get pagination (and all sorts of extras) for free.