Make a hyperlink from SQL and PHP - php

I have several "providers" with website urls listed in a table. I am wondering how I would link their websites that are listed in the table to make them live urls.
<?php if($Website){
echo "<div class='providerData1'>Website: </div> <div class='providerData1 providerData2'>" . $Website . "</div><br />"; }
?>
Any ideas? Thank you. :)

Base yourself on the following: (details can be found in comments)
<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";
$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
die('Connection failed [' . $db->connect_error . ']');
}
if($db->connect_errno > 0){
die('Unable to connect [' . $db->connect_errno . ']');
}
// Use
// $sql = mysqli_query($db, "select * from tablename ");
// Or select particular columns
$sql = mysqli_query($db, "select link,name from tablename LIMIT 1");
// You can remove LIMIT 1 if you want to show them all
echo "Pages (found):";
echo "<hr>";
while ($row = mysqli_fetch_array($sql)){
$name= $row['name'];
$Website = $row['link'];
echo $name;
echo "<br>";
echo "<div class='providerData1'>Website: </div> <div class='providerData1 providerData2'><a href='$Website'>" . $name . "</a></div><br />";
}
echo "<br>";
?>
In place of the entire while loop, you can replace it with:
Sidenote (having a URL for the link column)
echo '<table><tr><th>Name</th><th>Page</th><th>Link</th></tr>';
while ($row = $result->fetch_assoc()){
echo '<tr><td>'.$row["name"].'</td>';
echo '<td>'.$row["link"].'</td>';
echo '<td>Link</td></tr>';
}
echo '</table>';
and display your links neatly in an HTML table.

Related

php stmt similar products display

I am creating PHP stmt similar products display script this script working but not showing similar products I want to display similar products title
Here is my code
<?php
$id=$row['id'];
if($stmt = $con->prepare("SELECT title
FROM products order by rand() limit 3
")){
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//results
}}
?>
view.php
<?php
if(isset($_GET['id'])) {
include("config.php");
$id = $_GET['id'];
$sql = "select * from products where id = '$id'";
$result = $con->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
//results
}}}
?>
Ensure that Database credentials were okay.
Try the code below and let me know
1) Query using rand() method( This is what you want)
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'your password goes here';
$dbname = 'your database goes here';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = 'SELECT title FROM products order by rand() limit 3';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Title: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
2.) Query Using where Clause in this case an id(1) to show product title where id of 1 matches it in the database
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'your password goes here';
$dbname = 'your database goes here';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = "SELECT id,title FROM products where id='1'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Title: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Updated Section
First create table below and insert into it
create table products(id int(11) primary key auto_increment,title varchar(30),description varchar(30),image varchar(30),cat_id int(11));
You will insert at least one record
insert into products (id,title,description,image,cat_id) values(1,'product title','product details','product.png',100);
For testing query the products table to get details where id is 1
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'anglejs';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = "SELECT id,title,description,image,cat_id FROM products where id='1'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Id: " . $row["id"]. "<br>";
echo "Title: " . $row["title"]. "<br>";
echo "description: " . $row["description"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Now since the Id is coming from url
For testing purpose:
You can save the code as view.php like you did
Open your browser and Enter something like
http://localhost/-----yourfolder diretory goes here-----/view.php?id=1
You can see Id of 1 coming from the Url as appended to view.php files. since we have inserted record which has id of 1, the code below will display the
records only for rows where id is = 1 and so on....
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'anglejs';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$id = $_GET['id'];
//check if id is empty
if($id==''){
echo "Id is empty";
exit;
}
$sql = "SELECT id,title,description,image,cat_id FROM products where id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Id: " . $row["id"]. "<br>";
echo "Title: " . $row["title"]. "<br>";
echo "description: " . $row["description"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

add alt for images in a while statement

Can anyone help please?
This is my code.I want to add an alt option for images but everything I try is throwing errors.
I have tried studying the php handbook and have tried copying code from other questions but so far no luck.
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2";
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src='$row['ming']'>';
}
} else {
echo "0 results";
}
$conn->close();
?>
You have a string concatenation problem :
echo '<img src="' . $row['ming'] . '">';
Will output : <img src="myimage.jpg">.
To add a alt attribute, you could do :
echo '<img src="' . $row['ming'] . '" alt="' . $row['name'] . '">';
Will output something like
<img src="myimage.jpg" alt="image name">
change your code like this:
<?php
$servername = "localhost";
$username = "logosewe_5";
$password = "password";
$dbname = "logosewe_5";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mk2"
;
$sql = "SELECT * FROM mk2 WHERE brand='2786'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<img src="'.$row['ming'].'" alt="'.$altText.'">'; //change here
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem was that you did not have an alt attribute in your echo statement and the second was your concatenation was a little off with misplaced quotes.
while($row = $result->fetch_assoc()) {
echo '<img src=' . $row['ming'] . 'alt="Your alt message">';
}

use a PHP variable as the source in an iframe to play a youtube video

