Calling MySQL Stored Procedure through PHP - php

I've installed XAMPP and have been attempting to interrogate my database using PHP although I keep getting the same error.
<?php
$servername = "localhost";
$username = "root";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform queries
$result = mysqli_query($conn,
"use edgeserver; call ShowAll") or die("Query fail: " . mysqli_error());
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
$conn->close();
?>
However this presents me with the following error:
Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\amit\ShowAll.php on line 16
Query fail:

Mysql_error requires db connection to be passed. Try below:
mysqli_error($conn);

Thanks, it ended up being a combination of what you both said:
<?php
// Create connection
$conn = new mysqli('localhost', 'root', 'secret', 'edgeserver');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform queries
$result = mysqli_query($conn,
"call ShowAll") or die("Query fail: " . mysqli_error($conn));
//loop the result set
while ($row = mysqli_fetch_array($result)){
echo $row[0] . " - " . + $row[1];
}
$conn->close();
Great response, hopefully this will be the base to me developing some good code.

I think the problem is from use edgeserver; call ShowAll.
use edgeserver, where edgeserver is the database that you want to use.
call ShowAll - what you try to accomplish with this?
Your new mysqli must be like this:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
source: http://php.net/manual/ro/mysqli.construct.php
And on mysqli_query you must do the queries like "Select * from table".

Related

How to connect html to Azure database?

