I'm trying to figure out what's wrong with my mysqli code below. The data is definitely in the table. I have 2 questions:
Why isn't this giving me any results?
Could I have the echo <table></table> section in a separate php file, if yes, how would I call $name,$partner,$cell etc.?
<?php
$mysqli = new mysqli($host, $uname, $pword, $db);
if ($mysqli->connect_errno)
{
echo "Connection failed: %s\n", $mysqli->connect_error;
exit();
}
$query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC");
$result = $mysqli->query($query);
while ($row = $result->fetch_array()) {
$space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';
$name = $row['firstname'].$space.$row['lastname'];
$partner = $row['spousefirst'];
$cell = (!empty($row['phonecell'])) ? " {$row['phonecell']} (cell)" : '';
$email = (!empty($row['email'])) ? " {$row['email']} (email)" : '';
$ID = $row['ID'];
echo'<table>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="<? echo $ID; ?>"></td>
<td><strong><? echo $name; ?></strong></td>
<td><? echo $partner; ?></td>
<td><? echo $phonecell; ?></td>
<td><? echo $email; ?></td>
<td><? echo date("M jS, g:i A", strtotime($date)); ?></td>
<td><? echo $contacttype; ?></td>
<td><? echo $agentassigned; ?></td>
<td><? echo $leadstatus; ?></td>
<td>View + </td>
<td>View + </td>
<td>D</td>
</tr>
</tbody>
</table>';
}
?>
EDIT:
It's showing the <table> but just not giving me values for $name, $email...ect.
The final echo does not look well constructed. I would prefer to use a concatenated string to output the variables, just like this( I put only a few lines as example):
echo'<table>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="'. $ID .'"></td>
<td><strong>'. $name.' </strong></td>
The query looks solid as does the rest of your code. Here are a few troubleshooting steps to try out:
echo out $query and run it directly against your database. Do you get results? If you don't get any results from a direct query then that means the query is returning 0 rows and your tables will end up empty as well.
Do a var_dump($result) to verify there is information being stored in there. You could also add change $result = $mysqli->query($query); to $result = $mysqli->query($query) or die $mysqli->error;
The results from these 2 steps should give you a good idea on where things are going wrong. In general you want to stop through each part of the query process and look at every variable involved to ensure that it has the data you expect stored inside of it.
To answer your 2nd question: yes you can. If you put the code for the table in a separate file and then include()it, the variables will still be available to the included code.
See the PHP manual on include()
Related
i have a table with id, name, address, sector, financiar, link
on the link i when i press it i want to show me 2 tables from the id of row selected, ex: id 1.
http://postimg.org/image/khelg1m0z/
and the result: http://s28.postimg.org/srvcwj065/Capture2.jpg
now it's a static page with search clause where by id 1, but i need an automatically link show by id on each row.
<?php
include "connect.php";
$sql = "select * from studenti where id='1'";
$query = mysql_query($sql) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Nume</td>
<td>Localitate</td>
<td>Judet</td>
<td>Sector Financiar</td>
<td>Link</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
<?php } ?>
</table>
<?php
include "connect.php";
$sql1 = "select * from certificari where id='1' ";
$query = mysql_query($sql1) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Denumire certificare</td>
<td>Serie si numar certificare</td>
<td>Data certificarii</td>
<td>Valabilitate certificare</td>
<td>Sector Financiar</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['serie_numar']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['valabilitate']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<?php } ?>
</table>
You should not use the mysql_ functions since they are depricated and are very vulnerable to SQL injection attacks. Instead, I will use MySQLi in this answer, but you could also use PDO if you want to.
To display records for different ID's based on what the user selects you can pass an ID in the page URL. When you link to the page you add the desired ID to the address like this:
Link
The value of the variable id will now be available in page.php as $_GET['id'].
The actual PHP in your page would then look something like the code below. I have left out some of your HTML for brevity, but you can just add it int.
//Connect to the DB. Might want to put this in your connect.php and include it.
$db = new mysqli('localhost', 'user', 'pass', 'db');
//Prepare the statement. The ? will be your ID.
$statment = $db->prepare("SELECT * FROM studenti WHERE id = ?");
//Bind the parameters, so that the first (and only) question mark is turned into your ID.
//The 'i' means integer, if you store the id as a string your should use 's' instead.
$statement->bind_param('i', $_GET['id']);
//Execute the query.
$statement->execute();
//Get the results.
$result = $statement->get_result();
//Iterate over it to create the output.
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
</tr>
<?
}
//Then do the same thing for the table certificari.
//Note that you only need to connect to the DB once.
This beginners guide to MySQLi is very helpful if you need some more guidance.
Trying to list the data from mysql to a html table using php in main html file. I've been through all of the other questions on here and I'm sure I have mostly the same methods and code as them.
For some reason (which I suspect has something to do with mysql and not the code) the only result is a blank table with one row and five columns. Each time I try to implement the other codes they just seem to print text onto the site.
I'm very confused as I think I've done the right thing. I've even been able to list the data from mysql through php echo so I know it's there and that I can access it. Really would appreciate some help on this. Thank you.
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tbody>
</table>
You say this code is in your mail html file? And it is printing out onto the screen? Try changing your file to .php not .html! Php code won't run in a .html file, and will likely output your code directly onto the page.
Mysqli_query expect connection link as first parameter.
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
Just a quick not so related improvement. You can avoid inserting most of the php opening and closing clauses:
...
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['title']."/td>";
echo "<td>".$row['url']"</td>";
...
}
?>
Use <tr> because table must have atleast one row(<tr>) and one column(<td>)
<table border="1">
<tbody>
<tr>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tr>
</tbody>
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
if (!$results) {
mysqli_error($results);
die();
}
while ($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['url'] ?></td>
<td><?php echo $row['details'] ?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['submission_date'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
I'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
I am receiving a "Fatal error" once the function fetch_array(mysqli_both) is invoked. The file below on the while instance is exactly where the error is coming from.
<?php
include 'connect.php';
$id = $_SESSION['user_id'];
$res = mysqli_query($mysqli, "SELECT a.name,j.link,j.points, a.submit_time from journal j, article a where a.journal_id = j.id and a.professor_id = $id");
?>
<table>
<tr>
<th>Article</th>
<th>Journal</th>
<th>Points</th>
<th>Time</th>
</tr>
<?php
while ($row = $res->fetch_array(MYSQLI_BOTH)) {
?>
<tr>
<td><?php echo $row[0] ?></td>
<td><?php echo $row[1] ?></td>
<td><?php echo $row[2] ?></td>
<td><?php $date = date_create($row[3]);
echo date_format($date, 'Y-m-d H:i:s'); ?></td>
<tr>
<?php
}
?>
</table>
When i try it on my wampserver i don't have this problem as i activated couple of PHP extensions. Online, i don't think it's easy to activate such extensions. Do you think this error is related to the fact that i didn't invoked the $query as a variable with $mysqli in line 4?
First of all you should use require_once('connect.php'); because it is a needed file for this code.
Also you have to check your $mysqli variable if it's a correct connection to your database.
For more help you might have to post the full Error-Message
you should check result before fetch:
if ($res) {
while ($row = $res->fetch_array(MYSQLI_BOTH)) {
...
...
}
} else {
echo printf("Errormessage: %s\n", mysqli_error($mysqli));
}
I have a file /admin/php.php which has the following:
<?php
$ID=$_GET['ID'];
require("../admin/config.php");
$sql = "SELECT * FROM contacts WHERE contacttype IN ('New','Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC";
$space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';
$name = $row['firstname'].$space.$row['lastname'];
$partner = $row['spousefirst'];
$cell = (!empty($row['phonecell'])) ? " {$row['phonecell']} (cell)" : '';
$email = (!empty($row['email'])) ? " {$row['email']} (email)" : '';
mysql_query($sql) or die ("Error: ".mysql_error());
?>
On another page /admin/index.php I have:
<?php require("php.php"); ?>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
<td><strong><?php echo $name; ?></strong></td>
<td><?php echo $partner; ?></td>
<td><?php echo $row['phonecell']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo date("M jS, g:i A", strtotime($row['date'])); ?></td>
<td><?php echo $row['contacttype']; ?></td>
<td><?php echo $row['agentassigned']; ?></td>
<td><?php echo $row['leadstatus']; ?></td>
<td>View + </td>
<td>View + </td>
<td>D</td>
</tr>
</tbody>
<?php
}
mysql_close();
?>
</table>
When I run this, I get a Parse error: syntax error, unexpected '}' in /admin/index.php. I've tried removing the '}' after <?php in /admin/index.php and I get an error for unexpected $end in /admin/php.php. Really confused why this isn't working. Thanks for your help!
First of all, } should not be there. There is no opening { in this file, so there should not be a closing one.
Next up, unexpected $end - that error is in another file, so that's another problem.
The syntax of php.php looks valid, but:
your mysql_query call should return a result, and i don't see you assigning it to anything.
check that closing ?> isn't followed/preceeded by non-printable characters we do not see here, but that can make parser choke - basically, remove that line, recreate it and save the file.
<?php
}
mysql_close($sql);
?>
You have closing bracket but I can't see opening.
the "}" will cause an error from the code that is posted in your question. I'm not sure if this is all the code though.
also another question would be are you connection to a database at all? cause there is nothing in your code here that shows that either.
and you are not doing anything after the mysql_query($sql) is ran.