Problem with PDO and mysql_query connection

Asked

Viewed 372 times

1

Hello, I created the connected.php file as below:

   <?
   $conn= new PDO("mysql:host=localhost;dbname=site", "root", "");

   $count = ('SELECT * FROM conteudo');
   $stmt = $conn->prepare($count);

   $stmt->execute();
   $result = $stmt->fetch(PDO::FETCH_ASSOC);
   return $result;
   ?>

And here I give only the include of the connected.php file.?

    <?php include "conexao.php"; ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Contentype" content="text/html; charset=iso-8859-1" />
    <title> Painel Adm. Conteudo</title>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/normalize.css">
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js">
    </script>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="/css/result-light.css">
    </head>
    <body>

    <div id="texto001" align="left" style=" font-family:Verdana, Geneva, sans-serif;
    color:#000000; font-size:12px; float:none; width:<? echo $res['larg_tex001'];?>px; 
    height:<? echo $res['altu_tex001'];?>; position: absolute; left: <? echo $res['hori_tex001'];?>px;
    top: <? echo $res['vert_tex001'];?>px;">

    <img align="left" src="upload/esp_texto.png" /> <?php echo $res['texto001'];?>
    </div>

    <img width="<? echo $res['larg_img001'];?>" height="<? echo $res['altu_img001'];?>" 
    align="left" src="upload/<? echo $res['img001'];?>" style="position: absolute; 
    left: <? echo $res['hori_img001'];?>px; top: <? echo $res['vert_img001'];?>px;" />

    </body>
    </html>

But I’m getting the following message below:

Mensagem recebida

I ask friends what is missing to work on WAMPSERVER? If your friends can help me solve this problem, I’d really appreciate it. Hugs to all, and I await your help.

2 answers

1


Create a connection:

$conn= new PDO("mysql:host=HOST;dbname=NOMEDOBD", "USUARIO", "SENHA");

Create a statement:

$count = 'SELECT COUNT(*) FROM livro';
$stmt = $conn->prepare($count);

and execute the statement:

$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result;
  • Do I make this statement in the same connection file or create another file? I run where in my mysql_query, or in the content page? Well I did that and stopped sending me messages, but it doesn’t bring me anything else, the page is totally empty. Teria como vc. demonstrate to me what would be the acquisitions connected and where the search will be made. From now on my thanks and excuse me, because I am very lay in PHP and I am trying to learn.

  • All together in this same order, open connection creates the statement with the desired query and runs. Forget what you did, you joined PDO and mysql_query, both do not cohesite, or is the way I did PDO or mysql_query.

  • I edited my question with your guidelines. That would be it?

  • Friend Ricardo, solved, besides his tips being correct, I was using <? echo $result ['item']; ? >, or was forgetting PHP after ? to fetch the data. .

  • @Murilocabral!

0

You are not making a connection with PDO but with mysql.

The message says the function mysql_connect() is deprecated, and will be removed from PHP. Ideally, use PDO or mysqli.

A simple example of PDO connection:

$conexao = new PDO("mysql:host=HOST;dbname=NOMEDOBD", "USUARIO", "SENHA"); 
  • OK Igor, I made the PDO connection as you taught me and how I do mysql_query, because now I’m getting these two messages: Deprecated: mysql_query(): The mysql Extension is deprecated and will be Removed in the Future: use mysqli or PDO Instead in C:wamp www site conteudo.php on line 3. Warning: mysql_fetch_array() expects Parameter 1 to be Resource, Boolean Given in C: wamp www site conteudo.php on line 4.

Browser other questions tagged

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