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.
Related
I am trying to code it so users can type in their -- or others -- usernames which will be converted in their UUID then returns that user's stats from my MySql databse.
The code I attempted this with is
$username = $_POST['searchbox'];
$json = file_get_contents("https://api.mojang.com/users/profiles/minecraft/".$username);
$obj = json_decode($json);
$id = $obj->id;
$rank = Database::query("SELECT * FROM playerdata WHERE uuid=:uuid", array(':uuid'=>"".$id))[0]['rank'];
echo 'Showing results for '.$_POST['searchbox'].' '.$id.' Rank: '.$rank;
Except when I run this code it outputs:
"Showing results for kingbluesapphire 0d8d246d11c54cbbb197c6bc8ba01ee2 Rank:"
I know it's not a problem with the connection to the database because other queries are working
My goal right now is to get the field in the MySql Database thats called rank and I would like to display their rank.
Given the discussion in the comments, either you have to find out what is removing the dashes which I highly recomend to or change your query to:
$rank = Database::query("SELECT *
FROM playerdata
WHERE replace(uuid, '-','')=:uuid",
array(':uuid'=>"".$id))[0]['rank'];
Databases need to be given the exact value you are looking for, any different character in a equals operation will not give you any data.
It's more like:
<?php
if(isset($_POST['searchbox']){
$sb = $_POST['searchbox']);
$json = json_decode(file_get_contents("https://api.mojang.com/users/profiles/minecraft/$sb"));
if($queryRes = $connection->query("SELECT * FROM playerdata WHERE uuid={$json->id}")){
if($queryRes->num_rows){
$o = $queryRes->fetch_object(); // guessing there's only one row or you would put this in a while loop
echo "Showing results for $sb {$o->uuid} Rank:{$o->rank}";
}
}
else{
die($connection->connect_error);
}
}
?>
I'm assuming uuid is a number. Do tell.
I'm trying to display the results from 3 separate queries in 3 adjacent <div> containers. The three queries are as follows:
$query="SELECT * FROM students WHERE WeekOne='employer'";
$query="SELECT * FROM students WHERE WeekTwo='employer'";
$query="SELECT * FROM students WHERE WeekThree='employer'";
Desired output:
WEEK ONE ----- WEEK TWO ----- WEEK THREE
Result 1 ---------- Result 1 ----------- Result 1
Result 2 ---------- Result 2 ----------- Result 2
(etc...)
Can anybody help me with this? I've seen the multi_query on PHP manual but have no idea how to implement this. Many thanks.
#Emmanuel G
<?
$user = "xx";
$user_password = "xx";
$db_name = "xx";
$db = new mysqli ("xx", $user, $user_password, $db_name);
$queries = array(
"SELECT * FROM students WHERE WeekOne='$Name'",
"SELECT * FROM students WHERE WeekTwo='$Name'",
"SELECT * FROM students WHERE WeekThree='$Name'"
);
// iterate through the queries and their results
foreach($queries as $query){
$result = $mysqli->query($query);
echo '<div>'; // open that div up
while($row = $result->fetch_assoc()){ // gives u an array hoorah!
echo '<pre>'.print_r($row, true).'</pre>'; // just to look at it
echo "<p>{$row['FirstName']}</p>"; // just an example
}
echo '</div>'; // close it up
}
?>
The easiest way is change the query to this:
$query="SELECT * FROM students WHERE WeekOne='employer' OR WeekTwo='employer' OR WeekThree='employer'";
And then format the output accordingly.
Yes, change your query to bring your all result in one query request, then you manipulate from the data result, hitting multiple time for simple answer to the server is not a best practice, because your query is very simple.
but for your question
multi_query($query) //you can use this
have a look here http://suite101.com/article/how-tor-run-multiple-mysql-queries-with-php-a105672
To display it just iterate over each individual result and place that into a div. To make them adjacent you would add some css to your div's to float them since divs are inherently block elements and don't line up horizontally without some help.
Assuming you already have your database connection set up and should be using mysqli or the such you could generate your markup like so:
// u should try to make this one query but that's irrelevant to the question
// so I'll leave that to you to do later
$queries = array(
"SELECT * FROM students WHERE WeekOne='employer'",
"SELECT * FROM students WHERE WeekTwo='employer'",
"SELECT * FROM students WHERE WeekThree='employer'"
);
// iterate through the queries and their results
foreach($queries as $query){
if($result = $mysqli->query($query)){// only do stuff if you get a result
echo '<div>'; // open that div up
while($row = $result->fetch_assoc()){ // gives u an array hoorah!
echo '<pre>'.print_r($row, true).'</pre>'; // just to look at it
echo "<p>{$row['data']}</p>"; // just an example
}
echo '</div>'; // close it up
// mysqli free the result to prep for the next one
$result->free();
} else {
echo "Query failed to return results:\n$query\n";
}
}
i am guessing you are passing employer as URL parameter , so in either way you can also pass which week you want to compare with, let's say
$week = $_GET['weekOne'];
$employer = $_GET['employer'];
$query="SELECT * FROM students WHERE $week= $employer";
make sure you escape your data before executing query,to avoid sql injections
i hope it will help you. Thanks
well, i wanna pull out some data from a mysql view, but the wuery dos not seem to retrieve anything ( even though the view has data in it).
here is the code i've been "playing" with ( i'm using adodb for php)
$get_teachers=$db->Execute("select * from lecturer ");
//$array=array();
//fill array with teacher for each lesson
for($j=0;$j<$get_teachers->fetchrow();++$j){
/*$row2 = $get_lessons->fetchrow();
$row3=$row2[0];
$teach=array(array());
//array_push($teach, $row3);
$teach[$j]=mysql_fetch_array( $get_teachers, TYPE );
//echo $row3;*/
$row = $get_teachers->fetchrow();
//$name=$row[0]+" "+$row[0]+"/n";
//array_push($teach, $row1);
echo $row[0]; echo " ";echo $row[1]." ";
//$db->debug = true;
}
if i try something like "select name,surname from users", the query partially works . By partially i mean , while there are 2 users in the database, the loop only prints the last user.
the original query i wanted to execute was this
$get_teachers=$db->Execute("select surname,name from users,assigned_to,lessons
where users.UID=assigned_to.UID and lessons.LID=assigned_to.LID and
lessons.term='".$_GET['term']."'");
but because it didnt seem to do anything i tried with a view ( when you execute this in the phpmyadmin it works fine(by replacing the GET part with a number from 1 to 7 )
the tables in case you wonder are: users,assigned_to and lessons. ( assigned_to is a table connecting each user to a lesson he teaches by containing UID=userid and LID=lessonid ). What i wanted to do here is get the name+surname of the users who teach a lesson. Imagine a list tha displays each lesson+who teaches it based on the term that lesson is available.
Looking at http://adodb.sourceforge.net/ I can see an example on the first page on how to use the library:
$rs = $DB->Execute("select * from table where key=123");
while ($array = $rs->FetchRow()) {
print_r($array);
}
So, you should use:
while ($row = $get_teachers->fetchrow()) {
instead of:
for ($j = 0; $j < $get_teachers->fetchrow(); ++$j) {
The idea with FetchRow() is that it returns the next row in the sequence. It does not return the number of the last row, so you shouldn't use it as a condition in a for loop. You should call it every time you need the next row in the sequence, and, when there are no more rows, it will return false.
Also, take a look at the documentation for FetchRow().
for($j=0;$j<$get_teachers->fetchrow();++$j){
... a few lines later ...
$row = $get_teachers->fetchrow();
See how you call fetchrow() twice before actually printing anything? You remove two rows from the result set for every 1 you actually use.
while ($row = $get_teachers->fetchrow()) {
instead and don't call fetchrow() again within the loop.
Because you're fetching twice first in the loop
for($j=0;$j<$get_teachers->fetchrow();++$j){
... some code ...
// And here you fetch again
$row = $get_teachers->fetchrow();
You should use it like this
while ($row = $get_teachers->fetchrow()) {
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"
Ok, i have a problem here...
I am sending values of drop down lists via ajax to this PHP file.
Now I want to search a mysql database using these values, which I have managed to do, BUT, only if I set the values to something...
Take a look:
$query = "SELECT * FROM cars_db WHERE price BETWEEN '$cars_price_from' AND '$cars_price_to' AND year BETWEEN '$cars_year_from' AND '$cars_year_to' AND mileage BETWEEN '$cars_mileage_from' AND '$cars_mileage_to' AND gearbox = '$cars_gearbox' AND fuel = '$cars_fuel'";
now, what if the user doesnt select any "price_from" or "year_from"... The fields are only optional, so if the user doesnt enter any "price from" or "year from", then the user wants ALL cars to show...
Do I have to write a query statement for each case or is there another way?
I do something similar to davethegr8 except I put my conditions in an array and then implode at the end just so I don't have to worry about which conditions got added and whether I need to add extra AND's.
For example:
$sql = "SELECT * FROM car_db";
// an array to hold the conditions
$conditions = array();
// for price
if ($car_price_from > 0 && $car_price_to > $car_price_from) {
$conditions[] = "(price BETWEEN '$cars_price_from' AND '$cars_price_to')";
}
elseif ($car_price_from > 0) {
$conditions[] = "(price >= '$cars_price_from')";
}
elseif ($car_price_to > 0) {
$conditions[] = "(price <= '$cars_price_from')";
}
else {
//nothing
}
// similar for the other variables, building up the $conditions array.
// now append to the existing $sql
if (count($conditions) > 0){
$sql .= 'WHERE ' . implode(' AND ', $conditions);
}
You could simply detect which parameters are missing in your PHP code and fill in a suitable default. eg
if (!isset($cars_mileage_to))
$cars_mileage_to = 500000;
You can build you query, adding the "where" part only if your variables are different from "".
or if you're using mysql 5.x, you can also use subselects:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
don't forget to validate the input. It's trivial with firebug, for example, to inject some tasty sql.