Résolu Impossible d'afficher infos Mysql

Enzo34!

★REALITYGAMING★
Premium
Inscription
31 Janvier 2014
Messages
1 417
Réactions
329
Points
20 981
RGCoins
925
Bonjour j'ai besoin que ce code fonctionne :
PHP:
<?php
$host="xxxx"; //replace with database hostname
$username="xxxx"; //replace with database username
$password="xxxx"; //replace with database password
$db_name="xxxx"; //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);
?>

Pour pouvoir afficher sur une listView dans Android Studio :D
Voici l'erreur qui s'affiche dans mon naviguateur :

Code:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/u302146078/public_html/appa/employee_details.php:7 Stack trace: #0 {main} thrown in /home/u302146078/public_html/appa/employee_details.php on line 7

La ligne 7 Correspond à :
350d50f111d94f7e9aabacefbfa9a68d.png

Merci à celui qui m'aidera :hug:
 
Salut,

Ta connexion à MYSQL est obsolete, utilise PDO :
PHP:
$host="xxxx"; //replace with database hostname
$username="xxxx"; //replace with database username
$password="xxxx"; //replace with database password
$db_name="xxxx"; //replace with database name

$*** = new PDO("mysql:host=$host;dbname=$db_name", $username, $password));
$sql = "select * from emp_info";
$result = $***->execute($sql);
$json = array();

     if($***->rowCount($result) > 0){
       while($row=$***->fetch(PDO::FETCH_ASSOC)){
             $json['emp_info'][]=$row;
         }
      }
echo json_encode($json);
?>

Essaie mais je suis pas sur que sa fonctionne (j'ai du faire des petites erreurs)
 
Salut,

Ta connexion à MYSQL est obsolete, utilise PDO :
PHP:
$host="xxxx"; //replace with database hostname
$username="xxxx"; //replace with database username
$password="xxxx"; //replace with database password
$db_name="xxxx"; //replace with database name

$*** = new PDO("mysql:host=$host;dbname=$db_name", $username, $password));
$sql = "select * from emp_info";
$result = $***->execute($sql);
$json = array();

     if($***->rowCount($result) > 0){
       while($row=$***->fetch(PDO::FETCH_ASSOC)){
             $json['emp_info'][]=$row;
         }
      }
echo json_encode($json);
?>

Essaie mais je suis pas sur que sa fonctionne (j'ai du faire des petites erreurs)
Fatal error: Uncaught Error: Call to undefined method PDO::execute() in /home/u302146078/public_html/appa/employee_details.php:9 Stack trace: #0 {main} thrown in /home/u302146078/public_html/appa/employee_details.php on line 9

Code:
$con = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
$sql = "select * from emp_info";
$result = $con->execute($sql);
$json = array();

    if($con->rowCount($result) > 0){
      while($row=$con->fetch(PDO::FETCH_ASSOC)){
            $json['emp_info'][]=$row;
        }
     }
echo json_encode($json);
?>
 
Fatal error: Uncaught Error: Call to undefined method PDO::execute() in /home/u302146078/public_html/appa/employee_details.php:9 Stack trace: #0 {main} thrown in /home/u302146078/public_html/appa/employee_details.php on line 9

Code:
$*** = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
$sql = "select * from emp_info";
$result = $***->execute($sql);
$json = array();

    if($***->rowCount($result) > 0){
      while($row=$***->fetch(PDO::FETCH_ASSOC)){
            $json['emp_info'][]=$row;
        }
     }
echo json_encode($json);
?>
C'est normal, le code est censuré :p
 
Code:
$db_host  = '127.0.0.1';
$db_user  = 'root';
$db_pass  = '';
$db_database = '';

$bdd = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
Fatal error: Uncaught Error: Call to undefined method PDO::execute() in /home/u302146078/public_html/appa/employee_details.php:9 Stack trace: #0 {main} thrown in /home/u302146078/public_html/appa/employee_details.php on line 9

Code:
$*** = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
$sql = "select * from emp_info";
$result = $***->execute($sql);
$json = array();

    if($***->rowCount($result) > 0){
      while($row=$***->fetch(PDO::FETCH_ASSOC)){
            $json['emp_info'][]=$row;
        }
     }
echo json_encode($json);
?>
Salut , toujours pas résolu ?

PHP:
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="bdd"; //replace with database name

$PDO = new PDO("mysql:host=$host;dbname=$db_name", $username, $password));
$sql = "select * from emp_info";
$result = $PDO->execute($sql); // Ou essaie $result = $PDO->exec($sql) (pour executé directement la requete car c'est pas une requete "préparé" , avec des variables etc dans la requete)
$json = array();

     if($PDO->rowCount($result) > 0){
       while($row=$PDO->fetch(PDO::FETCH_ASSOC)){
             $json['emp_info'][]=$row;
         }
      }
echo json_encode($json);
?>

J'espère que sa fonctionnera ;)
 
Retour
Haut