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

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>";

Related

how to php echo individual column values of a result row from mysql query?

The below code works perfectly and gives me the "id" "firstname" and "lastname". What I want is to use a loop to echo all the field values in the result row without having to quote each column name like
$row["id"]
below is the working code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($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 "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " .
$row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
any thoughts please
This should work..
while($row = $result->fetch_array()) {
$i=0;
while($i<count($row)){
echo $row[$i];
$i++;
}
}
just use a foreach loop inside while like this
foreach($row as $key=>$value){
echo "<br> $key: ". $value. "<br>";
}
If I understand what you want to do is automate the output of the name of each column with its value (independent of the number of columns you get).
So do it like this :
if ($result->num_rows > 0) {
foreach($result->fetch_assoc() as $row) { // browse each records
foreach($row as $col => $value) { // browse each columns on a record
echo "<br>$col: $value<br>";
}
}
}
else {
echo "no result";
}

Displaying results of SQL query as a table?

I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.
My PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
check with var_dump :
some like that:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
maybe the query it's wrong.

learning how to read data from MYSQL

I'm trying to learn php and mysql, I was trying to read data from my database where i was following something online and encounter an error(s) here is my code
<?php
include 'includes/conn.php';
$query =sprintf("Select * from customer");
$result = mysql_query($query);
if (!$result)
{
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
$message .= 'everything' .$query;
die($message);
}
while ($customer = mysql_fetch_assoc($result))
{
echo $row['cust_id'];
echo $row['fname'];
echo $row['lname'];
echo $row['gender'];
echo $row['dob'];
}
mysql_free_result($result);
?>
//this is where i was trying to make a connection with the database
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db ='telmar_php';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
Replace
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
here your string is single quotation marks in it's is causing the problem
with
$message ="from you see this then it's not connecting!".mysql_error() ."\n";
...and to display your results change this:
while ($customer = mysql_fetch_assoc($result))
to this:
while ($row = mysql_fetch_assoc($result))
Please review the below example for reading data my mysql using php.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}

converting from array to string, implode didnt work

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "comp4";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id , First_Name, Last_Name FROM member WHERE username='tracy'";
$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["First_Name"]. " " . $row["Last_Name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I have this code in order to display some details about a member in my database but I want to make it into a string and a variable, I tried to use the php implode function but I recieved an error saying that some of the other variables were unexpected any tips or anything wrong that im doing?
Where you used implode()
`<?php
$id = $row["id"];
$first_name = $row["First_Name"];
$arr=array ($id,$first_name);
echo implode(" ",$arr);
?>`
In your while loop, you should first concatenate all your column in one array, to be then collapsed in one single string as following :
while($row = $result->fetch_assoc())
{
// temp variables
$id = $row["id"];
$first_name = $row["First_Name"];
$last_name = $row["Last_Name"];
$array = array($id, $first_name, $last_name); // creating an array
$result = implode(" - ", $array); // Collapsing data using dash
printf($result); // displaying data collapsed
}
from official php implode doc
I you want the result in a string, replace the echo by this:
$results .= "id: " . $row["id"]. " - Name: " . $row["First_Name"]. " " . $row["Last_Name"]. "<br>";
Now everithing is in $results
Here is perfect solution for your problem.. It will work. i am using mysql extension rather than mysqli..

Getting only one row of a mysql table displayed using PHP

I Currently have this code that displays all entries rows from a mysql table and displays them on a web page;
<?php
$servername = "localhost";
$username = "appuser1";
$password = "*****";
$dbname = "acmefg_app";
$row = mysql_fetch_array(mysql_query("SELECT jobnumber FROM appdata WHERE id = '5608' LIMIT 1"));
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, jobnumber, assetnumber FROM appdata";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How can I change this so it only displays one result? Lets say in this case I wanted to display the row with an id of 5611? Many Thanks
If you want to show only one row then no need to use while() loop. Remove loop
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
Or can set LIMIT 1 in your query string.
SELECT id, jobnumber, assetnumber FROM appdata LIMIT 1

Categories