From my database my code fetch (dynamic) two values (varChar) from 2 tables and I want to find is there at least one common number between the 2 values. If at least one number is common, my code will fetch rows and display the rows values (while loop).
I am new to php and new to 'stackoverflow'. My code works fine and fetch results as I desired. The problem 1.) will it work rightly all the time? 2.) I feel my code seems to be ugly and it can be refined.
Here my code:
$sql = "SELECT * FROM table1 WHERE userId = '".$_SESSION['userId']."'"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $nearBy = $row['nearBy']; $area_1 = explode(',', $nearBy); //Here, for example I am getting the result - 1,4,5,9,12
and
$sql = "SELECT * FROM table2 ORDER By id";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$area_2 = explode(',', $row['areas']); // 9,12,24,67
```
foreach ($area_1 as $value1) {
$value1;
foreach ($area_2 as $value2){
$value2;
if($value1 == $value2){
if(!empty($result)){
```
```
$sql10 = "SELECT * FROM table2 ORDER By id";
$result10 = $conn->query($sql10);
while($row10 = $result10->fetch_assoc())
```
`
// Finished, the idea is - I converted the first result (area_1) into an array by explode and I did the same to 'area_2'. Then I looped through both the arrays to find out at least any common number. If the result is not empty then at least one common number is available and the specific row details will be displayed. Like wise all the rows will be displayed one by one.
I again wish to remain the coders that I am very new to php coding. Thanks in advance for your upcoming helps.
My code works fine and fetch results as I desired.
The problem 1.) Will it work rightly all the time?
2.) I feel my code seems to be ugly and it can be refined.
Related
I want to get some elements from a database (phpmyadmin). The database "top" is set up like:
ID || Name
____________
1 || Home
2 || About
3 || Users
4 || Admin
...
I use the following Code to get the information:
<?php
$sql = "SELECT ID
FROM top
WHERE Name='Users' OR Name='Admin'";
$query = mysqli_query($dbconnect, $sql);
$oq = mysqli_fetch_assoc($query);
if(in_array($_GET['ID'], $oq)){
//Execute some Code
}
?>
If I execute the sql-code I get the result I want (ID 3 and 4) but in "$oq" there is only one element (the first one -> 3) left. Therefore the Code I want to execute is only displayed once.
You have to use a while loop:
$oq = array();
while($row = mysqli_fetch_assoc($query)) {
echo $row['ID'];
$oq[] = $row['ID'];
}
As you have done it you're only fetching one row in $oq. You will have to add each value to the array $oq in order to use in_array() for the test.
There are some other techniques you can use here, for instance you could fetch everything (an array of arrays) and loop through the array, depending on your needs.
You will have to use array to use have multiple DB values
$values=array();
while($row = mysql_fetch_array($result)){
$values = array('ID'=>$row['ID']);
}
I have two tables. First one contains first names, second one contains last names. (I could do it with one table with two columnes, but I'm using this way for other reason)
I want to make all possible combinations.
So if there are first names: John, Zed, Marko, and last names: Abbot, Zang I would like to get output like:
JohnAbbot
JohnZagn
ZedAbbot
ZedZang
MarkoAbbot
MarkoZang
I did something similar with For loop and alfanumeric signs. I was able to do it with 6 nested for loops, but I can't get this to work with while loop.
this is the code I use:
$query_name = "SELECT name FROM names";
$results=mysqli_query($connect, $query_name);
$query_surname = "SELECT surname FROM surnames";
$results2=mysqli_query($connect, $query_surname);
while ($row = mysqli_fetch_assoc($results)) {
while ($row2 = mysqli_fetch_assoc($results2)) {
echo $row['name'].$row2['surname']."<br/>";
}
}
with this I got only combinations with 1 name and all surnames.
after I added one more echo in first while loop like this:
while ($row = mysqli_fetch_assoc($results)) {
echo $row['name']."<br/>";
while ($row2 = mysqli_fetch_assoc($results2)) {
echo $row['name'].$row2['surname']."<br/>";
}
}
with this I got:
first name from names table
combinations with first name and all last names (surnames)
all others names
I did not get all the combinations with all names and surnames.
What am I doing wrong?
Thank you in advance
(sorry for my bad english)
That's because fetching is only done once per handle, so when your inner loop reaches the end of records, it will never go back to the first one - as you would need it to - but stay at the end and return false.
If your result sets are not million-row ones, try fetching both only once, then combine the arrays and foreach-loops which will obediently start from the beginning each time:
$a1 = array();while ($row = mysqli_fetch_assoc($results)) $a1[]=$row;
$a2 = array();while ($row = mysqli_fetch_assoc($results)) $a2[]=$row;
foreach($a1 as $row1) {
foreach($a2 as $row2) {
echo $row1['name'].$row2['surname']."<br/>";
}
}
You can do with a simple query
select a.name, b.surname
from names as a
FULL JOIN surnames as b
I am trying to return a json from a MySQL query to use it with xCode but I cannot get an array of several objects with multiple fields. I have read the documentation on php.net and over here, but I still can't get it.
1) Let's say I have a MySQL table with 3 rows (for example 3 people). Each row contains 3 fields (lastname, firstname, dateOfBirth):
$result = mysqli_query($mysqli, $sql) // where $sql = "SELECT * FROM tbl_syncList"
$resultArray = array();
while ($row = $result->fetch_array()) {
$resultArray[] = $row["lastname"];
}
echo json_encode($resultArray); // --> return "["Lastname1", "Lastname2"...] - that's okay, I understand I return the value of the key "lastname"
I don't know how to get an array with the entire rows (all the fields at once), like:
[["Firstname1", "Lastname1", "dateOfBirth1"], ["Firstname2", "Lastname2", "dateOfBirth2"],...]
I tried to replace
$resultArray[] = $row["lastname"];
with:
$resultArray[] = $row;
but it just gives the world...
"array"
...with no content. Does anyone have an idea?
Thanks a lot!
Are you sure the array is properly populated to begin with? print_r($resultArray) and see what you have there with = $row (which is correct).
I use a mysql database. When I run my query I want to be able to put each row that is returned into a new variable. I dont know how to do this.
my current code:
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution=$row['solution'];
}
?>
The thing is that check num rows can return a row of an integer 0-infinity. If there are more solutions in the database how can I assign them all a variable. The above code works fine for 1 solution, but what if there are more? Thanks.
You can't give each variable a different name, but you can put them all in an array ... if you don't know how this works I suggest looking at a basic tutorial such as http://www.w3schools.com/php/php_arrays.asp as well as my code.
A very simple way (obviously I haven't included mysql_num_rows etc):
$solutions = array()
while($row = mysql_fetch_assoc($result)) {
$solutions[] = $row['solution'];
}
If you have three in your result solutions will be:
$solutions[0] -> first result
$solutions[1] -> second
$solutions[2] -> third
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$solution = array();
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution[]=$row['solution'];
}
?>
I need to have a single row of data "printed out" through php.
So, take this example from w3schools:
http://www.w3schools.com/PHP/php_mysql_select.asp
There is a cicle that goes through all the rows ( in this case, 2) and prints them out. The end result is:
Peter Griffin
Glenn Quagmire
What I want is to be able to select row 1 or 2 (or more) and just have that row of data selected. Then I could say something like (I know this doesent work, just an example):
echo $row["Name",2];
And get:
Glenn Quagmire
I believe I have to get a special parameter in mysql_fetch_array, but I cant find it anywhere, and I bet its something really simple. Please help me out, full examples/tutorials/guides links are preferred.
you have 2 options. First is edit your SQL query like exmaple below this will return just 2nd row from database.
$result = mysql_query("SELECT * FROM Persons" WHERE id = 2);
Or during the foreach loop fetch all result into another array.
$result = mysql_query("SELECT * FROM Persons");
$rows = array();
while($row = mysql_fetch_array($result))
{
$rows[] = $row['FirstName'] . " " . $row['LastName'];
}
As far as i know mysql doesnt have a function to return the entire query as an array.
So you can switch to using PDO (recommended):
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetchAll();
echo $result[1]['name'];
or if you must use the mysql_functions just create an array with the loop:
while($row = mysql_fetch_array($result)) {
$result[] = $row;
}
echo $result[1]['name'];
fetchall the results into an array and print the row you want.