To avoid open rows on the screen, how can I reach that the 5 vip fields, only are echoed when there is a value in it?
Thanks, Benny
$db = new mysqli('host', 'user', 'pass', 'dbase');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SELECT
user.FName,
user.LName,
user.HerbalifeID,
user.UplineS,
registratie.PartnerFName as pfn,
registratie.PartnerLName as pln,
registratie.NaamVIP1 as vip1,
registratie.NaamVIP2 as vip2,
rgistratie.NaamVIP3 as vip3,
registratie.NaamVIP4 as vip4,
registratie.NaamVIP5 as vip5
FROM
registratie
INNER JOIN user
ON registratie.userID = user.UserID
AND registratie.eventID =127;
";
$result = $db->query($query) or die($db->error.__LINE__);
if ($result->num_rows) {
while ($row = $result->fetch_object()) {
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>
{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>"; //only
}
} else {
echo 'No Results';
}
if(!empty({$row->vip1}) and !empty({$row->vip2}) and !empty({$row->vip3}) and !empty({$row->vip4}) and !empty({$row->vip5}) ){
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}else{
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln})<br><br>";
}
I don't know if I get it.
If you want to print only when all vips are filled, use the following code:
//...
$range = range(1,5);
while ($row = $result->fetch_object()) {
//This will be printed in every iteration
echo "{$row->FName} {$row->LName} {$row->HerbalifeID} {$row->pfn} {$row->pln}";
//if one of the vips has no value, go to the next $row
foreach($range as $r){
$vipColumn = "vip$r";
if(empty($row->$vipColumn)){
continue 2; //breaks both 'foreach' and 'while' loops
}
}
//This only echo when all vips has values
echo "<br>{$row->vip1}{$row->vip2}{$row->vip3}{$row->vip4}{$row->vip5})<br><br>";
}
//...
Related
I am creating a result page using php to pull data from database.
I have been able to connect the database to my webspace and displayed the data.
Now , I would like to have the page not show all the data at once. I want to add an input field example "Origen= Los Angeles" "Destination = London" and then show the results based on that criteria.
This is my first time doing something like this so if this sounds like I should know this I'm sorry. I hope I can get some help.
<?php
// include connection settings
require_once "connect.php";
// display a list of flights
if(!empty($_GET) && !empty($_GET['name'])){
$query = "SELECT * FROM Flight_Information WHERE name LIKE ''".
$_GET['name']."".$_GET['name']."'";
} else {
$query = "SELECT ID, airlineName, departureAirport, departureDate, destinationAirport FROM Flight_Information";
} // END if
$result = $db->query($query);
if ($result) {
// ... display results in while loop
while ($row = $result->fetch_assoc()) {
echo '<li>'.$row['airlineName'].' '.$row['departureAirport'].''.$row['departureDate'].''.$row['destinationAirport'].'</li>';
} // END while
} else {
// if there was an error with your query
// this will display it
echo "SQL Error: " . $db->error;
} // END if
?>
</ul>
<?php
if (!empty($_GET) && !empty($_GET['Flight_Information_ID'])) {
$Flight_Information_ID = $_GET['Flight_Information_ID'];
$query = "SELECT * FROM Flight_Information WHERE ID =" . $Flight_Information_ID;
// perform the query
if ($result = $db->query($query)) {
// check we have a result
if ($result->num_rows > 0) {
// loop through and print out the results
while ($row = $result->fetch_assoc()) {
// output a title for each result
echo '<hr/><p>You have selected FriendShipper' .$Flight_Information_ID. ':</p> <ul>';
// loop through and output details for each item
foreach ($row as $key => $value) {
echo '<li><em>'.$key.'</em> = <strong>'.$value.'</strong></li>';
}
// output a horizontal rule under the result
echo '</ul><hr/>';
}
// num_rows = 0, no results found
} else {
echo "<p>No Flights Found " .$Flight_Information_ID. "</p>";
}
// if there was an error with your query, this will display it
} else {
echo "SQL Error: " . $db->error;
}
}
?>
I have this php file and I want to echo one array that ticketnumber is located in it with json encode, in meaning I want to print only one array when isset POST the ticketnumber for this array, but I tried and every time I get error how to slove this problem in addition I used the loop (while) so is it correct for one array?
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS','');
define('DB', 'ala');
$con = mysqli_connect(HOST,USER,PASS,DB) or die ('unable to connect');
if ($_SERVER ['REQUEST_METHOD']=='POST') {
$ticketnumber = $_POST['ticketnumber'];
$sql = " SELECT * FROM contact WHERE ticketnumber = '$ticketnumber' ";
$res = mysqli_query($con, $sql);
$result = array();
while($get = mysqli_fetch_array($res))
{
array_push($result,array('ticketnumber' =>$get[0], 'subject' =>$get[1],'response' =>$get[2]));
}
if(isset($get)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}
}
?>
You don't define variable $get .
I thinks there should be..
if(!empty($result)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}
Try like this..
if(!empty($get)){
echo json_encode(array("responseticket"=>$result));
} else {
echo " error";
}
I've tried like twenty times and the closest I got was when I put in a variable stored in row 1 of the db and it returned the content the last row in the db. Any clarity would be extremely helpful. Thanks.
// Create connection
$coco = mysqli_connect($server, $user, $pass, $db);
// Check connection
if (!$coco) { die("Connection failed: " . mysqli_connect_error()); }
// Start SQL Query
$grabit = "SELECT title, number FROM the_one WHERE title = 'on' AND (number = 'two' OR number='0')";
$result = mysqli_query($coco, $grabit);
// What I need it to do
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$titleit = $row["title"];
$placeit = $row["number"];
$incoming = 'Help';
if ($titleit[$_REQUEST[$incoming]]){
$message = strip_tags(substr($placeit,0,140));
}
echo $message;
}
} else {
echo "not found";
}
mysqli_close($coco);
Put the input that you want to match into the WHERE clause of the query, rather than selecting everything and then testing it in PHP.
$incoming = mysqli_real_escape_string($coco, $_POST['Help']));
$grabit = "SELECT number FROM the_one WHERE title = '$incoming' AND number IN ('two', '0')";
$result = mysqli_query($coco, $grabit);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['number'];
}
} else {
echo "not found";
}
I think you need to add a break; in that if or I would assume it would go through each row in the database and set message if it matches the conditional. Unless you want the last entry that matches...if not you should debug:
if ($titleit[$_REQUEST[$incoming]]){
// set message
}
and see when it's getting set. That may not be the issue, but it's at least a performance thing and could explain getting the last entry
Have you tried print_r($row) to see the row or adding echos to the if/else to see what path it's taking?
Ok so I wrote some code to find records on a test database, it works if there is a record and does display the data, if there is no record it still says that it found stuff. It should say it did not. It even finds stuff that is not in the database but obviously has no data to display, its annoying.
I need a new pair of eyes.
I think the error is here:
$sql = "SELECT * FROM Kittenzz
WHERE KittenID='".$_POST['KittenID']."';";
$result = mysql_query($sql, $connection);
But just in case here is the full code minus the login credentials to the db.
<?php
if(isset($_POST['Find']))
{
$connection = mysql_connect("Login Info Deleted");
// Check connection
if (!$connection)
{
echo "Connection failed: " . mysql_connect_error();
}
else
{ //else 1
//select a database
$dbName="Katz";
$db_selected = mysql_select_db($dbName, $connection);
//confirm connection to database
if (!$db_selected)
{
die ('Can\'t use $dbName : ' . mysql_error());
}
else
{ //else 2
if ($_POST[KittenID]=='')
{
$OutputMessage = 'Must add a Kitten-ID';
}
else
{//exception else
$sql = "SELECT * FROM Kittenzz
WHERE KittenID='".$_POST['KittenID']."';";
$result = mysql_query($sql, $connection);
while($row = mysql_fetch_array($result))
{
$Name = $row['Name'];
$KittenID = $row['KittenID'];
$KittenAge = $row['KittenAge'];
$Email = $row['Email'];
$Comments = $row['Comments'];
$Gender = $row['Gender'];
$Passive = $row['Passive'];
$Playful = $row['Playful'];
$Activity = $row['Activity'];
}
if ($result)
{
$OutputMessage = 'Record Found';
//echo "<p>Record found<p>";
}
else
{
$OutputMessage = 'RECORD NOT FOUND';
}
}//exception else
}//else 2 end
}//else 1 end
mysql_close($connection);
}
?>
if ($result)
{
$OutputMessage = 'Record Found';
}
There is your mistake, that means if the query executed successfully (even with 0 records) you are saying records found. You should only say that if the number of records returned are more than 0.
if (mysql_num_rows($result)>0)
{
$OutputMessage = 'Record Found';
}
But the bigger problem with your code can be solved by this reading
How can I prevent SQL injection in PHP?
This may happen, because if $_POST['KittenID'] is empty, the sql query would look like : SELECT * FROM Kittenzz WHERE KittenID=""; you have to change the above if statement to:
if (!isset($_POST[KittenID]) || empty($_POST[KittenID]) || $_POST[KittenID]=='')
{
$OutputMessage = 'Must add a Kitten-ID';
}
Does anyone know why debugger shows an empty value for $result and $mysqli in that script?
<?php
// show SELECT result as HTML table
function show_table($result) {
if(!$result) {
echo "<p>No valid query result.</p>\n";
return;
}
if($result->num_rows>0 && $result->field_count>0) {
echo "<table>";
// column headings
echo "<tr>";
foreach($result->fetch_fields() as $meta)
printf("<th>%s</th>", htmlspecialchars($meta->name));
echo "</tr>\n";
// content
// row fetch row
while($row = $result->fetch_row()) {
echo "<tr>";
foreach($row as $col)
printf("<td>%s</td>", htmlspecialchars($col));
echo "</tr>\n";
}
echo "</table>\n";
}
}
require_once 'password.php';
// connect to MySQL
$mysqli = new mysqli($mysqlhost, $mysqluser, $mysqlpasswd, $mysqldb);
if(mysqli_connect_errno()) {
echo "<p>Sorry, no connection! ", mysqli_connect_error(), "</p>\n";
exit();
}
// show SELECT result with show_table
if($result = $mysqli->query("SELECT * FROM titles")) {
show_table($result);
$result->close();
}
// disconnect
$mysqli->close();
?>
</body></html>
<?php
?>
hey DOD i think you $mysqldb is null because when its null you never revise any error or warning
i test it in other case such as without user name or password.
$mysqli = new mysqli($mysqlhost, $mysqluser, $mysqlpasswd, $mysqldb);