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
Related
In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:
Fatal error: Uncaught Error: Class 'mysql' not found in C:\Apache24\htdocs\php_file.php, Stack trace: #0 {main} thrown in C:\Apache24\htdocs\php_file.php on line 41.
This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).
The code I'm using is:
$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";
$conn = new mysql($servername, $username, $password, $dbname);
$sql = "SELECT * FROM column";
$result = $conn->query($sql);
$conn->close();
My updated code uses mysqli, but is still getting the same error.
From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.
Try using mysqli. The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you.
https://www.php.net/manual/en/function.mysql-connect.php
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>
Use mysql_connect($servername, $username, $password) instead.
This is my error:
Fatal error: Uncaught Error:
Function name must be a string in /home/kmsrutge/public_html/Login2process.php:5
Stack trace:
#0 {main} thrown in /home/kmsrutge/public_html/Login2process.php on line 5
My code:
$db_server = $mysqli_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . $mysqli_error());
$mysqli_select_db($db_database) or die("Unable to select database: " . $mysqli_error());
$sql = "UPDATE Invoices SET Ord_ID = '$9144' WHERE Prod_ID = '98'; ";
$result = $mysqli_query($sql);
if($result) echo "UPDATE success!"; else echo "UPDATE failed!";
if($result)
{
echo "<p>New row was successfully inserted</p>".
$insertQueryCount = $insertQueryCount+1;
}
else
{
echo "<p>Your insert failed.</p>";
die($mysqli_error());
}
?>
I read that it may be a possible PHP 7 issue. Any idea on how to correct this?
Try changing the result variable to the following:
$result = mysqli_query($db_server, $sql);
Please check your code SQL function do not start with $ so you got the error. remove all $ from mysqli.
Please change the first line:
$db_server = mysqli_connect($db_hostname, $db_username, $db_password,$db_database);
you don't require mysqli_select_db function so comment that line and check.
Also the fifth line change like mysqli_query($db_server,$sql).
Please read (https://www.w3schools.com/php/php_mysql_intro.asp)this documents how to connect SQL and how to write a query.
I am trying to select data from a local database on my PC using PHP but i am getting this error when i run 127.0.0.1/test.php which is what the file is called.
error:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in
C:\xampp\htdocs\test.php:11 Stack trace: #0 {main} thrown in
C:\xampp\htdocs\test.php on line 11
Here is my PHP script:
<?php
$servername = "Windows local servername";
$username = "Windows username";
$password = "windows password";
$dbname = "dbname";
// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn){
die('error connecting to database');
}else{
mysql_select_db(dbname, $conn);
}
$sql = "SELECT UserId, UserEmail, UserPassword FROM User";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["UserId"]. " - UserEmail: " . $row["UserEmail"]. " " . $row["UserPassword"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I have looked around online but i cant figure out what is wrong here. I can run a hello world php script and it works so i am assuming its a connection issue with the database but what have i missed here? Thanks
EXTRA:
i have:
Try to run
phpinfo()
And look for the mysql extensions, in ubuntu you can install them by typing
sudo apt-get install php-pdo-mysql
Anyway the instruction you're trying to use was deprecated in HP 5.5.0 and deleted in PHP 7.0.0 so may be this is why its missing... depending on you PHP version, you could try with the mysqli extension instead like:
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$res = mysqli_query($mysqli, "SELECT 'Please, do not use ' AS _msg FROM DUAL");
$row = mysqli_fetch_assoc($res);
echo $row['_msg'];
You could get more info on the actual way of performing this with PHP7 at http://php.net/manual/en/mysqli.quickstart.dual-interface.php for instance
From the docs
This extension was deprecated in PHP 5.5.0, and it was removed in PHP
7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
You'll probably want to use Microsoft's drivers for PHP for SQL Server rather than the ODBC driver in PDO.
In place of
$conn = mysql_connect($servername, $username, $password, $dbname);
use
$connectioninfo=array("Database"=>"dbname", "UID"=>"userid", "PWD"=>"password", );
$con = sqlsrv_connect($server,$connectioninfo);
I am trying to migrate data from mysql to mongo. It adds one record fine to mongo but then on the second record I am getting
Fatal error: Uncaught exception 'MongoDuplicateKeyException' with message 'localhost:27017: E11000 duplicate key error index: app.hospitals.$_id_ dup key: { : ObjectId('558365d7423467484bd63af3') }'
Not sure what I am doing wrong
here is my code
<?php
//echo phpinfo();
$host = "localhost";
$user = "root";
$password = "root";
$database = "database";
// Create connection
$conn = new mysqli($host, $user, $password, $database);
$connection = new MongoClient();
$db = $connection->database;
$collection = $db->hospitals;
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM hospitals";
if($result = $conn->query($sql)){
$i=0;
while($row = $result->fetch_assoc()) {
foreach($row as $key=>$value){
$collection->insert($row);
unset($collection->_id);
}
if($i > 3) die;
$i++;
}
}
$conn->close();
?>
using
$collection->save($row);
instead of insert solved the issue. Not sure why though.
Had the same problem, I believe it's a problem with the PHP mongodb driver that it is not generating a new id inside the loop.
The weird part that the first time I called the script it inserted all the documents then the exception popped in the end, then when I dropped the database and called the script again, it only inserted one document before giving the exception.
Anyway I solved after reading this answer by explicitly assigning new id.
In your case, add this before the insert:
$row['_id'] = new MongoId();
I can't get mysql table to load on localhost. below i used PDO and the table worked fine. no errors...the table works fine in phpmyAdmin...please help driving me nuts. i imported table from my web server to local host from a earlier version..
<?php
$user = "root";
$pass = "root";
try {
$dbh = new PDO('mysql:host=localhost;dbname=iosLeads',$user,$pass);
foreach($dbh->query('SELECT * from Customer') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error! :" . $e->getMessage()."<br/>";
die();
}
?>
but when i used this code it show a blank screen, no errors..i tried same code with other tables and it works fine.
<?php
// set up the connection variables
$db_name = 'iosLeads';
$hostname = 'localhost';
$username = 'root';
$password = 'root';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
// a query get all the records from the users table
$sql = 'SELECT * FROM Customer';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare( $sql );
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// convert to json
$json = json_encode( $result );
// echo the json string
echo $json;
?>
I'm getting this in my php error.log
[17-Dec-2014 03:39:55 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:00 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:23 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
If the array in $result has strings other than UTF-8 encoded, the function json_encode() may fail and will return false (see PHP manual json_encode). To check this, dump the value of $result before using json_encode():
var_dump($result);
If you have an array in $result but no other return value than false from json_encode(), you must set the correct character encoding for the database connection.