I have the following queries that work perfectly in MySql:
SELECT * FROM rapoarte WHERE nrtel LIKE '0256%' OR nrtel LIKE '0356%
SELECT * FROM rapoarte WHERE nrtel NOT LIKE '07%' AND nrtel NOT LIKE '0256%' AND nrtel NOT LIKE '0356%'
SELECT * FROM rapoarte WHERE nrtel LIKE '07%'
in PHP they will result the following:
results just for LIKE '0256%'
no results
inclomplete results. i have phone numbers that start with 076, 075 and it only shows the numbers that start with 076.
Anyone know why?
thanks,
Sebastian
EDIT
here is the code:
$select_int= mysql_query("SELECT * FROM rapoarte WHERE nrtel LIKE '0256%' OR nrtel LIKE '0356%'");
$local = mysql_fetch_array($select_int);
echo "<table align='center' border='0' width='600'><tr><td><b>Ziua</b></td><td><b>Ora</b></td><td><b>Trunchi</b></td><td><b>interior</b></td><td><b>Durata</b></td><td><b>Numar Format</b></td></tr>";
while($int = mysql_fetch_array($select_int)) {
echo "<tr>
<td>".$local['ziua']."</td>
<td>".$local['ora']."</td>
<td>".$local['linie']."</td>
<td>".$local['interior']."</td>
<td>".$local['durata2']."</td>
<td>".$local['nrtel']."</td></tr>";
}
echo "</table>";
Again...
Here $local = mysql_fetch_array($select_int); you discard your first line. You fetch it and you don't use it.
The second problem is here $int = mysql_fetch_array($select_int). You actually want $local = mysql_fetch_array($select_int) because that's what you use in the while block.
You seem to be discarding the first result on the 2nd line with
$int = mysql_fetch_array($select_int);
...not to mention the query in the code snippet you edited in doesn't actually match any of the three you claim work correctly.
You're not iterating over the results, rather, you're just getting the first one.
while(($local = mysql_fetch_array($select_int)) != null){
// $local contains 1 result row
}
The query you're using in your PHP script doesn't use "LIKE"
Related
In my mysql tables I have for example a row that holds values, lets call this table "days" and the row "haircuts".
So I have 5 rows all with "1","2","3","4","5" under "haircuts". What I want to do, in php, is add them all together and echo. So it would maybe look like this:
echo "Total haircuts: ",$haircuts;
Which would hopefully show:
Total haircuts: 15
I can't figure out how, though. Here's how I'm currently doing it.
$getstats = mysqli_query($con,"SELECT * FROM stats");
$gotstats = mysqli_fetch_array($getstats);
$haircuts = $gotstats['haircuts'];
But it only echoes the "haircuts" of the first row? :(
I have multiple columns I would like to total and echo too if that is possible?
You can use the MySQL statment SUM in your query, just change your code by this one:
$getstats = mysqli_query($con,"SELECT SUM(haircuts) AS totalhaircuts FROM stats");
$gotstats = mysqli_fetch_array($getstats);
$totalhaircuts = $gotstats['totalhaircuts'];
echo "Total haircuts: ",$totalhaircuts;
You probably should use MySQL SUM().
SELECT SUM(haircuts) as tot_cuts FROM stats;
$gotstats['tot_cuts'];
You need to use loop for getting all rows. Example:
$getstats = mysqli_query($con,"SELECT * FROM stats");
while($gotstats = mysqli_fetch_array($getstats)){
$haircuts = $gotstats['haircuts'];
echo $haircuts . '<br />';
}
I have a variable ($email_body) which has the body of an email in it. just before this variable i am running this PHP:
$sql3="SELECT * from billing_pdf_archive where sequence = '".$result["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($result3=mysql_fetch_array($rs3))
{
$invoices_list_data[] = $result3["invoice_number"];
}
$invoices_list = implode('<br>',$invoices_list_data);
i want to make it list all the rows found (invoice_number column) in the $email_body variable
so i have tried:
$email_body = $invoices_list;
but its only displaying one row
how can i do this?
I think your problem probably lies in debugging the query. First try and debug your code step by step.
$sql3="SELECT * from billing_pdf_archive where sequence = '".$result["sequence"]."' ";
echo $sql3;
The above should output your sql statement. Something like
SELECT * FROM billing_pdf_archive WHERE sequence = 'val'
Try connecting to your database and running it in mysql query browser. Does it return multiple results? If so continue to next step.
$rs3=mysql_query($sql3,$conn);
while($result3=mysql_fetch_array($rs3))
{
var_dump($result3);
$invoices_list_data[] = $result3["invoice_number"];
}
The above should output values. If it does then you are on your way to next step. Your implode should join the values up.
I have problem with mysql_fetch_array() and while loop. I have the query:
$tagsquery = mysql_query("SELECT `url` FROM `tags`, `mapa-tagow`, `statusy` WHERE `tags`.`id` = `mapa-tagow`.`tag-id` AND `statusy`.`id` = `mapa-tagow`.`article-id` AND `tags`.`tag` ='$tag' ORDER BY `url` ASC ") or die("ERROR: Tags doesn't exist.");
And results in while loop:
while($tags = mysql_fetch_array($tagsquery)) {
echo "<a href='tags.php?url=$url'>$url</a>, ";
}
When testing this query in PHPMyAdmin I have one more result than I get in PHP. I dont know why PHP always missing first result.
A few possibilities:
You're looking at a different copy of the same database
You've already started fetching your result set earlier than your while loop
On a side note, you should build an array of your tag links, and then output them using implode... while(){ $tag_links[] = '<a href...';} print implode(', ', $tag_links);
You need to change your code to:
while($tags = mysql_fetch_array($tagsquery)) {
echo ''.$tags['url'].', ';
}
Note that i took the opportunity to correct your tag output, there are many other ways to output HTML but this is easier to read in most cases.
If you can't spot the problem, i changed your $url to $tags['url'], so i output the URL from the $tags that you got from mysql_fetch_array()
Cheers
At this time I have this very complex query that I loop through and I get something like this:
List of Challenges:
TEAM A
- Challenge 1
TEAM A
- Challenge 4
TEAM A
- Challege 6
And I want to change to something like:
TEAM A
- Challenge 1
- Challenge 4
- Challenge 6
My question is, since the query is a very complex one, maybe I could do this inside the loop but, if that's the case, how can we achieve something like that?
Can I ask an example case so that I can use, in order to solve this issue?
Thanks a lot,
MEM
UPDATE:
The query is something like this:
Translated:
public function listachallengesPendentes()
{
$select = $this->getAdapter()->select();
$select->from(array("e"=>"teams"),array('name'));
$select->join(array("de"=>"challengeperteam"),"e.cod_team = de.cod_teamFk",array());
$select->join(array("d"=>"challenges"),"d.cod_challenge = de.cod_challengeFk",array('title'));
$select->columns(array("e.cod_team"
,"name_team"=>"e.name"
,"d.cod_challenge"
,"name_challenge"=>"d.title"
,"d.details"
,"d.score"
,"category"=>"d.cod_categoryFk"
,"de.proof"
,"de.date_concluded"
,"de.cod_challenge_team"
));
$select->where("de.status = 0");
$select->order(array('e.cod_team DESC', 'de.cod_challenge_team DESC'));
return $this->getAdapter()->fetchAll($select);
}
So I need to add a distinct some part :s :D ?
The foreach actually is pretty basic:
foreach ($challenges as $d){
//display the name:
echo $d['name_team'];
...
}
UPDATE 2
The clean query (not tested):
SELECT e.name
,d.cod_team
,d.cod_challenge
,d.title
,d.details
,d.score
,de.proof
,de.date_concluded
,de.cod_challenge_team
FROM teams e
INNER JOIN challengeperteam de ON de.cod_teamFk = e.cod_team
INNER JOIN challenges d ON d.cod_challenge = de.cod_challengeFk
WHERE de.status = 0
ORDER BY e.cod_team DESC, de.cod_challenge_team DESC;
Something along the lines of:
$current_team = null;
foreach($challenges as $challenge){
if($current_team != $challenge->team){
$current_team = $challenge->team;
echo $current_team, "\n";
}
echo $challenge->challenge_name, "\n";
}
At a very basic level, ie in the loop, you can just detect if the TEAM A variable is equal to the current (previous) value, and if so, don't print it a second time. This relies on the result set being sorted on the TEAM A column.
However, you can also do this in the SQL query, so if you can provide the current SQL Query, I can explain how you'd update it.
you could store the array results in a multi-dimensional array like so:
$query_Challenges = "SELECT `Team`,`Challenges` FROM YourTable";
$Challenges = mysql_query($query_Challenges, $dbconnection) or die(mysql_error());
$row_Challenges = mysql_fetch_assoc($Challenges);
$challengeResults = array();
do{
if(!array_key_exists($row_Challenges['cod_team'])){
$challengeResults[$row_Challenges['cod_team']] = array();
}
$challengeResults[$row_Challenges['cod_team']][] = $row_Challenges['cod_challenge_team'];
}while($row_Challenges = mysql_fetch_assoc($Challenges));
EDIT
looking at your query statement, the data should be already sorted properly by your ORDER clause, so if you just need not repeatedly print the team as shown in codeblock 2, then something like:
$team = '';
do {
if($team != $row_Challenges['cod_team']){
echo "TEAM $row_Challenges['cod_team']<br/>";
$team = $row_Challenges['cod_team'];
}
echo " - $row_Challenges['cod_challenge_team']<br />";
}while($row_Challenges = mysql_fetch_assoc($Challenges));
you could easily substitute a foreach for the do loop, as long as there is a variable used as the "current team" and an if statement used to say "dont print the next team name unless its different than the current team name"
was wondering how to do a search result using PHP + MySQL but not show all the data in the result but only a SUMMARY (lets say limited to 200 characters). And the summary would exactly contain the keyword portion. So -100 characters+keyword+100 characters might be how it would be shown.
Thanks!
Assuming you are fine with taking the first instance of the keyword to use in your summary, you could break up the results of your query in PHP in a way similar to this:
$sql = "SELECT data_field FROM your_table WHERE data_field LIKE '%".$keyword."%'";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
$data = $row['data_field'];
$first_pos = strpos($data,$keyword);
if ($first_pos !== false) {
$output = substr($data,max(0,$first_pos - 100),200 + strlen($keyword));
echo $output;
}
}
Obviously you could do whatever suited your needs with $output once you had it.