I wanted to fetch the data from the database and wants to store into an array and after storing the data into the array i want to access particular index of the array.
I'm a java developer need to do this in php (which I don't know much).
Basically there are 250 strings in a table i wanted to fetch those 250 strings into and array and wants to access some particular row.
for example :
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
**$uname100 = $row[100]; // this is not getting assigned to $uname100 variable**
**echo $row[100]; // here this is not printing**
**echo $uname100; // not even this printing**
mysqli_close($con);
?>
Please check the bold part in the code and help me out. I'm new to php so please dont panic over this.
And also wanted to do something like this :
$ctr=0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$uname[$ctr++] = $row['FirstName']; // AND USING THIS OUTSIDE LOOP
}
echo $uname[90];
Use array construction
$counter=0;
$data = array();
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$data[] = $row['FirstName'];
$counter++;
}
print_r($data);
First solution
$counter=0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$counter++;
$varName='uname'.$counter;
$$varName=$row['FirstName'];
}
echo echo $uname100;
You cannot access the variable $row[100] outside the while loop.
Also $row will not contain an index 100.
It will be containing only $row['FirstName'] or $row['LastName'] inside the while loop
Answer for second part:
$uname = array();
$ctr=0;
while($row = mysqli_fetch_array($result)) {
....
....
$uname[$ctr++] = $row['FirstName']; // AND USING THIS OUTSIDE LOOP
}
if(isset($uname[90])) echo $uname[90];
Related
I am using this code to get that data from SQL Server database using PHP
<?php
foreach ($dbDB->query($query) as $row) {
echo "<tr>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['OrderNumber'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['ShipDate'] . "</td>";
echo "<td>" . $row['ProducedDate'] . "</td>";
echo "</tr>"; }
?>
I am trying to replace these multiple lines but storing the columns' names in a string for example $_POST['SelectedColumns'].
The values coming into post as comma separated string, For example : Country,OrderNumber,Region,ShipDate,ProducedDate
I have tried this solution but still not working for me.
<?php
$ser="********";
$db="******";
$user="******";
$pass="******";
$query = 'SELECT '.$_POST['SelectedColumns'].' FROM reporting.REPORT_ALL';
$dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=*******;Database=******;Port=1456", $user, $pass);
$row = $_POST["SelectedColumns"];
$rows = explode(",",$row);
/*Here I have the another html code independent of this part */
foreach ($dbDB->query($query) as $dataRow) {
echo "<table>";
echo "<tr>";
foreach ($rows as $r ) {
echo "<td>" . $dataRow[$r] . "</td>"; }
echo "</tr>";
echo "</table>"; }
?>
Any suggestions please ?
<?php
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE'";
$result = mysqli_query($con , $q1 ) ;
$data = array();
while($row = mysqli_fetch_array($result))
{
$data[$row["term_no"]][] = array(
'code' => $row["code"],
'grade' => $row["grade"]
);
}
echo '<table width="200" border="1">';
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "</tr>";
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $data) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
echo "</table>";
?>
I have this code and it work very well but I faced problem when I want to add more column .
I tried to add fourth column(echo "<td>" . $row["crd"]. "</td>"; ) but there is no result .It give me empty cells. how I can do that?
I want add add this echo "<td>" . $row["crd"]. "</td>"; column to my code.
As mentioned in the comments, there are two errors that have been noticed.
You are re-declaring $data in your second foreach loop
You don't have $row initiated anywhere, and atempting to echo $row["crd"] will result in an empty cell.
Proposed Solution:
Change the name of the $data value in the foreach loop to $row and hence solve both problems at the same time:
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $row) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
And when you add echo "<td>" . $row["crd"]. "</td>"; now it should echo the value stored in the $row array (as long as the value was extracted from the table in the database in the first place of course).
Let me know if this worked for you.
So i got a little problem i just cant figure out to solve..
So i got a While loop that is printing from my database:
$resultArray = array();
$sql = "SELECT * FROM Kunde WHERE fornavn LIKE '%".$searchq."%' ";
$stmt = sqlsrv_query( $conn, $sql);
if ($stmt === false){
die( print_r(sqlsrv_errors(), tue) );
}
echo "<table border='1'>";
echo "<tr><th>Kundenr</th><th>Fornavn</th><th>Etternavn</th><th>Tlf</th><th>Epost</th><th>Produktnr</th><th>Adresse</th></tr>";
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_BOTH))
{
$resultArray[] = $row;
echo "<tr>";
echo ''.$row[0].'';
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
I now want to send the variable i press to a new php file. To do this i am using:
session_start();
$_SESSION['kundenr'] = $resultArray[0]['kundenr'];
The problem is that i my $resultArray i must write a number inside [], which define what loop number it are saving ['kundenr'] from. So now i have [0], it is always sending ['kundenr'] from the first loop. This works fine as long I dont have 10 search result to choose from.
So are here anyone that now a easy fix? somthing to store the loop value from my click or something?
Thanks!
I am echoing a MySQL table to HTML table, but some of my database fields are empty. When a database field is empty, how can I specify an alternate text (rather than it just showing blank?
The following works perfect (with blank cells):
<?php
include("../config.php");
$link = mysqli_connect("$db_host" , "$db_user" , "$db_password" , "$db");
mysqli_select_db($link, $db);
$result = mysqli_query($link,"SELECT * FROM the_table");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I think the answer should look something like this (but it doesn't work):
if (empty($row['name'])) {
echo "<td>Not Specified</td>";
} else {
echo "<td>" . $row['name'] . "</td>";
}
Suggestions?
I think this should work:
if ($row['name'] == null) {
echo "<td>Not Specified</td>";
} else {
echo "<td>" . $row['name'] . "</td>";
}
Try:
if(empty($row['name']) || $row['name'] == "")
See http://php.net/manual/en/function.empty.php for the empty documentation. Maybe your result contains spaces? Then use the trim function as commented on your question.
<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset = utf-8" />
<title>Using file functions PHP</title>
</head>
<body>
<h1>Web Development - Lab05</h1>
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$queryResult = "SELECT car_id, make, model, price FROM cars";
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array($queryResult);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
</body>
</html>
This is doing my head in, I cannot figure out why it will not display the actual data apart from the Table header. No matter what I used.
I have tried mysqli_fetch_array, mysqli_fetch_row, mysqli_fetch_assoc but nothing works.
Help and explanation why it was not displaying the data would be much appreciated :)
First: You aren't running a query, you are only putting the query text in a variable. You need to use mysqli_query.
Second: You should add mysqli_fetch_array to the loop.
For example:
while($displayrow = mysqli_fetch_array($queryResult))
{
}
Otherwise you are only getting the first row.
Third: Array keys are case sensitive. There is no $row['ID'], as Jeribo pointed out, it is $row['car_id'] as referenced in your query. $row['Make'] is not the same as $row['make'].
Please Precision to names of field in Query ( car_id,make,...)
while($displayrow= mysql_fetch_assoc($queryResult) )
{
echo "<tr>";
echo "<td>" . $displayrow['car_id'] . "</td>";
echo "<td>" . $displayrow['make'] . "</td>";
echo "<td>" . $displayrow['model'] . "</td>";
echo "<td>" . $displayrow['price'] . "</td>";
echo "</tr>";
}
If you want to query outside you still have to set it in the loop:
$result = $db->query($queryResult)
while($row = $result ->fetch_assoc()){
...
}
a Good Tutorial is shown here: http://codular.com/php-mysqli
$row needs to be initialized so why don't you try:
while($row = mysqli_fetch_array($queryResult))
{
....
}
You have to get the result set first and then try fetching array from result set
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$query = "SELECT car_id, make, model, price FROM cars";
$resultSet=mysqli_query($dbconnect,$query)
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array( $resultSet);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
http://www.w3schools.com/php/func_mysqli_fetch_array.asp