How do display SQL search results in separate places - php

I am using this code to search my sql database, it works but if there are more then one result it will just display them next to each over
Example:
echo "".$row['nick'].""; //would be NAME1NAME2NAME3NAME4
i would like to display
NAME1
MORE INFO
NAME2
MORE INFO
NAME3
MORE INFO
.
<?php
$conn = mysql_connect ("*****", "battlefield", "*****") or die ('I
cannot connect to the database because: ' . mysql_error());
$selected = mysql_select_db ("battlefield")
or die ("Could not select database");
// PHP Search Script
$sql = "SELECT * FROM loadout WHERE nick LIKE '%".$_POST['find']."%'";
$result = mysql_query($sql,$conn)or die (mysql_error());
if (mysql_num_rows($result)==0){
echo "Theres no one here called that!";
}else{
while ($row = mysql_fetch_array($result)){
echo "".$row['nick']."";
}
}
mysql_close();
?>
If you need the html or more info please ask xD
Thanks in advance!

Just add <br> :) !
while ($row = mysql_fetch_array($result)){
echo "".$row['nick']." <a href='link'>MORE INFO</a><br>";
}
Or if you want to display it somewhere else in the page, add all results to ann array and then iterate through them wherever you want:
Store them:
while ($row = mysql_fetch_array($result)){
$results[] = $row['nick']." <a href='link'>MORE INFO</a><br>";
}
Display them:
foreach ($results as $result) {
echo $result;
}

Related

Search mysql database and create links from result in php and html

I am a beginner to PHP. I have a database set up with songs in it. At the moment there are only 2 songs and one artist. I am trying to query the database by artist. The page seems to work but is only returning one song instead of two.
I am calling it like this :
search by artist
What is the correct way to do this?
<?php
// get artist id from page call
$artist = $_GET['artist'];
// search by artist
$exists = $mysqli->query("SELECT id FROM songs WHERE artist='$artist'") or die($mysqli->error);
// get numeric array out of result
$Songs = mysqli_fetch_array($exists, MYSQLI_NUM);
foreach($Songs as $key){
echo "<a href='http://www.waylostreams.com/login-system/playSong.php?id=$key&user=$user_id'>Listen</a>";
print "<br>";
}
?>
Thanks in advance for any help!
Sean
This is an example. You can use this code to select all elements in your table(w3schools) nere the link where explain step by step:
https://www.w3schools.com/php/php_mysql_select.asp
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = newmysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Use PHP to generate from an existing database for each row a new specific HTML that i already made

