I've a database with several columns. One of the columns is 'name' and stores the name of the companies in the database. Another column ('id') assign a unique id number to each company. The table has a minimum of 1 and a maximum of 4 companies.
I'd like to show the name of the companies in different places in a html document and therefore need to refer to $company_name_1, $company_name_2 etc. However, these variables should always be in the html document, irrespective of whether the companies are in the database. If not in the database, the value shown should be empty.
How can I automatically define the names of the companies with an array and a loop? I want to expand the database at a later stage hence manually defining the four company names is not an option.
Thanks!
$result = mysql_query($con, "SELECT name FROM companydetails");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['name'];
}
And then I would like to use:
echo $storeArray[0];
to show the first company and
echo $storeArray[1];
to show the second company. Etc.
Before echo first check if the element exists in array. This way there will be no error, something like this
if(count($storeArray)>1)
echo $storeArray[1];
else echo "NA";
if(count($storeArray)>2)
echo $storeArray[2];
else echo "NA";
Related
How do i filter an array list in php to skip one row that has already been added where one of its column value is repited.
I have tried using in_array to filter and array_exist
$getRoom = mysqli_query($con, $sql);
$json_array = array();
while($row=mysqli_fetch_assoc($getRoom)) {
if (in_array($row['hotel_name'], $json_array)) {
}
else {
$json_array = $row;
}
}and
echo json_encode($json_array);
I want hotel name and one of the rooms found
The code (as it is provided right now) is not actually adding a line to $json_array, it's just replacing the current value with the current row. $json_array = $row; needs to become $json_array = $row[];
The second issue is that in_array isn't going to recursively search the second level of array so it's never going to find $row['hotel_name']. I would recommend restructuring your arrays a bit (depending on how your downstream code is going to use them) to something more like this:
while($row=mysqli_fetch_assoc($getRoom)) {
$json_array[$row['hotel_name']][] = $row;
}
This will group your data so that each hotel is only listed once in the top level array but you keep all of the details about the various rooms. If you really only need the distinct hotel names then you should restructure your database query to only return unique values using a distinct clause.
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 have created a simple php code to print the result of a mysqli query no matter what query it is and how many columns and rows in there. My simplified php code is:
$result = $mysqli->query ($query);
while ($row = $result->fetch_row()) {
if ($row["status"] == "0") continue;
foreach ($row as $cell) {
echo $cell;
}
echo "\n";
}
Now I want to omit some column (ex: column named "status") to be printed, but I have to include the column "status" in the query because I need to check this "status" value to determine whether if the entire row will be printed or not (the check is a little more complicated than that and it's impractical to do it on the query itself). But if the row is printed, I don't want the column "status" is printed along in the table. But I have no means to know whether the $cell I get inside foreach is named "status" or not, and I have several other columns that have similar value like "status" so I can't check based on value either. How can I do this? I've read on php mysqli::fetch_row() manual but it doesn't seem that each of the $cell is an object that can be applied method to ask what's its column name. Thanks.
So only select the rows with status !=0
SELECT a,b,c FROM user WHERE status!=0;
Update:
unset($row["status"]);
var_dump($row);
OK, this is an amateur question but I am having the most trouble displaying the values of my database. I want to display the entire 'phone' column so my mysql query is this:
$result = mysql_query('SELECT phone FROM contactList WHERE phone != 1') or die(mysql_error());
$row = mysql_fetch_array($result)
My PHP script is as this:
while(isset($rows))
{
echo $rows['phone'] . "html break tag";
}
It looks like though I'm only getting the first result instead of looping through the entire column.
Do I need to increment the ID in my loop and get value via ID? The only problem with that is my auto-incremental ID column starts # 130 and skips some numbers here and there.
try this instead
While($row = mysql_fetch_array($result))
echo $row['phone'];
That should help you looping through the whole array.
I'm fairly new to PHP and MySQL (experienced with other languages). Basically I want to load data from a row relative to its id stated in the URL. Example, load the 3rd row when "index.php?id=3"
Heres what I've managed to do so far: http://pastie.org/1436865
I pretty sure this question has been asked a million times over, but I don't know what term to search far and have not been able to find anything so far :S
Thanks guys
The part id=3... is called query string and you can access it using the $_GET array (see PHP: Predefined Variables).
BTW: It does not make sense to load the 3rd row from a database table, because the rows in the database table do not have an ordering (so instead of a list of rows, a set of records is a more appropriate analogy in this case). Records are usually identified by a so called primary key and you have to make sure yourself, that your database tables have a primary key.
Or if you prefer array's instead of objects try something like this:
$query="SELECT * FROM table WHERE id=".(int)$_GET['id']." LIMIT 1";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row['column'];
}
or if you like indexed columns
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo $row[0];
}
or both (column names and indexes):
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
echo $row[0].'-'.$row['column'];
}
You can even use this if you need only one row:
$result = mysql_query("SELECT * FROM table WHERE id=".(int)$_GET['id']);
$row = mysql_fetch_row($result);
echo $row[0];
Based on all these not-so-clear but working examples, you can make a function:
function getRow($query){
$res = mysql_query($query);
if (!$res) {
trigger_error(mysql_error." in ".$query);
return array();
}
$row = mysql_fetch_assoc($res));
if ($row) return $row;
return array();
}
then store it into some library file, then include this file into your script and call with just single line
$data = getRow("SELECT * FROM tablename WHERE id=".intval($_GET['id']));
Also note Oswald's note, it's very important. You can't and you shouldn't rely on the row's relative position as there is no position at all. A DB table is a heap, not ordered list.
Use certain unique field value to address certain row. That's the way to go
The simplest way is
$id = (int) $_GET['id'];;
$result = mysql_query("SELECT * FROM tablename WHERE id=".$id);
while($res = mysql_fetch_object($result)){
echo $res->columnname;
}
Of course, you should check input parameters, etc. This is only approach.