A couple of things, that may help. A nice simple db.connect.php
Call it at the start of routines that require access to the dbase. At the end of calls, don't forget to put inCode:<?php
/////////////////////////////////////////////////////////////////////////////////
//Database connection
/////////////////////////////////////////////////////////////////////////////////
$host="localhost"; // the path to the database, localhost, in this case, but also could be your MySQL
// provider. eg mysqldb5.your.host.com
$user="user_name"; // your login name, for this part set up a user with only sufficient privalidges to get the job done - NOT root !!!!
$password="Password"; // the password you set - no password is dangerous !!!
$database='your_database_name'; // The database you want to read
$link = mysql_connect($host,$user,$password) or die("Unable to Connect to Server");
mysql_select_db ($database)or die ('Unable to Select Database ');
/////////////////////////////////////////////////////////////////////////////////////////////
?>In my case I have it in phplibrary, so i call it thus ..Code:mysql_close($link);
A second hint - if you're testing for if magic_quotes are in existance, force a quit and tell the fool not to use them if it finds them !!Code:include './phplibrary/dbconnect.php';
//php code
mysql_close($link);
It causes problems, will really make Mr O'Reilly very fed up when it knarls his name up and is going to be
i) depreciated
ii) removed in further releases of php.
The correct route, is the use of mysql_real_escape_string. I note that you're already aware of that command.
Regards,
Phill.
@kasha
I tried fixing the code...you had some syntax issues in database.php and config.php.
config.php
database.phpCode:<?php
//Db constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost"); // Host name
defined('DB_USER') ? null : define("DB_USER", "root"); // Mysql username
defined('DB_PASS') ? null : define("DB_PASS", "root"); // Mysql password
defined('DB_NAME') ? null : define("DB_NAME", "User"); // Database name
?>
Code:<?php
require_once("config.php");
class MySQLDatabase {
private $connection;
function __construct() {
$this->open_connection();
}
public function open_connection() {
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
}
else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!db_select)
die("Database selection failed: " . mysql_error());
}
}
public function close_connection() {
if(isset($this->connection)) {
mysql_close($this->connection);
unset($this->connection);
}
}
public function query ($sql){
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
public function mysql_prep( $value ){
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists ("mysql_real_escape_string");
if($new_enough_php) {
if($magic_quotes_active )
$value = stripslashes($value);
$value = mysql_real_escape_string($value);
}
else {
if($magic_quotes_active)
$value = addslashes($value);
}
return $value;
}
public function confirm_query ($result) {
if (!$result)
die("Database query failed: " .mysql_error());
}
}
$database = new MySQLDatabase();
$db =& $database;
?>
@k1net1cs
Thanks i appreciate the help soo much! It actually works!
Ur officially the awesomest person in the world![]()
hello
i have a big problem here and i really dont know how to solve it
i made a form for uploading images in a file called upload.php and here is the code
then i made another file called upload_file.php and here is the codeCode:<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
and here is the tableCode:<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000)){
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{
$imgname = $_FILES["file"]["name"];
$imgtype = $_FILES["file"]["type"];
$imgsize = $_FILES["file"]["size"];
$imgdata = $_FILES["file"]["tmp_name"];
require_once('Connections/config.php');
$db = @mysql_select_db($database_config, $config) or die;
if (strlen($image) < 149000){
mysql_query ("insert into image (img_name,img_type,img_size,img_data,img_date) values ('$imgname','$imgtype','$imgsize',\"".$imgdata."\",now())");
}
}
}else{
echo "Invalid file";
}
?>
the problem is, i tried many codes for displaying this images not only yours i just got one error message
any ideas coz im starting to be crazy of this problem\Code:Warning: Cannot modify header information - headers already sent by (output started at C:xampphtdocsadminUntitled-6.php:12) in C:xampphtdocsadminUntitled-6.php on line 16
thank you
PS. im still new in php so please explain
Are you trying to include this file that shows that picture in some file? If yes then it doesn't work.. because you have set your header as your file will be an image.
What we need to see is the code that you used to get images from the database, not the one for putting them.
My guess is that you're trying to include the .php file that you used to get the image in another .php file, or you're trying to get the image straight from the .html file using some php codes.
Try reading my post a few pages back in this thread; click here.
If you can't understand what I'm trying to say there, then feel free to post the codes that you used to get the images.
hello again
at the beginning i want to thank you for your concern
what i want to do here...
is making a form for the end user so he will be able to upload image to the database to put to be finally appeared in a magazine
so i made the code for uploading the image to database as u see and i succeed in it my problem now is i cant display the picture
here is some explanation
so how i can do that all using only 4 pages
2 for input image
and 1 for post
and one for display the whole post including the image ofcourse
its somthing like in this webpage http://zakinettkmag.000a.biz/
as u can see i made the first step only, the image uploaded successfully and its now in database im willing to step 2 which the image display
i have a code to get the image from database
here i got the problem with the headerCode:<?php
require_once('../Connections/config.php');
$db = @mysql_select_db($database_config, $config) or die;
$gotten = @mysql_query("select * from pix order by pid desc limit 1");
if ($row = @mysql_fetch_assoc($gotten)) {
$title = $row[pid];
$bytes = $row[imgdata];
echo "yes";
} else {
echo "no";
}
// If this is the image request, send out the image
if ($_REQUEST[gim] == 1) {
header("Content-type: image/jpg");
print $bytes;
exit ();
}
?>
and the image appear as it opened by notepad
so please help
k1net1cs
i read your post and try to study it but there is something missing beside as i told you im just a beginner so i couldn't know what i will do
Code:HEADER;
// Put the recursive call to generate the images here.
// Maybe something like this:
?????????????????????????????????=======>like what i dont know
// Place db connection code here.
$sql = "SELECT `fileId` FROM `fileStore`";
$result = mysql_query($sql);
thank you
Last edited by WESSAmnz; 03-15-2010 at 09:50 PM.
The 'recursive call', or the 'loop', is actually the lines under the last line you quoted.
It's this one.
It's simply generating a simple code line in HTML, like this :Code:for ($row = mysql_fetch_assoc($result)) {
echo "<img src=\"getfile.php?&id=".
$row["fileId"].
"\" /><br /><br />\n";
}
This file, imgshow.php, just generate a simple HTML page with images.HTML Code:<img src="getfile.php?&id=1" /><br /><br />
The only thing it gets from the database is the fileId field.
So, if you have 3 images with fileIDs of 1, 2 & 3, then what imgshow.php will generate is this :
As you can see, the src attribute only points to a .php file, namely the getfile.php that I wrote below imgshow.php on my original post.HTML Code:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"> <html> <head> <title>Image Displayer</title> </head> <body> <img src="getfile.php?&id=1" /> <img src="getfile.php?&id=2" /> <img src="getfile.php?&id=3" /> </body> </html>
The reason there's a problem with headers in your original code is basically because you try to generate two files from one .php file, and that's why the headers, well, 'clashed' with each other.
Of course, at first you'd think that you only call the headers function just once so, why is there a clash?
Whenever you echo something, you actually have sent a header from the very first echo or print that you call.
That is why I separated the codes for getting the images.
imgshow.php generates the HTML, getfile.php fetches the images from the database.
HTH.
Last edited by k1net1cs; 03-16-2010 at 11:15 PM.
hey guys
im trying to get my code to require_once certain files. But i think im writing it wrong. The files are in an admin folder in my localhost.
How would i require once the files from localhost while working on the file in Admin?
for example :
All the above files are in stored in localhost, they are included in the file login.php which is in the admin folder.Code:require_once("../functions.php");
require_once("../database.php");
require_once("../user.php");
require_once("../session.php");
or should it be :Im so sorry if this makes no sense!Code:../admin/<filename>
Hi first i do realise this is an old tutorial but i am hopeful you still look over it from time to time.
I am working on a sort of social networking project which would be required to to display profile pictures and the likes . I have it set up so on the registration form i embedded your upload file and all the data gets sent to the correct tables
However when i try to display the data of my user profile page i can either see the persons data combined with Raw blob data or if i edit the content type i see only a broken image link.
Thats the code for my user_Profile.php which currently displays the broken image link.Code:<?php
header('Content-type: image/jpg');
require_once('auth.php');
require_once('config.php');
require_once('opendb.php');
session_start();
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
$user = $_SESSION['SESS_USERNAME'];
if(!$user)
{
echo "<br><p>Blah blah you arent logged in </p><br>";
}
else
{
//We need to grab the login name variable from the URL.
$user_id = clean($_REQUEST['user_id']);
$view_profile = mysql_query("SELECT * FROM members WHERE login = '$user_id'");
$user = mysql_fetch_array($view_profile);
$member_id= $user['member_id'];
$login = $user['login'];
$dob = $user['dob'];
$query = mysql_query("SELECT * FROM tbl_images WHERE id='$member_id'");
$row = mysql_fetch_array($query);
$content = $row['image'];
print ("member_id is : $member_id <BR>");
print ("UserName for login is : $login<BR>");
print ("Date of birth is : $dob<br>");
echo $content;
}
?>
if i remove header('Content-type: image/jpg');
I see the persons details but for the image i only see raw data .
I have tested this with the other code you supplied to check if the image was being uploaded correctly and it did display it using
Any tips or advice would be appreciated.Code:<?php
require_once('config.php');
require_once('opendb.php');
$storedid = "11";
$id = (int)$storedid;
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{
$query = mysql_query("SELECT * FROM tbl_images WHERE id='$id'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
}
?>
Thanks,
Pages.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks