Php file that creates html, how do I get the information to come from the database?

Asked

Viewed 102 times

-2

As I do for the html created by this php instead of containing the text between the quotes, it contains information that comes from my database.

<?php
# Nome do arquivo html
$pagename = "pastatest/paginahtml.html";

# Texto a ser salvo no arquivo
$texto = "<h1>texto que vai conter no html</h1>";

#Criar o arquivo
$fp = fopen($pagename , "w");
$fw = fwrite($fp, $texto);

#Verificar se o arquivo foi salvo.
if($fw == strlen($texto)) {
   echo 'Arquivo criado com sucesso!!';
}else{
   echo 'falha ao criar arquivo';
}
?>
  • Have you made the connection to the database? this is the part that has doubt??

  • not I already have the connection to database, my problem is with select from, only that as you can see the variable $text only command the created text between the " " and I want this text in case comes from the database

  • 1

    @cadeira13 If possible edit the question and put the code that makes the connection, and also the select .. you want to save to the file. See also the [tour]!.

2 answers

2

Make a SELECT

<?php
# Nome do arquivo html
$pagename = "pastatest/paginahtml.html";

# Pega texto no Banco de dados

$query = $conn->prepare("SELECT * FROM sua_tabela WHERE id='1'"); //Em id vc coloca qual registro do banco de dados você quer pegar.
$query->execute();

$row = $query->fetch(PDO::FETCH_ASSOC);

# Texto a ser salvo no arquivo
$texto = "<h1>".$row['texto']."</h1>";

#Criar o arquivo
$fp = fopen($pagename , "w");
$fw = fwrite($fp, $texto);

#Verificar se o arquivo foi salvo.
if($fw == strlen($texto)) {
   echo 'Arquivo criado com sucesso!!';
}else{
   echo 'falha ao criar arquivo';
}
?>

The SELECT i made based on a PDO connection. Make a query according to your connection to the Database (Mysql, Mysqli, PDO, etc.)

  • people i did something wrong ta giving this error >> Fatal error: Call to a Member Function prepare() on a non-object in C: Appserv www test create.php on line 21

  • @chair 13 you first have to create a PDO connection and assign it to $Conn variable

  • yes I already got rsrs created here the Pdo connection, it went all right obgd

-1

The file using mysql will look like this:

<?php
# Nome do arquivo html
$pagename = "pastatest/paginahtml.html";

# Texto a ser salvo no arquivo
$selecionar = "SELECT * FROM tabela";
$conectar = mysql_query($selecionar);
$array = mysql_fetch_array($conectar);
$texto = $array['texto'];

#Criar o arquivo
$fp = fopen($pagename , "w");
$fw = fwrite($fp, $texto);

#Verificar se o arquivo foi salvo.
if($fw == strlen($texto)) {
   echo 'Arquivo criado com sucesso!!';
}else{
   echo 'falha ao criar arquivo';
}
?>

Note: mysql is obsolete. PDO or mysqli are most recommended.

  • people thanks for the reply, I was pleased obgd

  • if you can send me connection to the db I’m using this in PDO $host = "localhost";&#xA;$db = "devmedia";&#xA;$user = "root";&#xA;$pass = "88319076";&#xA;// conecta ao banco de dados&#xA;$con = mysql_pconnect($host, $user, $pass) or trigger_error(mysql_error(),E_USER_ERROR); &#xA;// seleciona a base de dados em que vamos trabalhar&#xA;mysql_select_db($db, $con);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.