selecting in php WHERE x = $array[index]? - php

I've tried searching for answers to my problem but no one else seems to have had this problem! I'm basically trying to select in php using the WHERE statement, I want to compare my $ID to variables stores in an array called $resultAddID2. This array has values [1,2,1]; if I try accessing $resultAddID2[0] it works fine, but if I try to do it with $resultAddID2[1] or $resultAddID2[2] it doesn't work at all! I'm sure this is something silly but for the life of me I just can NOT figure it out! Any help would be much appreciated, thank you.
Here's the part where I try to do this:
$resultAddID = mysql_query("SELECT ADDRESS_ID FROM hospital");
while($resultAddID1=mysql_fetch_array($resultAddID)){
$resultAddID2[]=$resultAddID1['ADDRESS_ID'];
for ($i = 0; $i <= 2; $i++) {
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = $resultAddID2[]");
$resultAddTm= mysql_fetch_array($resultAddT);
$resultAddT2[]=$resultAddTm['GOVER_ID'];
}
}
$response["hospADD"]= $resultAddT2;

You should add {}'s like
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = {$resultAddID2[$i]}");
It would be better if you did something like this because you are querying the second item too many times
$resultAddID = mysql_query("SELECT ADDRESS_ID FROM hospital");
while($resultAddID1=mysql_fetch_array($resultAddID)){
$resultAddID2[]=$resultAddID1['ADDRESS_ID'];
}
for ($i = 0; $i <= 2; $i++) {
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = {$resultAddID2[$i]}");
$resultAddTm= mysql_fetch_array($resultAddT);
$resultAddT2[] = $resultAddTm['GOVER_ID'];
}
$response["hospADD"]= $resultAddT2;
OR simpler yet:
$resultAddID = mysql_query("SELECT ADDRESS_ID FROM hospital");
while($resultAddID1=mysql_fetch_array($resultAddID)){
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = {$resultAddID1['ADDRESS_ID']}");
$resultAddTm= mysql_fetch_array($resultAddT);
$resultAddT2[] = $resultAddTm['GOVER_ID'];
}
$response["hospADD"]= $resultAddT;

Related

PHP Number Ticket System?

So, I'm trying to make a ticket system. So a users will be assigned 1000 tickets each, I will then generate a number from 1 to 5000, I then need it to select the user.
The way I have done it was made an array then looped 5000 times and assigned each user a ticket, however this doesn't work with really big numbers like 5,000,000. So I'm trying to think of the best way to do this and I'm unsure how.
Any advice?
$users = array();
$ticketNum = 0;
$result = $MySQL->query("SELECT * FROM `users` ");
while($row = $result->fetch_assoc()){
$i = 0;
while($i < $row['points']){
$i++;
$ticketNum++;
$ar = array("ticketNum" => $ticketNum, "username" => $row['username']);
array_push($users, $ar);
}
}
$winningTicket = 2500;
foreach($players as $ar){
if($winningTicket == $ar['ticketNum']){
//winner
}
}
I'll need more code to suggest a perfect solution, and also know why you want to assign 1000 tickets to a person?
But you could assign a from-to key instead of looping ALL of the numbers through, like
$users[$userNumber]["from"] = 1;
$users[$userNumber]["to"] = 6;
$findNumber = rand(1,6);
$foundUser = false;
foreach($users as $userNumber => $user) {
if ($user["from"] <= $findNumber && $user["to"] >= $findNumber) {
$foundUser = $user;
break;
}
}
if ($foundUser) {
print "Hooray, someone just got a ticket.";
}
but it does sound like a job you would solve in a different manner, maybe through your database.
Edit: I wrote the above before you added your code example, In your case I would probably do the following.
$result = $MySQL->query("SELECT username FROM users ORDER BY RAND() LIMIT 1");
$winningUser = $result->fetch_assoc();
print "We got a winner: ".$winningUser["username"];

Mysqli query doesn't work with id from another table

