简体   繁体   中英

getting an error to connecting mysql db in php

I am working on a new project and I am getting an error when I connect with my DB I don't know why it is happening my DB name and user name is all correct but why I am getting this error

Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\treeview\treeview.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\treeview\treeview.php on line 9

Code:

<?php
mysql_connect('localhost', 'root');
mysql_select_db('test');
$qry = "SELECT * FROM treeview_items";
$result = mysql_query($qry);
$arrayCategories = array();
while ($row = mysql_fetch_assoc($result)) {
    $arrayCategories[$row['id']] = array("parent_id" => $row['parent_id'], "name" => $row['name']);
}

I'll assume you are on PHP 7+, which means that mysql_query is deprecated. Please use PDO or mysqli.

More info on that function's man page: https://www.php.net/manual/en/function.mysql-query.php

You can obtain the connection to your database through:

$connection = new mysqli($hostname, $db_username, $password, $db_name);
if ($connection->connect_error){
    $error = "Failed to connect to db: " . $connection->connect_error;
    // Do whatever you want with $error
}
$connection->query("SET NAMES utf8mb4"); // mostly, people use utf8 instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM