PHP array error in displaying a table - php

I am trying to display all the data from a mysql table into a html table and the PHP is getting the data fine but the table isn't displaying it properly this is my code:
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Phone Number</td>
<td>Email</td>
<td>Time</td>
<td>Number Of People</td>
<td>Time Placed</td>
</tr>
<?php
$host = "localhost";
$database = "reservation";
$user = "root";
$pass = "root";
//connection to the database
mysql_connect($host, $user, $pass)
or die ('cannot connect to the database: ' . mysql_error());
//select the database
mysql_select_db($database)
or die ('cannot select database: ' . mysql_error());
$sql = "SELECT * FROM reservation ORDER BY timeplaced";
$result = mysql_query($sql);
while($data = mysql_fetch_row($result)){
echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr><td>$data[3]</td></tr><td>$data[4]</td></tr><td>$data[5]</td></tr><td>$data[6]</td></tr>");
}
?>
</table>
Please help.

$sql = "SELECT * FROM reservation ORDER BY timeplaced";
$result = mysql_query($sql);
while($data = mysql_fetch_row($result)){
echo("<tr>
<td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
<td>$data[4]</td>
<td>$data[5]</td>
<td>$data[6]</td>
</tr>");
}
when you use TDs in TR, they all must be same numbers, or use colspan, try the above echo statement, it will display your table properly.

When you are echoing array items it should be
echo "<tr><td>" . $data[0] . "</td></tr>";

echo "<tr><td>{$data[0]}</td><td>{$data[1]}</td><td>{$data[2]}</td></tr><td>{$data[3]}</td></tr><td>{$data[4]}</td></tr><td>{$data[5]}</td></tr><td>{$data[6]}</td></tr>";
Use braces.

Related

PHP won't display result of echo

I have a problem, when I try with this code without hosting I'm getting result but when I try it on host php won't display result, I know Select * is not good nowadays but if you can please help.
This is screen how it looks like:
Here is code:
$conn = mysql_connect("localhost", "username", "password");
if (!$conn) {
die("no connection".mysql_error());
}
$db = mysql_select_db("db_name");
mysql_set_charset('utf8', $conn);
I'm using this code on HTML:
<thead>
<tr>
<th>id</th>
<th>სახელი</th>
<th>ტექსტი</th>
<th>პატარა ტექსტი</th>
<th>სურათი</th>
<th>წაშლა</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM news";
$result = mysql_query($sql);
while ($data = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$data['id']."</td>";
echo "<td>".$data['name']."</td>";
echo "<td>".substr($data['article'], 155,300)."</td>";
echo "<td>".substr($data['text'], 155)."</td>";
echo "<td>".$data['img']."</td>";
echo "<td>წაშლა</td>";
echo "</tr>";}
?>
</tbody>

How i can retrieve data of a individual by s_no

i have 2 html table one is table.php and anther one is viewdata.php. In frist table there arenumber of rows and data is extracting from mysql database. If i will click on serial no 1 of my html table data then the detail of serial no 1 must show in another table and i tried so. But i dont understand how to do it.
table.php
this is the html table
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass)
or die ('Error connecting to mysql');
$dbname = 'form_db';
mysql_select_db($dbname);
$query = "SELECT * FROM form";
$result = mysql_query($query)
or die(mysql_error());
print "
<table id=\"AutoNumber2\" border=\"1\">
<tr>
<th>S.no</th>
<th>Title of thesis:</th>
<th>View detail:</th>
</tr>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
print "<tr>";
print "<td>" . $row['s_no'] . "</td>";
print "<td>" . $row['title of thesis'] . "</td>";
print "</tr>";
}
print "</table>";
?>
dataview.php another table
<?php
$query = "SELECT * FROM form";
$result3 = mysql_query($query)
or die(mysql_error());
$result3 = mysql_query("SELECT * FROM form where s_no='11'");
while($row3 = mysql_fetch_array($result3, MYSQL_ASSOC)){
$s_no=$row3['s_no'];
$obs_time=$row3['obs_time'];
$title=$row3['title'];
$type=$row3['type'];
$thesis=$row3['thesis'];
$year=$row3['year'];
$proposer=$row3['proposer'];
$institute=$row3['institute'];
$email=$row3['email'];
$present=$row3['present'];
$date=$row3['date'];
}
?>
Here I have mannualy select the s_no'11'. I dont know how i can pass the s_no automatically simply clicking on the row (view detail) which i want to show in another table with its detail. thank you so much./!!
send the variable via the URL using $_GET
dbconnect.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'form_db';
mysql_select_db($dbname);?>
table.php
include('dbconnect.php');
$query = "SELECT * FROM form";
$result = mysql_query($query) or die(mysql_error());
print "
<table id=\"AutoNumber2\" border=\"1\">
<tr>
<th>S.no</th>
<th>Title of thesis:</th>
<th>View detail:</th>
</tr>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
print "<tr>";
print "<td>". $row['s_no'] . "</td>";
print "<td>" . $row['title of thesis'] . "</td>";
print "</tr>";
}
print "</table>";?>
dataview.php
<?php
include('dbconnect.php');
$sn= $_GET['s_no'];
$sql = "SELECT * FROM form where s_no=". $sn;
$result3 = mysql_query($sql);
while($row3 = mysql_fetch_array($result3, MYSQL_ASSOC)){
$s_no=$row3['s_no'];
$obs_time=$row3['obs_time'];
$title=$row3['title'];
$type=$row3['type'];
$thesis=$row3['thesis'];
$year=$row3['year'];
$proposer=$row3['proposer'];
$institute=$row3['institute'];
$email=$row3['email'];
$present=$row3['present'];
$date=$row3['date'];
}
?>

can't get the data from database PHP

i created a function to get data from the database but it's not working i don't know what's wrong
here is the code :
<?php
function Connect_Show($server_name,$server_user,$server_pass,$db,$db_table){
$connect = mysql_connect($server_name,$server_user,$server_pass) or die ('error , can not connect to database ') ;
$select = mysql_Select_db($db) or die (' error , can not select the database ');
$query = mysql_query("SELECT * FROM $db_table order by id ")
while ($rows = mysql_fetch_assoc($query)) {
echo $rows['id'];
echo $rows['name'];
echo $rows['emails'];
echo $rows['password'];
}}
echo Connect_Show("localhost","root","root","learn","users");
?>
Instead of
echo Connect_Show("localhost","root","root","learn","users");
Just use this:
Connect_Show("localhost","root","root","learn","users");
Connect_Show is a function, with echo commands in it, its not returning anything.

Populating the values from mysql in a dropdown list

the code works but the values are not populated from my database table. it shows an empty dropdown. Please give me some suggesstions friends.
Thanks in Advance...
enter code here
<?php
$mysql_hostname = "host";
$mysql_user = "uname";
$mysql_password = "pass";
$mysql_database = "dbname";
$prefix = "";
$db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Could not connect database");
//echo "hurray!!!connection successful";
//$query = ("SELECT teacherid,fname from tablename") ;
?>
<table>
<tr>
<td>teacher name</td>
<td><select name="teacherr_name">
<?php
$query = 'SELECT teacherid,fname FROM teacher_master';
$result = mysql_query($query, $db) or die(mysql_error($db));
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['teacherid'] . '"> ' . $row['fname'] . '</option>';
}
?>
</select></td>
</tr>
</table>
</html>
Have you selected the database to work with??? You can do this by
mysql_select_db($mysql_database);
You also need to select your database
$db_selected = mysqli_select_db($mysql_database, $db);
Add this line after you made the connection. Also convert all your mysql statements to mysqli. For more details see this
You forgot to select the database, after you've connected to the database server:
mysql_select_db($mysql_database, $db);
needless to say that this is the old way of doing it and it will be depricated...
maybe...
while ($row = mysql_fetch_array($result))
{
echo '<option value="' . $row['teacherid'] . '"> ' . $row['fname'] . '</option>';
}
Even if mysql command are deprecated you can try using the mysql_fetch_array to obtain a "row" and access it array style. You need first to check what you have at db, that's for sure.
select database from where you want to fetch data
mysql_select_db('database name','connection variable');

How can i get the index value from the MySQL query with php?

Hi i am fetching the data from mysql data base i want get the index of the columns in the table
<?php
//connects to database
$con = mysql_connect("locahost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("appulentoweb", $con);
//retrieve data from database
$result = mysql_query("SELECT * FROM questions");
?>
i want get the index of the columns in the table then i will display that data in the screen.
in the table i have 2 columns i'e qno and titile i want display that titles in the output page
Thanks in Advance...
please try this..
<?php
//connects to database
$con = mysql_connect("locahost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("appulentoweb", $con);
//retrieve data from database
$result = mysql_query("SELECT * FROM questions"); ?>
<table>
<tr>
<th> Question no</th>
<th> Question </th>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['qno'];?></td>
<td><?php echo $row['question'];?></td></tr>
<?php } ?>
</table>
i hope that it is help you....
if you have nay problem let me know...
You can use function mysql_fetch_array which return indexed and associative array.
try this;
<?php
//connects to database
$con = mysql_connect("locahost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("appulentoweb", $con);
//retrieve data from database
$result = mysql_query("SELECT * FROM questions");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
echo $rows['qno'];
echo $rows['title'];
OR
$rows = mysql_fetch_array($result, MYSQL_NUM);
echo $rows[0];
echo $rows[1];
?>

Categories