I am trying display images on webpage, where image path stored in database and images is stored in server.But i am not able to display those images using following codes, so pls somebody help me with this issue,..
<form method="post" enctype="multipart/form-data" action="file_upload.php">
<table>
<?php
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");
$path1 = mysql_query("select * from '$tbl_name' where id='1'");
$path2 = mysql_query("select * from '$tbl_name' where id='2'");
$path3 = mysql_query("select * from '$tbl_name' where id='3'");
echo '<tr><td><img src="$path1"></td>' ;
echo '<td><img src="$path2"></td>' ;
echo '<td><img src="$path3"></td></tr>' ;
?>
</table>
</form>
A couple of things before we begin:
I recommend using mysqli, and will do so in my examples below (mysql is deprecated)
I'll use a cycle to iterate through results, instead of querying each element individually.
PHP code
$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if (!$conn)
{
die('Could not connect: ' . mysqli_connect_error());
}
$result = mysqli_query($con, "SELECT * FROM `$tbl_name`");
while ($row = mysqli_fetch_array($result))
{
echo '<tr><td><img src="'.$row['image'].'"></td>' ;
}
Note how I first "fetched" the results from the query. The query first returns a mysqli object, one that contains all the results the query returned. These have to be extracted; the method I present is widely used in examples elsewhere as well.
Also note how the backtick character was used instead of single quotes when referring to the table.
After execute the query we will get the result set cursor. We need to iterate it to get all the rows .
Try the below code it should work.
$result = mysql_query("SELECT * FROM '$tbl_name' WHERE id IN ( 1, 2, 3 ) ");
if (!$result) {
// show your respective error messages
}else{
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td><img src="'.$row['database_column_name'].'"></td>' ;
}
}
mysql_query(); has 2 arguments.
argument1: the connection.
argument2: the query.
this is what i will do if i was you:
$sql = "select * from `$tbl_name` where `id` between 1 and 3";
$path = mysql_query($conn, $sql);
while($row = mysqli_fetch_array($patht)) {
echo '<tr><td><img src="' . $row['name of colum'] . '"></td></tr>' ;
}
mysql_close($con);
sorry for my bad english. i'm Dutch.
Related
I am trying to get a question with answers out of my database. I just want to get one thing out of the database and not with a row. I thought this would work but it puts out this: Resource id #4 can someone explains what I am missing.
Thanks :)
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('lotto');
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
$test = mysql_query($sql);
echo $test;
?>
As said at least 10000 times everywhere in internet, never use MySQL_ ! (If your are trying to learn something new by using tutorials over internet, don't use old ones)
I recommend to use PDO which is modern API in PHP and a lot more secure when using it correctly with prepared statement ! But you can also use MYSQLI which is more similar to the MYSQL !
You have to export your data from return array :
Using PDO :
$db = new PDO ("mysql:host=".$hostname.";dbname=".$dbname, $username, $password);
$query = $db -> prepare ("SELECT * FROM vraag1");
$query -> execute (array ());
$rows = $query -> fetchAll (PDO::FETCH_ASSOC);
foreach ($rows as $row)
{
echo $id = $row["id"];
echo $vraag = $row["vraag "];
echo $AntwA = $row["AntwA "];
echo $AntwB = $row["AntwB "];
echo $AntwC = $row["AntwC "];
echo $AntwD = $row["AntwD "];
}
Using MYSQLI :
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM vraag1";
$rows = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($rows))
{
echo $row["id"];
echo $row["vraag"];
echo $row["AntwA"];
echo $row["AntwB"];
echo $row["AntwC"];
echo $row["AntwD"];
}
First of all the mysql function you are using is depreciated and no longer supported. you should use mysqli or pdo instead with prepared statements.
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1";
$test = mysqli_query($conn, $sql);
if (mysqli_num_rows($test) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($test)) {
echo "ID : ".$row['id']."<br>";
echo "vraag :".$row['vraag']."<br>";
echo "AntwA :".$row['AntwA']."<br>";
echo "AntwB :".$row['AntwB']."<br>";
echo "AntwC :".$row['AntwC']."<br>";
echo "AntwD :".$row['AntwD']."<br>";
}
} else {
echo "no results found";
}
mysqli_close($conn);
?>
For select function mysql_query() returns a resource on success, or FALSE on error.
so your assignment statement
$test = mysql_query($sql);
assign the resource to $test.
if you want the data inside the resource you can do
while($row= mysql_fetch_assoc($test)):
print_r($row);
endwhile;
also you can access the $row['column_name']
If you want to return only one row you can do this limit in query
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1 limit 1';
You need to add something like the following:
while($row = mysql_fetch_array($result)){
echo $row['id'];
echo $row['vraag'];
echo $row['AntwA'];
echo $row['AntwB'];
echo $row['AntwC'];
echo $row['AntwD'];
}
use mysqli instead of mysql
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
mysqli_select_db('lotto');
$sql = 'SELECT id, vraag, AntwA, AntwB, AntwC, AntwD FROM vraag1';
$test = mysqli_query($sql);
echo $test;
?>
I am trying to connect my html page with MySQL database to show data from one specific table to my page, but I always get an error actually it just goes to die part of an SQL code. I am really new to PHP programming so please can someone help me, what am I doing wrong?
Here is my code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AJDE</title>
</head>
<?php
$servername = "localhost";
$username = "******";
$password = "*****";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "select * from PERCENTILE";
$result = mysqli_query($conn,$query);
if(!$result) {
die ("Umro!");
}
/* close connection */
$mysqli->close();
?>
<body>
</body>
</html>
Thank you!
Do you want to show the table as a HTML table or just an array?
The following is what I did to display my table as a HTML table:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$dbname = 'dbname';
$selectedTable = 'whateverTableYouWant';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if (!$conn){
die('cannot connect to mysql');
}
$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {
echo("<div class = 'data_wrapper'>");
// Display Header of the table
$fieldcount=mysqli_num_fields($result); //value = number of columns
$row = mysqli_fetch_assoc($result); //Fetch a result row as an associative array:
//array to string conversion
echo("<table id='example' class='table table-striped table-bordered' cellspacing='0' width='100%'>");
echo("<thead> <tr>");
foreach($row as $item){
echo "<th>" .$item. "</th>";
}
echo("</tr> </thead>");
//Footer
echo("<tfoot> <tr>");
foreach($row as $item){
echo "<th>" .$item. "</th>";
}
echo("</tr> </tfoot>");
//Display Data within the table
echo("<tbody>");
while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
foreach ($row as $item){
echo "<td contenteditable = 'true'>" . $item . "</td>"; //Change contenteditable later
//Editable data should be constricted, int = numbers only, string = words, date = date
}
echo "</tr>";
}
echo("<tbody>");
echo "</table>";
echo("</div>");
}
The following is just displaying as an array:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '*****';
$dbname = 'dbname';
$selectedTable = 'whateverTableYouWant';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if (!$conn){
die('cannot connect to mysql');
}
$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {
while ($row = mysqli_fetch_array($result)){
print_r($row);
}
}
Please change the connection string like mentioned below.
<?php
$con = mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Please let me know if you've any queries.
I Think you need to select a database where you will run the query:
Try with this code:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with (so replace the database name examples)
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM PERCENTILE");
//fetch the data from the database and display results
//replace id,name,year with the columns you have on your table PERCENTILE and you want to show
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'name'}."Year: ". $row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>
Hope it helps,
Vince.
I'm using PHP code to upload or insert data from MDB file to MySQL database.I want my table value get inserted into MySQL database. But data does not inserted into MySQL database.This code shows me no error.Here is my code. please help I have tried every solution on net.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'payroll_system';
//mysql
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$sql = "SELECT * FROM attendance";
$result = mysql_query($sql);
//mdb
$conn2 = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn2->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\xampp\\htdocs\\payroll\\eTimeTrackLite1.mdb");
$rs = $conn2->Execute("SELECT * FROM AttendanceLogs");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$column1=$row["AttendanceLogId"];$column2=$row["AttendanceDate"];$column3=$row["EmployeeId"];$column4=$row["InTime"];
$column6=$row["OutTime"];$column8=$row["Duration"];
echo "hello";
echo $column1;echo $column2;echo $column3;echo $column4;
$rs->MoveFirst();
while (!$rs->EOF)
{
$attendance_id = $rs->Fields("AttendanceLogId");
$attendance_date = $rs->Fields("AttendanceDate");
$emp_id = $rs->Fields("EmployeeId");
$in_time = $rs->Fields("InTime");
$out_time = $rs->Fields("OutTime");
$duration = $rs->Fields("Duration");
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '"); ?>
<?php
$rs->MoveNext();
}
}
?>
</table> <?php
mysql_free_result($result);
$rs->Close();
$conn2->Close();
$rs = null;
$conn2 = null;
?>
you need to INSERT each records data instead of UPDATE. Re-write the following line:
mysql_query("UPDATE attendance SET AttendanceLogId = '$attendance_id', AttendanceDate='$attendance_date', EmployeeId='$emp_id',InTime='$in_time',OutTime='$out_time',Duration='$duration' '");
I was trying to make a backup of my MySQL db called "backup" using a PHP script below, but for some reason, it doesnt work. Any ideas what is wrong? I wanted to create a file called test.sql in the same folder that would contain the data (because the db is quite big I only selected values of Temp>35, but I could change that later). Right now when I run it, I get the echo, but no file created.
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$tableName = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * WHERE Temp>35 INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
echo "Backed up";
?>
Just Try.
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName WHERE Temp>35";
$result = mysql_query($query) or die(mysql_error());
Try this and let me know:
<?php
$dbhost = '...';
$dbuser = '...';
$dbpass = '...';
$dbname = 'backup';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$databasename = 'backup';
$backupFile = 'test.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $databasename WHERE Temp>35";
$result = mysql_query($query);
echo "Backed up";
?>
<?php
$source_db='source_db';
$target_db='target_db';
$server='127.0.0.1';
$user='root';
$password='';
mysql_connect($server,$user,$password);
mysql_select_db($source_db);
// Get names of all tables in source database
$result=mysql_query("show tables");
while($row=mysql_fetch_array($result)){
$name=$row[0];
$this_result=mysql_query("show create table $name");
$this_row=mysql_fetch_array($this_result);
$tables[]=array('name'=>$name,'query'=>$this_row[1]);
}
// Connect target database to create and populate tables
mysql_select_db($target_db);
$total=count($tables);
for($i=0;$i < $total;$i++){
$name=$tables[$i]['name'];
$q=$tables[$i]['query'];
mysql_query($q);
mysql_query("insert into $name select * from $source_db.$name");
}
?>
im having trouble with this, this is my code
?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'password';
$database = 'nzpcgames';
$table = 'gameinfo';
# $dbcon = mysql_pconnect($db_host,$db_user,$db_pwd);
if (!$dbcon)
{
die('Could not connect : ' . mysql_error());
exit;
}
mysql_select_db($database, $dbcon);
$query = "SELECT gameinfo.rank, gameinfo.game, gameinfo.platform, gameinfo.genre, gameinfo.publisher, gameinfo.developer, gameinfo.score*
FROM gameinfo
WHERE (((gameinfo.genre)='rpg'))";
$result = mysql_query("SELECT * FROM {$table}");
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
echo 'No results were found';
exit;
}
?>
though every time I try to excute it, i get the title error. On my tutorial power point it said to change $result, but it did not say what to, thanks for your help
may be it is because you didn't put the connection,
$result = mysql_query("SELECT * FROM {$table}",$dbcon);
or you could try
SELECT COUNT(*) FROM $table_name
check out this http://www.php.net/manual/en/function.mysql-num-rows.php