Building a search results page via php - php

I need to build HTML page to show and organize my results. Where do I place the HTML? I tried it and I get errors. Does it go inside the php script? after the php script? before? Confused.
<?php
if (isset($_POST['Submit'])) {
if (!empty($_POST['reg'])) {
$record = $_POST['reg'];
$query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
$result = mysql_num_rows($query);
if ($result != 0) {
$row = mysql_fetch_array($query);
$connect_date = $row['connect_date'];
$reg = $row['reg'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$nickname = $row['nickname'];
$gender = $row['gender'];
$birthday = $row['birthday'];
$home_state = $row['home_state'];
$national = $row['national'];
$location = $row['location'];
} else {
header("Location: search_error1.php");
exit;
}
} else {
header("Location: search_error2.php");
exit;
}
}
?>

PHP is embeddable into HTML, so you can write your html markup right in php file. You just need to place all your php code inside <?php ?>:
<?php
//php code
?>
<html>
<!-- ... -->
</html>
<?php /* php again */ ?>
PHP interpreter simply ignores all text written outside the php tag (<?php ?>) and writes it directly to stdout.
So for you it will be something like this:
<?php
// ...
if ($result != 0) {
while($row = mysql_fetch_array($query)) {
$connect_date = $row['connect_date'];
$reg = $row['reg'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$nickname = $row['nickname'];
$gender = $row['gender'];
$birthday = $row['birthday'];
$home_state = $row['home_state'];
$national = $row['national'];
$location = $row['location'];
?>
First name: <?php echo $first_name ?><br/>
Last name: <?php echo $last_name ?><br/>
...
<?php
}
}
// ...
?>

Related

select all not working in mysql

pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out.
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
?>
<?php
}
?>
<?php
echo $id;
echo $body;
?>
Either print the values in while loop or store them in an array to print them outside the loop.
while($row = mysql_fetch_assoc($query)){
echo $row["id"];
echo $row["body"];
}
condb.php
<?php
$con = mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
index.php
<?php
include 'database/condb.php';
$query = "SELECT * FROM posts";
$res = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($res)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo $body;
}
?>
For storing data, sometimes using array becomes helpful. I usually recommend it:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
$array[$id] = ['username'=>$username, 'body'=>$body, 'date_added'=>$date_added, 'hasttags'=>$hasttags];
}
?>
<?php
foreach($array as $id => $value){
echo $id; //Prints id
echo "<br>";
echo $value['username'].", ".$value['body'].", ".$value['date_added'].", ".$value['hasttags'];
}
?>
Or you can simply do this:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
?>
<?php
echo $id.", ".$username.", ".$body.", ".$date_added.", ".$hasttags;
}
?>
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo "<br>";
echo $body;
?>
<?php
}
?>
As an answer to the question "pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out."
Put echo $id, echo $body be inside the loop.

PHP include(); not including file

I am trying to include a separate snippet of code to fetch data from MySQL tables. My page code:
<?php session_start(); ?>
<html>
<?php include('head.php'); ?>
<body>
<?php include('navigation.php'); ?>
<div id="container">
<?php
$section = "Movies";
print "THIS IS A TEST";
//$_SESSION['sectiontemp'] = $section;
include('section-grabinfo.php');
include('footer.php');
?>
</div>
</body>
</html>
My section-grabinfo.php page:
<?php
print "THIS IS A TEST";
$sql = mysqli_query($conn, "SELECT * FROM article JOIN person ON article.author=person.id WHERE section='".$section."' AND status=1 ORDER BY aid DESC LIMIT 1;");
if ($sql == 'false') {
print "SQL doesn't work";
}
elseif ($sql == 'true') {
print "Works all fine";
}
else {
print "Wrong code.";
}
while ($row = mysqli_fetch_assoc($sql)) {
$title = $row['title'];
$preview = $row['preview'];
$author = $row['name'];
$person_id = $row['author'];
$id = $row['id'];
$username = $row['username'];
include('featured-article.php');
}
//LEAVE SPACE FOR ADS
?>
<div id="secondaryArticleSection">
<?php
$sql2 = mysqli_query($conn, "SELECT * FROM article JOIN person ON article.author=person.id WHERE aid<(SELECT max(aid) FROM article WHERE section='."$section."' AND status=1) AND section='".$section."' AND status=1 ORDER BY aid DESC;");
while ($row = mysqli_fetch_assoc($sql2)) {
$title = $row['title'];
$preview = $row['preview'];
$author = $row['name'];
$person_id = $row['author'];
$id = $row['id'];
$username = $row['username'];
include('secondary-article.php');
}
?>
</div>
Basically my problem is that section-grabinfo.php and footer.php are not being included on the main page. Please keep in mind that this worked completely when I had this on my laptop computer (not the server I'm currently working with). Thank you.
If it is really an including trouble, you could try to include this way
<?php include(__DIR__.'/head.php'); ?>

twitter bootstrap typeahead source from mysql/JSON

So basically, I want my my typeahead to get source from mysql table.
Below is my code:
<script src="../../js/bootstrap-typeahead.js"></script>
<script type="text/javascript">
var patients = [HERE I WANT TO PUT THE SUGGEST_PATIENTS.PHP];
$('#search_bar').typeahead({source: patients})
</script>
suggest_patient.php
<?php
include('../../db.php');
$query = $conn->prepare('SELECT * FROM patients WHERE fname LIKE ?');
$query->execute(array('value%'));
$output_string = '';
for($i=0; $row = $query->fetch(); $i++){
$fname = $row['fname'];
$lname = $row['lname'];
$mname = $row['mname'];
$bday = $row['bday'];
$religion = $row['religion'];
$occupation = $row['occupation'];
$gender = $row['gender'];
$phoneno = $row['phoneno'];
$address = $row['address'];
$type = $row['type'];
}
$output_string = $fname;
echo json_encode($output_string);
?>
But it's not getting the typeahead effect. Can you please help me figure out what's missing in my code? Help is much appreciated.
If you read the documentation it states that the source should be an array or a javascript function.

PHP/MySQL double loop

I am working on a directory where some of the listings have a images associated with them and others do not. I am wondering how I can write a loop within a loop to get my results.
Example, User selects state they want results from, query goes to DB requesting all listings in that state.
<?php
if (isset($_POST['searchButton'])) {
$state = $_POST['state'];
$query = "SELECT * FROM directory LEFT JOIN directory_images ON directory.id = directory_images.user_id WHERE directory.state = '$state' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "<p>Sorry, there are no listings in '$state', check back soon!</p>\n";
}
else
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$has_support_pics = $row['file_name'];
?>
<h4><?php echo $name ?></h4>
<p><?php echo $address ?><br/>
<?php echo $city . ' ' . $state . ', ' . $zip; ?><br/>
</p>
<?php
// check to see if ID has extra images
if (isset($has_support_pics)) {
$query2 = "SELECT file_name FROM directory_images WHERE user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());
echo $query2.'<br/>';
?>
<ul class="support_images">
<?php
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$support_image = $row['file_name'];
echo $support_image.'<br/>';
}
?>
</ul>
</div>
<br/>
</div>
<?php
}
echo "<hr/>";
}
}
?>
Do NOT run queries in loops - use a join.
Here is a tutorial: http://thewebmason.com/tutorial-parent-child-lists/

