Mysql PHP XAMPP - Connect to database

Asked

Viewed 3,566 times

1

Hello, I need to create a webservice, but I have little experience with php and mysql, so I have some questions, I am using XAMPP and Mysql Workbench, in Workbench I did the connection test and said it worked, so maybe it is not a problem with Mysql. The webservice I am creating is to make the contents of the database available in a JSON, very simple indeed. I am basing myself on at that link, if anyone has any tutorial for this. I read that you may have to use PDO, it will be?

<?php
$host="XXXXX"; //replace with database hostname 
$username="XXXXX"; //replace with database username 
$password="XXXXX"; //replace with database password 
$db_name="XXXXXX"; //replace with database name

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from emp_info"; 
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['emp_info'][]=$row;
}
}
mysql_close($con);
echo json_encode($json); 
?> 

The database I can create, as well as update the values, but I have this problem and I doubt if it is a problem with Mysql, with XAMPP, PHP or some configuration that remains to be done. If anyone can give me some direction to know what I should chase to resolve.

  • 4

    You haven’t described your problem, but you can see you’re using mysql_*(), this extension has been discontinued to learn more Why should we not use mysql type functions_*?

  • The problem is in relation to the functioning, I will try to use another function. But I think that’s precisely, thank you for the quick response.

  • 3

    Complementing what Wellingthon said, if you are using PHP7+ you will not be able to use mysql_... because in that version no longer exists, and you will even have to use mysqli_...

1 answer

-1

Maybe your solution to connect to the PDO bank:

<?php
//inicia a conexão com dados informados
try{
$conn = new PDO ("mysql:host=localhost;
                database=banco_de_dados","login","senha");
}catch (PDOException $e){
    echo $e->getCode(); //mostra erro 
}
?>

Browser other questions tagged

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