PHP and DB connections replace with mysqli from mysql [duplicate] - php

This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 3 years ago.
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("dbname", $con);
$q = "select*from tablename";
$qry = mysql_query($q);
can you please help me to convert this mysql query to mysqli

Use like
// mysqli_connect(host,username,password,dbname,port,socket);
$con = mysqli_connect("localhost", "root", "", "your_db");
// mysqli_select_db(connection, dbname);
$db = mysqli_select_db($con, "dbname");
$q = "select*from tablename";
// mysqli_query(connection,query,resultmode);
$qry = mysqli_query($con, $q);
Refer below docs
PHP Mysqli
w3schools Mysqli

Related

What is an alternative for mysqli_fetch_all on shared hosting? [duplicate]

This question already has answers here:
Alternative to mysqli_fetch_all needed
(2 answers)
mysqli_fetch_all not working on shared hosting, need alternative [duplicate]
(2 answers)
Closed 5 months ago.
mysqli_fetch_all is not working.
$conn = mysqli_connect("localhost","root","","file_upload");
$sql= "SELECT * FROM files";
$result = mysqli_query($conn,$sql);
$files = mysqli_fetch_all($result, MYSQLI_ASSOC);
May this be of any help - using PDO.
$dsn = "mysql:host=$host;dbname=$dbname;";
$conn = new PDO($dsn, $user, $pass, null);
$sql = "SELECT * FROM files";
$rs = $conn->query($sql);
$files = $rs->fetchAll(PDO::FETCH_ASSOC);

How to fetch data from MySQL using mysql_fetch_array [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I am trying to fetch MySQL data using below code, but it seems have some mistake. Can anyone help please. Thank you!
<?php
$link = mysqli_connect("localhost", "myusername", "mypassword", "mydb") or die("Error " . mysqli_error($link));
$query = "SELECT * FROM users WHERE id=2";
$res = mysqli_query($link, $query);
$userRow = mysql_fetch_array($res);
echo $userRow["user_firstname"];
?>
Thank you #Mario
<?php
$link = mysqli_connect("localhost", "myusername", "mypassword", "mydb") or die("Error " . mysqli_error($link));
$query = "SELECT * FROM users WHERE id=2";
$res = mysqli_query($link, $query);
$userRow = mysqli_fetch_array($res);
echo $userRow["user_firstname"];
?>

Count number of rows in Mysql using PHP7 [duplicate]

This question already has answers here:
number of rows in a table
(3 answers)
Closed 2 years ago.
I have upgraded PHP on my server from version 5 to 7 and I found out that mysql_* functions were removed and now I should use mysqli or PDO. I updated the following code (mysqli instead of mysql) that I used to print number of rows in a given table, but it doesn't work as it used to.
$link = mysql_connect("xxx", "xxx", "xxx");
mysql_select_db("xxx", $link);
$result = mysql_query("SELECT * FROM blackandwhite", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n"; // This works in PHP5
Updated code PHP7:
$link = mysqli_connect("xxx", "xxx", "xxx");
mysqli_select_db("xxx", $link);
$result = mysqli_query("SELECT * FROM blackandwhite", $link);
$num_rows = mysqli_num_rows($result);
echo "$num_rows Rows\n"; // This doesnt work anymore.
The mysqli_ functions actually accept the link as the first argument, as it is non-optional like it is with the normal mysql_ functions.
This should work as you expect:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("host", "username", "password","db_name");
mysqli_set_charset($link, "utf8mb4");
$result = mysqli_query($link, "SELECT count(*) FROM blackandwhite");
$num_rows = mysqli_fetch_row($result)[0];
echo "$num_rows Rows\n";

PHP get data from DB not working [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
Can you figure out my code? All the code does is: No database selected It won't get the data from the db. The server os is Ubuntu or OS X. I been pulling my hair out for hours.
<?php
mysqli_connect("localhost", "root", "");
mysql_select_db("hit-counter");
$sql_get_count = mysql_query("SELECT id FROM hit_info ORDER BY id DESC LIMIT 1");
if($sql_get_count === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($sql_get_count)) {
print_r($row);
}
?>
I try this, it does the same
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("hit-counter");
$sql_get_count = mysql_query("SELECT id FROM hit_info ORDER BY id DESC LIMIT 1");
if($sql_get_count === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($sql_get_count)) {
print_r($row);
}
?>
you have an error in your code. You use mysqli_ function to connect the server but you use a deprecated function mysql_ to select the database.
Try this code:
mysqli_connect("localhost", "root", "");
mysqli_select_db("hit-counter");
Another option when using mysqli_ is to select the database you want during connecting to the server:
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
You didn't mention database name:try this
<?php
$con = mysqli_connect("127.0.0.1","root","654321","testV2") or die("Some error occurred during connection " . mysqli_error($con));
// Write query
$strSQL = "SELECT id FROM did ORDER BY id DESC LIMIT 1";
// Execute the query.
$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo $result["id"]."
";
}
// Close the connection
mysqli_close($con);
?>
You cannot interchange the mysql and mysqli functions, please modify your mysql_select_db to mysqli_select_db.
I will not go over the errors everyone else has pointed out. But, I will mention one that no one has. I think the - character in your database name will also cause problems. You should enclose the database name in back ticks. The back tick is this ` character, most likely the far left key above the TAB key. If you had error reporting turned on, or looked at your php error log, you would have seen the error.

PHP: mysqli_query is not working [duplicate]

This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 3 years ago.
I've a doubt with mysqli_query..
this is a part of my code:
$con = db_connect();
$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";
$result = mysqli_query($con, $sql);
return $result;
I can't do the query...
If I try to do a query like this:
$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";
It works.
What's the problem?? I can't use SET with mysqli_query?
Thanks
You can not execute multiple queries at once using mysqli_query but you might want to use mysqli_multi_query as you can find out in the official documentation:
http://www.php.net/manual/en/mysqli.multi-query.php
Lets start with creating a working php script.
<?php
// replace for you own.
$host ="";
$user = "";
$password = "";
$database = "";
$con= mysqli_connect($host, $user, $password, $database);
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
// Begin SQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($con,$sql) OR Die('SQL Query not possible!');
var_dump($result);
return $result;
var_dump($result);
// End SQL query
mysqli_close($con);
};
?>
INSERT query:
$sql= "INSERT INTO categorias(name) VALUES ('ssss')";
mysqli_query ($con,$sql) OR Die('SQL Query not possible!');
UPDATE and DELETE query:
$sql= "DELETE FROM users WHERE username = 'Hola';";
$sql.= "UPDATE users SET foreign_key_checks = 0 WHERE username = 'Hola'"; /* I made a guess here*/
mysqli_multi_query ($con,$sql) OR Die('SQL Query not possible!');
Check the SET query. I think something is missing. I have changed it to what I think was your aim.
The connection should be established like this:
$Hostname = "Your host name mostly it is ("localhost")";
$User = "Your Database user name default is (root)"//check this in configuration files
$Password = "Your database password default is ("")"//if you change it put the same other again check in config file
$DBName = "this your dataabse name"//that you use while making database
$con = new mysqli($Hostname, $User , $PasswordP , $DBName);
$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";
In this query:
put categorias in magic quotes(`) and column names also
For your next query do this:
$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";
Change to:
$sql= "SET foreign_key_checks = 0; DELETE FROM `users` WHERE `username` = 'Hola'";

Categories