2

I want to use php and load an iframe using a url i have stored in my database, could anyone show me how. I am not great at web programming.

I have tried things like:

<?php
echo <iframe src="$products_url" style="width: 100%; height: 100%;" frameborder="0"></iframe>;
?>

And I looked into: http://www.daniweb.com/web-development/php/threads/280949/displaying-url-from-mysql-db-in-an-iframe

Do I have do this whole database connect thing? Other parts of the database are used in my code without connecting like this website does.

Or do I need to declare a variable and set the parameter in the database to it?

Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
  • That depends entirely how you like to do it. Normally you can configure the database connection in the php.ini and do not need to care at all about that, however other things are possible as well. So, do like it suits you. This is not a programming question at all. Just feel good about what you want to do and do it. – M8R-1jmw5r Apr 30 '13 at 00:23
  • Thank you for the reply, I have been messing around and really just can't a basic iframe to show at all. – Jordan.J.D Apr 30 '13 at 00:31
  • You will likely see a blank page instead, right? Try to learn about the white page of death: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851 – M8R-1jmw5r Apr 30 '13 at 00:32
  • Yea i had the white page. I have been messing around and fixed it, thanks for this link though I will look through it. – Jordan.J.D Apr 30 '13 at 00:52

3 Answers3

0

In separated php file you can GET you URL string and return it back to your main page

<?php 

 database query here .. use $product_url as your link string variable

?>

<iframe src="$product_url" .....></iframe>
matzone
  • 5,703
  • 3
  • 17
  • 20
0

Make a database connection, but do not use the old mysql_connect. Try using PDO if you make your steps into this world.

// PDO as you can find on php.net

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser'; // your database username
$password = 'dbpass'; // your database password

// note: do not use root, but create a separate user for the database.

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

//this depends on the data in your database.
$sql = "SELECT url_column FROM table_name WHERE id_column = id;" 

$sth = $dbh -> prepare($sql);
$sth -> execute();
$sth -> fetchAll(PDO::FETCH_OBJ);

$products_url = $sth[0]->url_column;

The data should be fetched before your page renders the $product_url.

Edit: (extending explanation on pdo with resources)

PDO is the latest standard of handling database interaction. Provided it is configured correctly in the queries with prepared statements, PDO is safe to use and provides protection against injection. While you are making a simple single query, you can see that I do use a prepared statement.

One important rule is that you assign a designated user and password for your database. It is not safe to use root as username. Here is a resource that may help you with creating a user for the database in case you need explanation.

The best thing you can do is to familiarize yourself with PDO. You can get a good explanation from a series from phpacademy about PDO, which is well worth following and well invested time.

Daniel
  • 4,816
  • 3
  • 27
  • 31
  • Is this safe for me to use, or can someone access my page/database? – Jordan.J.D Apr 30 '13 at 03:36
  • I like to help you, but I sense that you are not very familiar with coding and may need a little more in depth explanation. There are resources that do an excellent job. I have extended my answer that provide some links that may be a great help for you. Even if you jut quick scan the resources, you may get more out of it then from me trying to explain stuff. – Daniel Apr 30 '13 at 04:06
  • Thank you very much for taking your time with me. I will look through all links. – Jordan.J.D Apr 30 '13 at 04:22
  • You're welcome. It takes time to learn, but a pointing finger in the right direction is a great help. Good luck and ask again if you need help. Do try to work out things a little and be concise in your questions. You will get better help that way. :-) – Daniel Apr 30 '13 at 05:26
-1
<?php
 ...
 //your php codes here..
 ...
 ?>

 <iframe src="<?php echo $products_url ?>" style="width: 100%; height: 100%;" frameborder="0"></iframe>

<?php
 ...
 //your another php codes here..
 ...
 ?>

Do I have do this whole database connect thing?

If you want to retrieve data from database, you have to make database connection. The tutorial is using mysql_* that is deprecated, I recomend you to use Mysqli or PDO instead.

egig
  • 4,370
  • 5
  • 29
  • 50