First I'm hitting on a wall here and I really could use your help. I coded the database so I have it all up and working plus all the data inside. I worked the HTML and the CSS media print query and I have it how I want it to look exactly. All I have to do now is:
for every row of the mysql select table I have to fill every specific input form
of the html page I made and print it
Can someone give me a hint of how I can do that?
Assuming you want to connect to your database and simply fetch the id you can do the following.
Ensure you change my_host, my_user, my-password, my_databse,my_tablewith your configuration settings. Then if you want to fetch anything else thanid` just change it to the column name you are looking for.
Be aware we are using PHP here.
// Open Connection
$con = #mysqli_connect('my_host', 'my_user', 'my-password', 'my_databse');
if (!$con) {
echo "Error: " . mysqli_connect_error();
exit();
}
// Some Query
$sql = 'SELECT * FROM my_table';
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($query))
{
echo $row['id'];
}
// Close connection
mysqli_close ($con);
Check this link to get a in-depth explanation.
You can do this with two pages. One page gives you the overview and the other page gives you a print preview of your invoice.
The overview:
// DB select stuff here
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>\n";
echo " <td>".htmlspecialchars($row['what'])."</td>\n";
echo " <td>".htmlspecialchars($row['ever'])."</td>\n";
echo " <td>Detail</td>\n";
echo "</tr>\n";
}
The detail page:
$sql = 'SELECT your, columns FROM tab WHERE id = ?';
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
echo "There is no data for the given Id\n";
return;
}
echo "What: ".htmlspecialchars($row['what'])."<br />\n";
echo "Ever: ".htmlspecialchars($row['ever'])."<br />\n";

Select data from MySQL DB based on the URL example (www.example.co.uk/essex.php)

I am very new to php and I am looking to create dynamic pages.
Basically If a person lands on www.example.co.uk/essex.php I would like to be able to add something like <h1>Company In <?php echo $row['County']; ?> so that it would show
Company In Essex when Live and save time if I need to duplicate the page for another county or town.
So far I have setup a database in phpmyadmin
Picture of Table
My code so far to grab the table is;
$dbconnect = mysqli_connect("HOST", "USER", "PASSWORD", "DB");
if(mysqli_connect_errno()) {
echo "Connection Failed:".mysqli_connect_error();
exit;
}
$count_sql="SELECT * FROM areas";
$count_query=mysqli_query($dbconnect, $count_sql);
$count_rs=mysqli_fetch_assoc($count_query);
and then on www.example.co.uk/essex.php
<?php
do {
echo $count_rs['County'];
} while ($count_rs=mysqli_fetch_assoc($count_query))
?>
But where I have gone wrong is it pulls all the data from the table for County where I only wish it to pull the county of Essex.
Please forgive me for explaining this badly.
UPDATE -
$row = url2content();
extract($row);
function url2content($url = NULL){
if(is_null($url)){
if(isset($_SERVER["REQUEST_URI"]) && $_SERVER["REQUEST_URI"]){
$url = $_SERVER["REQUEST_URI"];
}elseif(isset($_SERVER["PATH_INFO"]) && $_SERVER["PATH_INFO"]){
$url = $_SERVER["PATH_INFO"];
}
if (substr($url,0,1) == '/'){
$url = substr($url,1);
}
}
if (DEBUG_MODE){
echo ('URL requested: "'.$url.'"<br>');
}
function get_towns($county){
$query = sprintf("select townName from " . AREAS_TABLE . " where countyName = '%s' order by rand() LIMIT 0,24" ,mysql_real_escape_string($county));
$res = mysql_query($query);
if(!$res){
printf("Unable to query ".AREAS_TABLE." table: %s \n", mysql_error());
}
$html="";
while($row = mysql_fetch_assoc($res)){
$html.="<li>".stripslashes($row["townName"])."</li>\n";
}
return $html;
}}
You have to use WHERE clause in your query in order to select a specific country from your db
SELECT * from table WHERE felid_country=$count_rs['County'];
You can use strpos() to match the County (eg.essex) with url $_SERVER['REQUEST_URI'] (eg.www.example.co.uk/essex.php) in your case.
Just like this
<?php
do {
if(strpos($_SERVER['REQUEST_URI'],$count_rs['County'])!=false)
echo $count_rs['County'];
} while ($count_rs=mysqli_fetch_assoc($count_query))

Printing Query Issue - MySQL, PHP, HTML -

I had a very random problem in an IIS class, it has stumped my tutor so here I am and I'll try my best to explain it well!
I'm running Xampp with Apache and MySQL, I run the query I want and get the expected output to a table, but I have a problem with the outputting of pictures. I have the right file type, extention and path selected, because I can get pictures to show up, but as long as at least one picture in the query result has the extension removed, and this picture will not load.
Database
Website
If I have each query result with the correct name and extension, which is the same as when it shows up, none of them show up at all!
PHP:
<?php
// set server access variables
include 'db2.inc';
// open connection
$connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!");
// select database
mysql_select_db($databaseName) or die ("Unable to select database!");
// create query
//$query = "SELECT * FROM products";
$query = "SELECT * FROM products WHERE CategoryName = 'Surfboards'";
//Check initial letter
//if (!$initialLetter=="")
//{
// $query = $query." Where country like '$initialLetter%' ";
//}
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<table cellpadding=20 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['ProductID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Description']."</td>";
echo "<td>".$row['Brand']."</td>";
echo "<td>".$row['Model']."</td>";
echo "<td>".$row['BoardLength']."</td>";
echo "<td>".$row['BoardType']."</td>";
echo "<td>".$row['Colour']."</td>";
echo "<td>"."<img src=images/".$row['Image']."> </td>";
echo "<td>".$row['UnitPrice']."</td>";
echo "<td>".$row['CategoryName']."</td>";
echo "</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
Any help or direction would be greatly appreciated!
Thanks
Will
On this line:
echo "<td>"."<img src=images/".$row['Image']."> </td>";
Your image src is not enclosed in quotes. If this is a direct copy/paste of your code then that is definitely a problem, but may not be the only one.
As other people have said, look at pdo, or mysqli. The mysql_ functions you are using are deprecated for multiple reasons.

Cannot echo out the right result. PHP/MySQL

I want to make a code that check if a value in an exact column is equal to an 'neodobren', and if there is such value in the column to echo out the button 'Add Member' and Yes for each value. I've tried to do the following and it is not echoing the Yes:
I have the following MySQL table content:
UID Name Phone Email SchoolGymnasium City Password Status
1 neodobren
2 neodobren
and I have the following PHP code inside the HTML index page:
$con = mysql_connect("localhost","****","****");
mysql_query("SET NAMES UTF8");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("***", $con);
include("../../sql.php");
session_start();
$zaqvki = mysql_query("SELECT * FROM Directors WHERE Status='neodobren' LIMIT 1");
if(mysql_num_rows($zaqvki) > 0) {
echo '<div align="right">Add Member</div><br>';
// display data in table
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array($zaqvki)) { //$zaqvki was $result, before a guy comment me this...
echo "Yes";
} }
else {
echo "No";
}
It is showing only the button add member, and not showing the Yes, which must be echo-ed.
Use mysql_fetch_assoc to get an associative array and make a comparison on each row of a certain column:
while($row = mysql_fetch_assoc($result))
{
if($row['Status'] === 'neodobren')
echo "Yes";
else
echo "No";
}

Categories