I have spent the better part of three days on this problem. I've tried several solutions that I've found on this site but with little success. What I'm trying to do is use a PHP variable to play a youtube video in an iframe. I'm also trying to run two MySQL queries with the same input. Here is my code as it sits right now. At this moment when it runs I get the table that I'm wanting, though I still need to format it. But the iframe isn't even showing up. A previous solution I tried would pull up the iframe but inside would be an error where I was basically passing the sql query to the iframe.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$trainingid = $_POST["trainingid"];
$sql = "SELECT * FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Training ID</th><th>Title</th><th>Training URL</th><th>Training Quiz URL</th></tr>";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["trainingid"] . "</td><td>" . $row["trainingtitle"] . "</td><td>" . $row["trainingurl"] . "</td><td>" . $row["trainingquizurl"] . "</td></tr>";
}
echo "</table>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$videosql = "SELECT trainingurl FROM purpletrainer.trainingcontent WHERE trainingid = '$trainingid';";
$videoresult = $conn->query($videosql);
if ($videoresult->num_rows > 0) {
while ($videourl = $videoresult->fetch_assoc()) {
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?php echo htmlspecialchars($videourl); ?>' frameborder="0" allowfullscreen></iframe>
</div>
It's common practice not to use the variable $videourl outside of the while loop. (Put your iframe within the while loop).
sidenote: fetch_assoc() will create an array. It should be $videourl['trainingurl'];
Here's an example:
<?php
if ($videoresult->num_rows > 0)
{
while($videourl = $videoresult->fetch_assoc()){
?>
<div class="col-md-6">
<iframe width="420" height="315" src= '<?= $videourl['trainingurl']; ?>' frameborder="0" allowfullscreen></iframe>
</div>
<?php
}
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
edit for clarity and future readers
Note that the <?= $variable ?> opening tag has only been properly supported since php 5.4+. When not using this version, maintain the usage of <?php echo $variable; ?>

how to convert the text outputted from a database to links?

Iam class 11th..recently I started learning php and mysqliI have been facing a problem. I am trying to create a database which has a small list of movies.when the page loads, it displays the list of those movies from database..but the problem is, it displays them as a simple text..i Want them to be like links so that whenever it is clicked it displays the info about that particular movie..but i dont want to write anchor tag links for each movie..Is there any other way?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title FROM movies";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
According to your code.
if ($result->num_rows > 0) {
$htmlLink = '';
while($row = $result->fetch_assoc()) {
$id = $row["id"];
$title = $row["title"];
$htmlLink .= "<a href='movie.php?{$id}'>id: {$id} - Name: {$title}</a><br>";
echo $htmlLink;
}
} else {
echo "0 results";
}
Then on your movie.php page use $_GET[] to get the query string data
Replace:
echo "id: " . $row["id"]. " - Name: " . $row["title"]. "<br>";
With:
$id = $row["id"];
$title = $row["title"];
echo "<a href='/movie/{$id}'>id: {$id} - Name: {$title}</a><br>";

SELECT * FROM Table Where ID

I am trying to retrieve information from my database depending on the ID a user types into my URL.
For example: If USER A went to www.exampleurl.com/index.php?id=1 it would echo out the user's information which has an ID of 1. Same thing if the id was 2, 3, etc. Users are entering their information via a form in a different file called submit.php.
Here is my code to retrieve data depending on ID :
<?php
$id = $_GET['id'];
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
echo "Hello, " . $result['name'];
?>
Any ideas on if my SELECT request is wrong?
EDIT
Here is my code for showing the data altogether in a table. This works fine.
<?php
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "!";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query, $con);
echo "<table border=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Age </th>
</tr>";
while($record = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['name'] . "</td>";
echo "<td>" . $record['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
→ Try This:
You should consider using PHP PDO as it is safer and a more object oriented approach:
$usertable = "";
$database = new PDO( 'mysql:host=localhost;dbname=DB_NAME', 'DB_USER_NAME', 'DB_USER_PASS' );
$statement = $database->prepare('SELECT * FROM $usertable');
$statement->execute();
$count = $statement->rowCount();
if( $count > 0 ) {
$R = $statement->fetchAll( PDO::FETCH_ASSOC );
for( $x = 0; $x < count($R); $x++ ) {
echo "<tr>";
echo "<td>" . $R[ $x ]['id'] . "</td>";
echo "<td>" . $R[ $x ]['name'] . "</td>";
echo "<td>" . $R[ $x ]['age'] . "</td>";
echo "</tr>";
}
}
else { echo "Error!"; }
you need to use mysql_fetch_assoc function for retrieve the results.
$result = mysql_fetch_assoc(mysql_query($query, $con));
echo "Hello, " . $result['name'];
You should be error checking your mysql_querys:
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
if(!result)
echo mysql_error();
You should also retrieve the results:
$array = mysql_fetch_assoc($result);
I'll consider some secure features like
Check if $_GET['id'] is set and if is int
Apply Mysql escape with mysql_escape_string() function

Categories