Display an image from a MySQL database using PHP - php

New to this but loving it. I have a database of URLs of pictures of cats that I want to display in a webpage. I wrote some php to do this but all I'm seeing it the URL of the image and not the image itself. Here is my full code:
<html>
<body>
<h1>Katz!!</h1>
<!--Connect to Database-->
<?php
$db_host = "localhost";
$db_username = "Alex";
$db_pass = "";
$db_name = "cats";
#mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mySQL");
#mysql_select_db("$db_name") or die ("No database");
$sql="SELECT * FROM cats_test";
$records = mysql_query($sql);
?>
<!--Populate table from database-->
<table border="2" cellpadding="1" cellspacing="1">
<tr>
<th>cat table</th>
</tr>
<?php
while($cats_test=mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>".$cats_test['image']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
As you can see, the database name is "cats" with a table called "cats_test" that holds all of the images in a column called "image" you can find a screenshot of that here
You can also see the URLs being displayed instead of images here
I've probably done something really stupid, so would appreciate any help you guys might have!!

Replace your :
echo "<td>".$cats_test['image']."</td>";
With :
echo "<td><img src='".$cats_test['image']."' ></td>";

Related

MySQL 5.6 to PHP to HTML Table Fail: Diagnosis of table creation snippet

I have reviewed, and edited, all the code previously discussed and it seems to be working fine now. Thanks very much for your help. Below is the last snippet that seems to be hanging the code:
print('N. of records: '.$result->num_rows.'<br />');
print('<table>');
while ($row = $result->fetch_object()){
print('<tr><td>'.$row['last'].'</td><td>'.$row['first'].'</td></tr>');
}
print('</table>');
I can not find an error in this snippet. As well, I can verify that the connection to the database is working because a test in the code shows that:
"N. of records: 5" were selected. Those 5 records all have data in the 'first' and 'last' fields. So, I think there must be an error in the code above which would not cause an error, but would also still cause the table to not be created. To be clear, the table is not just empty, it is not there at all.
Since the table is not being created, my guess was that the error must be in the print('<table>'); line, but for the life of me, I can't see an error in it. Can you?
It seems there are many errors in your code and also you didn't use the proper MySQLi functions. You should have some more research on the MySQLi with PHP and then it will be quite easy for you.
I refined your code have a look and try this:
<html>
<body>
<?php
$dbhost = 'localhost:3306';
$username = "root";
$password = "";
$database = "newworld";
$conn =mysqli_connect($dbhost, $username, $password, $database);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
} else {
#db connected
}
/**/
$sql = 'SELECT first, last FROM photos';
$result = mysqli_query($conn, $sql);
/**/
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td><font face="Arial, Helvetica, sans-serif"><u>first</u></font></td>
<td><font face="Arial, Helvetica, sans-serif"><u>last</u></font></td>
</tr>
<?php
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row["first"]; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $row["first"]; ?></td>
</tr>
<?php
}
} else {
echo "0 results";
}
?>
</table>
</body>
</html>
Note*: Update your database details like DB name, username, and password

MySQL doesn't seem to work

So I made my first database online. I used phpmyadmin, I created the table and the user.
Now I'd like to show the table on a page of my site, as well as giving the possibility to people to edit the database from the site.
My problem is that the database does not work: it doesn't connect. I have no idea what to do.
My database is called letstenf_santi and my table passeggeri.
This is the code I'm trying to use to show the table on the site.
<?php
//establishing connection
mysql_connect('localhost', 'root', '');
//selecting a database
mysql_select_db('letstenf_santi');
$sql = 'SELECT * FROM `letstenf_santi`.`passeggeri`';
$records=mysql_query($sql);
?>
<html>
<head>
<title>mostra</title>
</head>
<body>
<table width="300" border="1" cellpadding="10" cellspacing="1">
<tr>
<th>id pass</th>
<th>nome</th>
<th>eta</th>
<th>sesso</th>
</tr>
<?php
while($pass=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$pass['idpasseggero']."</td>";
echo "<td>".$pass['nome']."</td>";
echo "<td>".$pass['eta']."</td>";
echo "<td>".$pass['sesso']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
instead of this,
mysql_connect('localhost', 'root', '');
mysql_select_db('letstenf_santi');
you can try this,
$connection=mysql_connect('localhost', 'root', '');
$db=mysql_select_db('letstenf_santi',$connection);
try this code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "letstenf_santi";//your db name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM tablename";//replace table name with your table name
$result = mysqli_query($conn, $sql); ?>
<html>
<head>
<title>mostra</title>
</head>
<body>
<table width="300" border="1" cellpadding="10" cellspacing="1">
<tr>
<th>id pass</th>
<th>nome</th>
<th>eta</th>
<th>sesso</th>
</tr>
<?php if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['idpasseggero']."</td>";
echo "<td>".$row['nome']."</td>";
echo "<td>".$row['eta']."</td>";
echo "<td>".$row['sesso']."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</table>
</body>
</html>

How can I pass a data to a session coming from an array which also comes from database in PHP?

I have this problem. I am displaying all the animals which is owned by a certain pet owner. Here is my code:
<?php
session_start();
$_SESSION['indicator'] = "";
$conn = mysqli_connect("localhost","root","","appnimal")
or die ("Could not connect to mysql because ".mysql_error());
$sql=mysqli_query($conn,"SELECT * FROM tbl_pet_animal WHERE pet_owner_id='$user'")
or die(mysql_error());
while($row=mysqli_fetch_array($sql))
{echo "<tr class='even pointer record'>
<td class=' '>",$row[1],"</td>
<td class=' '>",$row[2],"</td>
<td class=' '>",$row[3],"</td>
<td class=' '>",$row[4],"</td>
<td class=' '>",$row[5],"</td>
<td class=' '>",$row[6],"</td>
<td class=' '>",$row[7],"</td>
<td class=' '>",$row[8],"</td>
<td class=' '>",$row[9],"</td>
<td class=' '>",$row[10],"</td>
<td><button type='button'
onClick=location.href='try.php?".$_SESSION['indicator']."=$row[1]'>
</td>";
?>
What am I trying to do is let us say, echo the $_SESSION['indicator'] value in another page. This is my code there from another page where I want to echo the value:
<?php
session_start();
$indicator="";
$indicator= $_SESSION['indicator'];
$conn = mysqli_connect("localhost","root","","appnimal")
or die ("Could not connect to mysql because ".mysql_error());
$sql=mysqli_query($conn, "SELECT pet_animal_name FROM tbl_pet_animal WHERE pet_animal_name='$indicator'") or die(mysql_error());
while($row=mysqli_fetch_array($sql)){
echo ($row['pet_animal_name']);
}
?>
But when I go there, it doesn't display anything. Please help me.
It displays nothing because (at least) in this code, your $_SESSION['indicator'] is "" (empty string) and You probably dont have pat with name "".
You can replace this part in first script :
<?php
session_start();
$_SESSION['indicator'] = "";
$conn = mysqli_connect("localhost","root","","appnimal")
With
<?php
session_start();
$_SESSION['indicator'] = "ANY_NAME_FROM_DB"; // N.B.: strings can be case-sensitive.
$conn = mysqli_connect("localhost","root","","appnimal")
And then check if you get something.
UPDATE #1 (from comments)
If you want to assign value on line
onClick=location.href='try.php?".$_SESSION['indicator']."=$row[1]'
You should do this instead:
onClick=location.href='try.php?".$_SESSION['indicator']=$row[1]."'
Because in your example "=$row[1]" is part of string, not php code. It means that .'=whatever' will not have any impact on value of your $_SESSION['indicator'].

Modify php script to populate SQL Server 2012

I have this php script that parses data from a regular url in the form of "http://example.com/board.php?username=John&score=15&session=976837465", places that in a simple MySQL db, then sorts the score to form a top 10.
Can someone help me with the code to do the same for a SQL Server 2012 db? Please assume that the db has already been created.
Note : I know the method above is insecure and not recommended, but at present it is the only way I am able to pass data over - trust me on this.
Please find the full php code below.
Thanks in advance,
John
<?php
$dbhost = 'localhost'; // Database Host Name - usually 'localhost'
$dbname = 'mydb'; // Database Name
$dbuser = 'myusername'; // Database User Name
$dbpass = 'mypassword'; // Database User Password
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname,$conn);
$game = 'My Game'; // The Name of your game
{
$name = urldecode(mysql_real_escape_string($_GET['username']));
$score = intval(mysql_real_escape_string($_GET['score']));
$gamesession = mysql_real_escape_string($_GET['session']);
global $tlb;
if (!empty($game) && !empty($name) && !empty($score) && !empty($gamesession)) {
$query = "INSERT INTO game_leaderboard (game, name, score, session)
VALUES('$game', '$name', '$score', '$gamesession')";
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
}
}
?>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Sansita+One'
rel='stylesheet' type='text/css'>
<link href="table.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div role="main">
<header>
<h2 class="toptitle">My Game</h2>
</header>
<?php
$lboard = "SELECT * FROM game_leaderboard WHERE game = '$game' ORDER BY score DESC LIMIT 10";
$leaderboard = mysql_query($lboard) or trigger_error(mysql_error()." in ".$lboard);
?>
<table class="th">
<thead style="background:#f7f7f7;">
<tr>
<td style="font-size: 18px; width:30%;">Name</td>
<td style="font-size: 18px; width:70%;">Score</td>
</tr>
</thead>
<tbody style="background:#ffffff;border:1px solid #bfbfbf;">
<?php
$i=0;
while ($i < mysql_numrows($leaderboard)) { ?>
<tr style="border-bottom:1px solid #cccccc;">
<td><?php echo ucwords(mysql_result($leaderboard,$i,"name")); ?></td>
<td><?php echo mysql_result($leaderboard,$i,"score"); ?></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</div>
</body>
</html>
PHP has made it easy to access data from any database, you can follow this guide i found here for ms sql servers http://www.easysoft.com/developer/languages/php/sql_server_unix_tutorial.html

Displaying the info from a MySQL database onto a html table

Recently I have been building my first website and as it is my first time working with PHP and databases I am having trouble. I have been researching for an answer for over three hours now. After following several tutorials I cannot get the databases information to display inside the table on my webpage. The only items that appear are a table header and one blank row. Am I not connecting to the database or is it something else that has to do with my code? Below is the code from the PHP that is inside my html file.
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('jcsavage_initiumjobs');
if(!$conmnection){
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT name, address, ages FROM companies ORDER BY name");
?>
<table id='company_tables' border='1' align='centre'>
<tr> <th colspan='3'>Companies</th> </tr>
<tr> <th>Name of Company</th> <th>Location</th> <th>Ages Accepted</th> </tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr><td> <?php . $row['name'] . ?> </td><td> <?php . $row['address'] . ?> </td><td> <?php . $row['ages'] . ?> </td></tr>
<?php
}
?>
</table>
<?php
mysql_close($connection);
?>
I also had a second question. What do the strings localhost and root mean? And why is the password blank? Any help is appreciated. If you need more information about my database, my website hoster, more code, or anything else, feel free to ask.
Try
<?php
while($row = mysql_fetch_array($result)){
?>
<tr><td> <?php echo $row['name']; ?> </td><td> <?php echo $row['address']; ?> </td><td> <?php echo $row['ages']; ?> </td></tr>
<?php
}
?>
$connection = mysql_connect('localhost', 'root', '');
localhost is the domain of the server that is hosting the database 'jcsavage_initiumjobs'
root is the MySQL userid that you are connecting to MySQL server with.
'' is the password, which in this case has not been set. When you set a password on the Userid 'root' you would add that instead of an empty (non existant) password
So for example if you set root's password to 'andBranch' you world code you connect as
$connection = mysql_connect('localhost', 'root', 'andBranch');
Try this code
<?php
$connection = mysqli_connect('localhost', 'root', '','jcsavage_initiumjobs');
if(!$connection){
die("Could not connect:" . mysqli_connect_error());
}
?>
<html>
<body>
<table id='company_tables' border='1' align='centre'>
<tr><th colspan='3'>Companies</th></tr>
<tr><th>Name of Company</th><th>Location</th><th>Ages Accepted</th> </tr>
<?php
$statement = "SELECT name, address, ages FROM companies ORDER BY name";
if($result = mysqli_query($connection, $statement))
{
while($data = mysqli_fetch_object($result))
{
echo "<tr><td>$data->name</td><td>$data->address</td><td>$data->ages</td></tr>";
}
}
?>
</table>
</body>
</html>

Categories