Mysqli get array from database and format to string [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to get array of fields from database:
$query=("SELECT firstname FROM users");
$res = $connect->query($query);
Target is to display first name of every users:
echo $firstname
Output:
Firstname1,Firstname2,Firstname3
I know should be easy, if I just knew more.

For getting all records or a set of records you have to fetch each records inside a while loop.
$query=mysqli_query($connect,"SELECT firstname FROM users"); // execute the query
$string="";
while ($row = mysqli_fetch_array($query)) // fetch each records
{
$string.=$row['firstname']).','; // append the firstname to a variable
}
echo $string; // output the constructed variable with all firstnames

Related

what do this code do? this code from thingengineer? the array one i can't understand [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
this code from thing engineer
$cols = Array ("id", "name", "email");
$users = $db->get ("users", null, $cols);
if ($db->count > 0)
foreach ($users as $user) {
print_r ($user);
}
can someone explain it to me? thanks a lot
Looks like Codeigniter.
Fetches 3 columns from table "users" of the active database and if at least one row was provided, iterate over them and print the row.

I am trying to get the user id which is created automatically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
$sql4 = "SELECT userid, userName FROM $user_table WHERE userName= '$userNames'";
$result = $conn->query($sql4);
$_SESSION['myUser'] = $result['userid'] ;
I have most code I can find but nothing works
Assuming these lines of code come immediately after executing an insert of a new user record and you are using PDO. You could try:
$last_id = $conn->lastInsertId();
$_SESSION['myUser'] = $last_id;

How to print data from SQL database to HTML table based on query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
so I have records in my database that I would like to print to a HTML table IF they belong to the user.
$sql="SELECT * FROM ".$tbl_name." WHERE Username='".$username."'";
The table would read :
Date, Room, Period , Cancel
With cancel being a red cross designed to delete that specific entry from the database :
Booking ID, Date, Period,Type,RoomID, Username are the fields in the database.
How would I go about doing this? I was thinking echoing a table using a for loop but I wouldn't know where to begin?
Try:
$query_rsSearch = "SELECT * FROM ".$tbl_name." WHERE Username='".$username."'";
$rsSearch = mysql_query($query_rsSearch) or die(mysql_error());
$row_rsSearch = mysql_fetch_assoc($rsSearch);
$totalRows_rsSearch = mysql_num_rows($rsSearch);
do {
echo $row_rsSearch['Date']." | ".$row_rsSearch['Room']." | ".$row_rsSearch['Period'];
} while($row_rsSearch = mysql_fetch_assoc($rsSearch));

How to display row from page ID [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am very new and trying to get from the url ie
device.php?id=2
and use the id number multiple times.
can anyone tell me how to display this?
Thanks in advance
1) Get the ID.
$id = $_GET["id"];
2) Search for $id in your database, using MySQL functions like mysqli_fetch_array() etc.
3) Echo the values you fetched, using simple HTML and PHP.
$id = $_GET['id'];
$sql= mysql_query("select * from tableName where id = $id ");
while($row = mysql_fetch_array($sql))
{
echo $row['columnName'];
}
The variable should be in $_GET["id"].
<?php
echo $_GET['id'];
?>

How to select all fields where a string is common in all the tables in a mysql database? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a mysql database where there is 3 tables 'groupstage', 'quarterfinal,' semifinal. All of these table have a column named 'Date'. Now I want to write a query from where I can get all the fields from those tables with a specific date? what should i write it in php?
To get what you want, you can write a query which join the 3 tables like :
SELECT *
FROM groupstage, quarterfinal, semifinal
WHERE groupstage.date = quarterfinal.date AND quarterfinal.date = semifinal.date
AND date = "YOUR DATE"
In php you should just create a var $query = "My query above"

Categories