I have this php script.
$cwZ = count($wiegen_zutat);
$cwM = count($wiegen_menge);
$cwS = count($wiegen_schritt);
if($cwM == $cwS and $cwM == $cwZ and $cwZ == $cwS){
for($x = 0; $x < $cwZ; $x++){
$aktZuat = $wiegenZutat[$x];
$qr = "SELECT ID_Zutat FROM Zutaten WHERE Name='$aktZutat' LIMIT 1";
$id_get = mysqli_query($verbindung,$qr );
$id = mysqli_fetch_array($id_get);
$zuatenID = $id['ID_Zutat'];
echo $id['ID_Zutat'];
echo $zutatenID;
$sql3 = "INSERT INTO Wiegen (ID_Zutat, Menge) VALUES ('$zutatenID', '$wiegenMenge[$x]')";
$wiegenEintragen = mysqli_query($verbindung, $sql3);
}
}
$wiegen_zutat, _menge, _schritt are all three arrays which contain the information from my form.
I go through the first array, and check the variable against a table which contains the ingredients for my website. I want to get the id of a ingredient which was added some steps before and add it into another table.
The problem is that neither the echos or the query are working.
What am I missing?
Please don't get confused by the name of the variables, I'm german :)
Best regards

PHP Repeat a func when variable doesn't meet requirement

I'm newbie at PHP (I'm learning it) so could someone help me to fix my script?
$total = mysql_num_rows(mysql_query("SELECT * FROM files;"));
$rand = rand(0,$total);
$check = mysql_num_rows(mysql_query("SELECT * FROM files WHERE id=".$rand.";"));
do {
$rand;
}
while($check < 1);
I write this code to get a random column ID from MySQL ( I tried "ORDER BY RAND()" but it's too slow), When this ID doesnt exist in MySQL's table it will repeat this job until it find a exist ID with this way:
$check = mysql_num_rows(mysql_query("SELECT * FROM files WHERE id=".$rand.";"));
But I think I was wrong in coding it. Please help me to fix. Thanks.
You'll need to place the query inside the loop otherwise it won't actually check again. See below:
$total = mysql_num_rows(mysql_query("SELECT MAX(id) FROM files;"));
$check = 0;
do {
$rand = rand(0,$total);
$check = mysql_num_rows(mysql_query("SELECT * FROM files WHERE id=".$rand.";"));
}
while($check < 1);
Is it ok?
do {
$rand = rand(0,$total);
}
while(mysql_num_rows(mysql_query("SELECT * FROM files WHERE id=".$rand.";")) < 1);

PHP loop not returning all possible MySQL query results

I am new to php & MySQL.
I have a db that contains two tables called person (property owner) and property.
There is a one-to-many relationship between the tables. For instance, a person can own many properties.
I am using php to retrieve data from the db (I have already established a connection with the db).
What I am trying to achieve is this: if a query is carried out for a person's name - (whether it is fname or lname) - then a list of all the properties owned by this person will be displayed in my search results form. The output will be shown as lname, fname, property number, property road, property zipcode.
However, with this code I am only getting two results output per property owner name. :
$request = mysql_query("SELECT person.fname, person.lname, property.number, property.road, property.zipcode,
FROM person p
INNER JOIN property py
ON p.id = py.p_id
WHERE p.fname LIKE '%$search%'
OR p.lname LIKE '%$search%'
ORDER BY p.lname");
$number = mysql_num_rows($request);
if ($number == 0){
$result .= 'No results'.' '.$search;
} else {
$propertyinfo= array();
$count = 0;
$real = false;
$real_within_array = 0;
while ($nrow = mysql_fetch_array($query)){
$lname = $nrow['lname'];
$real = false;
for ($l = 0; $l < count($propertyinfo); $l++)
{
if ($propertyinfo[$l][0] == $lname)
{
$real = true;
$real_within_array = $l;
break;
}
}
if ($real)
{
$fname = $nrow['fname'];
$lname = $nrow['lname'];
$number = $nrow['number'];
$road = $nrow['road'];
$zipcode = $nrow['zipcode'];
$propertyinfo[$real_within_array][1][1] = $fname;
$propertyinfo[$real_within_array][2][1] = $lname;
$propertyinfo[$real_within_array][3][1] = $number;
$propertyinfo[$real_within_array][4][1] = $road;
$propertyinfo[$real_within_array][5][1] = $zipcode;
}
else
{
// Get all the data from the row.
$fname = $nrow['fname'];
$lname = $nrow['lname'];
$number = $nrow['number'];
$road = $nrow['road'];
$zipcode = $nrow['zipcode'];
$propertyinfo[$real_within_array][1] = $fname;
$propertyinfo[$real_within_array][2]= $lname;
$propertyinfo[$real_within_array][3][0] = $number;
$propertyinfo[$real_within_array][4][0] = $road;
$propertyinfo[$real_within_array][5][0] = $zipcode;
$count++;
}
}
Example: Adam Smith owns 4 properties, but I will only get a result of two of his properties instead of all 4 in a list.
Please help. I have tried my very best to sort this out but I seem to have stumbled upon a difficult problem.
Your help is much appreciated.
Thank you in advance.
I think it's a logical error;
when you defined $propertyinfo=array(); it's size is 0, therefore the code withing the block of
for ($l =0; $l< count($propertyinfo); $l) will only start running after some values has been assigned to $propertyinfo.
What is $uqery? Maybe:
while ($nrow = mysql_fetch_array($request)){
You would see this with:
error_reporting(E_ALL);
ini_set('display_errors', '1');
As for all the other stuff it's impossible to tell without knowing what $propertyinfo looks like. Very convoluted looking so I don't follow.
Try using while(($nrow=mysql_fetch_assoc($query))!=null) to cycle on your query results

Improve & Functionalize Queried Array

Need a little help, advise, or link to an example or useful tutorial so I can learn this. As I am very new to programming in general.
I have 11 Select boxes in a form named 'ore1' thru 'ore11' that send data up to $_POST.
This little block turns that into an array that is used in another function.
//function calculateValue($sellValue){ -- add when you figure it out
if(isset($_POST['submit'])){
$i = 11 //total number of select boxes
$pdquery = "SELECT * FROM ore
WHERE '".$_POST['ore1']."'
LIKE id";
$result = mysql_query($pdquery);
$oredata1 = array();
while ($row = mysql_fetch_row($result)) {
$oredata1 = $row; }
}
else {}
}
I'd like like to be able to use this one bit of code with all 11 Select boxes without having to copy it 11 times by getting
.$_POST['ore#']. //5th line
$oredata# = $row; //10th line
to replace # with the appropriate # depending on the select box it is used on.
Any tips?? Thanks in advance.
In your HTML:
<select name="ore[]">
...
</select>
<select name="ore[]">
...
</select>
In your PHP
if(isset($_POST['submit']) && isset($_POST['ore'])){
foreach($_POST['ore'] as $ore){
$ore = (int)$ore;
$pdquery = "SELECT * FROM ore WHERE id = $ore";
...
}
}
Also do not use mysql_* function since deprecation process has begun
for ($i = 1; $i <= 11; $i++) {
$ore = 'ore'.$i;
$pdquery = "SELECT * FROM ore WHERE '".$_POST[$ore]."' like id";
...
execute it in a for-loop - from 1 to 11. Then you can use the variable $i to access stuff.
// I assume that there is some MySQLi object called $mysqli already present. You'll have to create it if it does not
if(isset($_POST['submit'])){
$pdquery = 'SELECT * FROM ore WHERE id LIKE ?';
$oredata = array();
if($stmt = $mysqli->prepare($pdquery)) {
for($i = 1; $i < 12; $i++) {
$ore = 'ore' . $i;
if(empty($_POST[$ore])) {
continue;
}
$oreValue = (int)$_POST[$ore];
$stmt->bind_Param('i', $oreValue);
$stmt->execute();
$oredata[$i] = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
}
}
$stmt->close();
}
This does NOT take into consideration the fact that there might be more than just those eleven inputs. Using an array on HTML side would certainly be cleaner (And you'd just have to replace the for-loop with a foreach-loop and use the key instead of $ore)

Categories