How can I acces first value and second value of this foreach seperatly?
foreach($database->query('SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1') as $resultRandom) {
}
You could do something like this from what I think you are saying:
$query = "SELECT `id`, `firstname`, `lastname` FROM `staff` WHERE `categorie_bit` = 1";
$rows = [];
while ($result = mysqli_fetch_assoc($database->query($query)))
{
$rows[] = [
"id"=>$result['id'],
"firstName"=>$result['firstname'],
"lastName"=>$result['lastname']
];
}
var_dump($rows);
<?
$query= "SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1";
if ($result=mysqli_query($con,$query))
{
$rowcount=mysqli_num_rows($result);
print "<table width=600 border=1>\n";
while ($get_info = mysqli_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td>$field</td>\n";
print "</tr>\n";
}
print "</table>\n";
mysqli_close($link);
?>
Related
I have upgrade my code. In the old code I had 2 functions: display_maker_success() and display_maker_fail() but I realised I can combine those two functions into one display_maker_stat() by putting more arguments into the function. I like it very much!
Is better way to do this? I want more code reuse.
function display_maker_success($link, $userid){
$status="closed";
$result="completed";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 6;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If ($isempty ==0) {
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>Completed</td></tr>";
};
echo "</table>";
};
};
function display_maker_fail ($link, $userid) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>fail</td></tr>";
};
echo "</table>";
};
};
function display_maker_stat ($link, $userid, $reuslt, $limit) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
};
echo "</table>";
};
};
Try the below,
Also there were few errors in your code and i have corrected them.
function display_maker_stat($link, $userid, $reuslt = 'fail', $limit)
{
$status = "closed";
$html = '';
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$query = mysql_query($sql, $link);
if (mysql_num_rows($query) != 0) {
$html .= "<table border=1>";
$html .= "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
$html.= "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
}
$html.= "</table>";
echo $html;
}
else {
echo "No Record";
}
}
Read about OOP
Whoops firgured otu where my parse error was. I had my semi-colon inside my first query.
So essentially I am trying to query with three different select statements in the same PHP script. Is this possible? (Last question I promise, after this I think the basics should get me a few weeks without having to ask more)
<?php
include("server_connect.php");
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querythree) or die(mysql_error());
?>
Part Two
Ok so I changed the code as suggested and tried to add it into a table. I'm still getting
Parse error: syntax error, unexpected '$results1' (T_VARIABLE) in C:\xampp\htdocs\3718\assign5SELECT.php on line 7
I tried it without the table and it is still the same error. Is there something I am missing? Here is the updated code with the new variables.
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results1 = mysql_query($query) or die(mysql_error());
echo "Column One, Column Two, Column Four : </br>";
echo "<table border=\"1\">\n";
while ($row1 = mysql_fetch_assoc($results1)) {
echo "<tr>\n";
foreach($row1 as $value1) {
echo "<td>\n";
echo $value1;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results2 = mysql_query($querytwo) or die(mysql_error());
echo "Column One, Column Two, Column Five : </br>";
echo "<table border=\"1\">\n";
while ($row2 = mysql_fetch_assoc($results2)) {
echo "<tr>\n";
foreach($row2 as $value2) {
echo "<td>\n";
echo $value2;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results3 = mysql_query($querythree) or die(mysql_error());
echo "Column 4 has this many 1989s : </br>";
echo "<table border=\"1\">\n";
while ($row3 = mysql_fetch_assoc($results3)) {
echo "<tr>\n";
foreach($row3 as $value3) {
echo "<td>\n";
echo $value3;
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results = mysql_query($querythree) or die(mysql_error());
The problem you are encountering is that you are overwriting your results by using the same variable name.
Example:
$Var = "test";
echo $Var; // Will output "test"
$Var = "Another String";
echo $Var; // Will output "Another String" rather than "test";
So append:
$Results_a = ...;
$Results_b = ...;
$Results_c = ...;
So you can work with your variables easily, another thing you could look at is SQL joins: http://dev.mysql.com/doc/refman/5.0/en/join.html to cut down your amount of seperate queries
You need to save the queries to a different variable:
<?php
include("server_connect.php");
mysql_select_db("rnissen");
$query = "SELECT column_one, column_two, column_four FROM tbltable;"
$results1 = mysql_query($query) or die(mysql_error());
$querytwo = "SELECT column_one, column_two, column_five FROM tbltable WHERE column_five = 1989";
$results2 = mysql_query($querytwo) or die(mysql_error());
$querythree = "SELECT COUNT(column_five) FROM tbltable WHERE column_five = 1989";
$results3 = mysql_query($querythree) or die(mysql_error());
?>
However, this is a more convenient way to do perform multiple queries using mysqli_multi_query:
Example from the docs:
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if (mysqli_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}
} while (mysqli_next_result($link));
}
Edit:
For your updated question, you are missing a semicolon here:
$query = "SELECT column_one, column_two, column_four FROM tbltable";
I need to do a search in each $row2 variable, so i could find if there is columns that are not equal to $row1 column.
This is what I tried,
$result1 = mysql_query('SELECT gpu FROM ng2s3_content');
$result2 = mysql_query("SELECT gpu FROM bigc3_gpu");
while ($row1 = mysql_fetch_array($result1)) {
while ($row2 = mysql_fetch_array($result2)) {
foreach ($row2 as $ar) {
if ($ar == $row1[0]) {
}
else {
echo $ar;
}
}
}
}
looks like you want distinct values from your database ,
use mysql query , that will return a resource with distinct values ,
$result = mysql_query('SELECT gpu FROM ng2s3_content UNION SELECT gpu FROM bigc3_gpu');
while ($row = mysql_fetch_array($result)) {
echo $row['gpu'];
}
The query I have below will only show me one result even if there are multiple matching entries (completely or partially matching). How do I fix it so it will return all matching entries:
//$allowed is a variable from database.
$sql = "SELECT `users`.`full_name`, `taglines`.`name`, `users`.`user_id` FROM
`users` LEFT JOIN `taglines` ON `users`.`user_id` = `taglines`.`person_id`
WHERE ( `users`.`user_settings` = '$allowed' ) and ( `users`.`full_name`
LIKE '%$q%' ) LIMIT $startrow, 15";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$num_rows1 = mysql_num_rows($result);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$person = htmlspecialchars($row['full_name']);
}
}
}
print $person;
Because your overwriting $person on each iteration.
Hold it in a $person[] array if your expecting more then one. Then loop through it with a foreach loop when you intend to output.
Not related but your also querying twice, you only need 1 $result = mysql_query($sql);
Update (Simple Outputting Example):
<?php
$person=array();
while($row = mysql_fetch_array($query)){
$person[] = array('full_name'=>$row['full_name'],
'email'=>$row['email'],
'somthing_else1'=>$row['some_other_column']);
}
//Then when you want to output:
foreach($person as $value){
echo '<p>Name:'.htmlentities($value['full_name']).'</p>';
echo '<p>Eamil:'.htmlentities($value['email']).'</p>';
echo '<p>FooBar:'.htmlentities($value['somthing_else1']).'</p>';
}
?>
Or an alternative way to is to build your output within the loop using concatenation.
<?php
$person='';
while($row = mysql_fetch_array($query)){
$person .= '<p>Name:'.$row['full_name'].'</p>';
$person .= '<p>Email:'.$row['email'].'</p>';
}
echo $person;
?>
Or just echo it.
<?php
while($row = mysql_fetch_array($query)){
echo '<p>Name:'.$row['full_name'].'</p>';
echo '<p>Email:'.$row['email'].'</p>';
}
?>
$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)){
// PRINT COLUMN NAMES WITH EMPTY VALUE
}
how can i do this?
thanks
$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
foreach ($row as $columnName => $value)
{
if (empty($value))
{
echo $columnName;
}
}
}