I'm trying to connect my Azure Sql storeage and html to show everything i have. but i'm having some trouble. i researched w3school and other resources but i still don't know what is wrong?
so i use Notepad++ and save it as html and use php to establish the connection
here is the code in my notepad+++ so far:
<?php
$servername = "servername*****.mysql.database.azure.com";
$username = "loginfor****n#mysql***";
$password = "*****";
$db = "db";
// Create connection
$conn = new mysqli($servername, $username, $password. $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
and this is what i got
connect_error) { die("Connection failed: " . $conn->connect_error);
i have no idea where did i went wrong. please help me if you can, thanks
I'm not 100% sure as I'm not a PHP dev, but Microsoft have the following on their Azure documentation (here):
<?php
$serverName = "your_server.database.windows.net"; // update me
$connectionOptions = array(
"Database" => "your_database", // update me
"Uid" => "your_username", // update me
"PWD" => "your_password" // update me
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
$tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p
ON pc.productcategoryid = p.productcategoryid";
$getResults= sqlsrv_query($conn, $tsql);
echo ("Reading data from table" . PHP_EOL);
if ($getResults == FALSE)
echo (sqlsrv_errors());
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
?>
My guess is that your PHP is malformed and you're getting a render of the code rather than it actually properly executing - be sure to read the documentation.
i've also tried this code too
<?php
$host = '****.mysql.database.azure.com';
$username = '****#mysql****';
$password = '****';
$db_name = '*****';
//Establishes the connection
$conn = mysqli_init();
printf("hello")
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
printf("sory");
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
// Run the create table query
if (mysqli_query($conn, '
select * from table1;
')) {
printf("Table created\n");
}
//Close the connection
mysqli_close($conn);
?>
it returns a blank page, i feel that some how it just skip all of my php code

Select and print out Data from MySQL with php, not working

I have a table ("module databas") in a database in MySQL and I need to print out that table (very simple, two rows "Fnamn" and "Enamn" with names in it) on the server by writing a php script and using MySQL. Problem is : it doesn't work. The html part of the document (.php) works perfectly fine (the h1 appears on the screen) but I get nothing else. What could be the problem ?
Tried a few different ways to do it, even by copy/pasting from w3 schools (https://www.w3schools.com/php/php_mysql_select.asp) and changing a few variables, but nothing (had a "0 results" with this W3 one, and now nothing with the new one).
<h1>Script modul</h1>
<?php
$servername = "localhost";
$username = "antony";
$password = "thepassword";
$dbname = "antony";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM moduledatabas";
$result = mysqli_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysqli_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysqli_fetch_assoc($result)) {
echo $row['Fnamn'];
echo $row['Enamn'];
}
mysqli_free_result($result);
You have not pass $conn in your mysqli_query(),just change your code like below :
$query = "SELECT * FROM moduledatabas";
$result = mysqli_query($conn,$query);
For more info refer mysqli_query

Show all tables within given MySQL database

I'm trying to show all tables within a given MySQL database with php.
I'm very new to all this though and can't find a solution for it. Keeps giving an error 'no found file or directory'.
Anyone who can point out my mistakes here please?
Much appreciated!
<?php include "../inc/dbinfo.inc"; ?>
<html>
<body>
<h2>LIST TABLES FROM DATABASE</h2>
<?php
// Create connection
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
// Check connection
if ($conn->connect_error) {
die("Connection with the database failed: </br>" . $conn->connect_error);
}
echo "Connection established with the database! </br>";
// SQL to show tables
$sql = "SHOW TABLES FROM paperlessdb";
$result = mysql_query($sql);
if (!$result) {
echo "Database error, could not list tables.\n</br>";
echo 'MySQL error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "- {$row[0]}\n </br>";
}
mysql_free_result($result);
?>
First make up your mind, either use mysqli procedural or object orientated. Not a combination of both because its confusing. To avoid that all together use pdo instead.
Now properly connect to the database, you can select the database when connecting to it automatically:
const DB_DATABASE = 'paperlessdb';
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if ($conn->connect_error) {
die("Connection with the database failed: </br>" . $conn->connect_error);
}
if($result = $conn->query('SHOW TABLES')){
while($row = $conn->fetch_array($result)){
$tables[] = $row[0];
}
}
print_r($tables);
Use below query,
$sql = "SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'paperlessdb'";
We are fetching the data from information_schema db which stores the meta data about our database.
You are using mysqli to connect to the database but use the depreciated mysql to query the database.
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)){}
mysql_free_result($result);
You should use mysqli_query() and mysqli_fetch_array() instead.
It'a a bit more complex but mysql is decrecated and remove as PHP 7 so no choice to jump ahead. Check out PDO ass well. I personally go for mysqli but most say pdo is more intuitive.
It should look more something like:
$result = mysqli_query($conn,$sql);
if(!$result){
die('MySQL error: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_row($result)) {
echo "- {$row[0]}\n </br>";
}

how to convert mysqli_set_charset($conn,"utf8") into PDO format in MYSQL?

I trying to solve the UTF8 problem. So far i manage to test out and it works in mysqli connection.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tesddddt";
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_set_charset($conn,"utf8");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM customer";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["uid"]. " - Name: " . $row["username"]. " " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
I manage to Insert and Select data with characters shown in it's language format correctly with the above coding.
However, when I try to do it in PDO , it shows 1 error when I insert data.
public function __construct(){
try {
$conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name, $this->db_pass);
mysqli_set_charset($conn,"utf8"); // <- added here
$this->conn=$conn;
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
I got this error
Warning: mysqli_set_charset() expects parameter 1 to be mysqli, object given in...
My php project is using PDO and hence need to get this work in PDO format. Anyone know how to settle this?
You try to connect via PDO and then change the charset via mysqli, without having a mysqli connection, that is what's causing the warning.
In PDO the charset usually is being specified within the connection string, like this:
$conn = new PDO("mysql:host=yourhost;dbname=yourdbname;charset=utf8");

How to write PHP to Mysql page

Currently I just done HTML and I trying writing PHP Script on connection Mysql, But all web site on google is confuse, because now I need specify write connection by used PHP Only,
Please could you help explain step by step on code from HTML to PHP and write PHP to MySql, Thank you very much.
There is many docs about this, but you can start from here:
http://www.w3schools.com/php/php_mysql_select.asp
And here is the example code, what i suggest you:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>

Categories