What is wrong with this code that uses the mysql extension to fetch data from a database in PHP?

I want to fetch data from a mysql table using php. Please, can anyone tell me what is wrong with this code? What is the correct code to fetch data from a mysql database:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$connect=mysql_connect("localhost","root","");
// connect to databsase
mysql_select_db("form1",);
enter code here
// query the database
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
// fetch the result / convert resulte in to array
while ($rows = mysql_fetch_array($query)):
$rows = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment'];
echo "$Name<br>$Address<br>$Email<br>$Subject<br>$Comment<br><br>";
endwhile;
?>
Variables in php are case sensitive. Please replace your while loop with following:
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
Try this
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'database name';
// 2. Create a database connection
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
// 3. Select a database to use
$db_select = mysql_select_db($dbname,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
while ($rows = mysql_fetch_array($query)) {
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
}
?>
Not tested!!
*UPDATED!!
Change the "WHILE" to "while". Because php is case sensitive like c/c++.
Select a database with identifier mysql_select_db("form1",$connect);
Are you getting syntax error? If please put a ; next to $comment = $rows['Comment'].
Also the variables should be case sensitive here
If this is the code you have, then you will get an error because, you are reassigning $row while in the loop, so you would never be able to iterate over the results. Replace
$rows = $rows['Name'];
with
$name = $rows['Name']'
So your code would look like
WHILE ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment'];
Also I am assuming that the column names in the table users are Name, Address, Email etc. and not name,address, email. Remember that every variable name/field nameh is case sensitive.
Your syntax is wrong... The correct coding is:
<?php
mysql_connect("localhost","root","");
mysql_select_db("form1");
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ");
while($rows = mysql_fetch_array($query))
{
$rows = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo $rows.'</br>'.$address.'</br>'.$email.'</br>'.$subject.'</br>'.$comment;
}
?>
<table border="1px">
<tr>
<th>Student Name</th>
<th>Email</th>
<th>password</th>
</tr>
<?php
If(mysql_num_rows($result)>0)
{
while($rows=mysql_fetch_array($result))
{
?>
<?php echo "<tr>";?>
<td><?php echo $rows['userName'];?> </td>
<td><?php echo $rows['email'];?></td>
<td><?php echo $rows['password'];?></td>
<?php echo "</tr>";?>
<?php
}
}
?>
</table>
<?php
}
?>
Try
$query = mysql_query("SELECT * FROM users WHERE name = 'Admin' ")or die(mysql_error());
and check if this throw any error.
Then use while($rows = mysql_fetch_assoc($query)):
And finally display it as echo $name . "<br/>" . $address . "<br/>" . $email . "<br/>" . $subject . "<br/>" . $comment . "<br/><br/>" . ;
Do not user mysql_* as its deprecated.
use this code
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment'];
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;
?>
Code:
while ($rows = mysql_fetch_array($query)):
$name = $rows['Name'];
$address = $rows['Address'];
$email = $rows['Email'];
$subject = $rows['Subject'];
$comment = $rows['Comment']
echo "$name<br>$address<br>$email<br>$subject<br>$comment<br><br>";
endwhile;

Categories