I'm trying to get my raspberry working like a webserver.
It all works, except the queries of my mysql database. The values are correct, the query is correct and my database works fine.
But when I want to get information from my database with PHP it gives an empty result.
The code:
<?php
$dbCon = new mysqli($servername, $username, $password, $database);
$Query = "SELECT * FROM `machineuptime`";
if($stmt = $dbCon->prepare($Query))
{
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
$ArrayData2[] = $row;
}
}
echo "this is de first row of the array: " . $ArrayData2[0]['ID'] . ". Thats nice!";
?>
the result:
this is de first row of the array: . Thats nice!
Even when I use the query in phpmyadmin, it gives the values back, so the Query isn't wrong.
So how can I solve this issue?
Edit:
The error that I get is.
Fatal error: Call to undefined method mysqli_stmt::get_result() in /var/www/html/Stramit/index.php on line 59
you don't close the query at:
$Query = "SELECT * FROM `machineuptime`;
change it to:
$Query = "SELECT * FROM `machineuptime`";
also for the error
Fatal error: Call to undefined method mysqli_stmt::get_result() in /var/www/html/Stramit/index.php on line 59
look at this answer.
Look at the user notes for http://php.net/manual/en/mysqli-stmt.get-result.php
It requires mysqlnd driver.Make sure you have that driver installed. If you do not have them installed use BIND_RESULT & FETCH
uncommented the extension=php_mysqli_mysqlnd.dll in php.ini; and restarted Apache2.2 and MySQL services. says it in the comment of the answer.
Here is the link to the driver: http://php.net/manual/en/book.mysqlnd.php
after you have installed it you should be able to use $stmt->get_result(); ofcourse reboot your raspberry pi.
Note: your php version must be above php 5.3.0
Related
My automation code encountered a subject-like problem when calling a particular function.
It occurs randomly during execution, not immediately after execution, and after the problem occurs, the error "mysql_num_rows() expect parameters 1 to be resource, boolean given in" occurs and normal behavior is not performed.
The OS is Ubuntu 18.04.
PHP version is 5.6.40.
Mysql version is 5.7.38.
The problematic function code.
$conn = mysql_connect("127.0.0.1","ID","PW");
if($conn ==NULL)
{
echo "<script> alert(\" Error : DB 연결 에러입니다. 불편을 드려 죄송합니다. 관리자에게 문의 하십시요\"); </script>";
return $conn;
}
mysql_select_db("mysql");
mysql_query("set session character_set_connection=utf8;");
mysql_query("set session character_set_results=utf8;");
mysql_query("set session character_set_client=utf8;");
$dbname = "test_table";
$str = "select * from $dbname where 1";
$leak_result = mysql_query($str);
$leak_num1 = mysql_num_rows($leak_result);
Please give me a solution.
This error is appearing because of the maximum number of database connection requests have made.
Your Ubuntu 18 with PHP server it's making a lot of requests.
The "PID=3629" you read it's the Process Id that generates the error.
The method you are using mysql_query it's old and deprecated.
Now PHP uses mysqli method like this
$connect = mysqli_connect( $host, $user, $pass, $DBname );
$query = 'SELECT .... '; //here your query
$result = mysqli_query($connect,$query);
Like said in comments, if there's an error mysqli_query gives back a false result
PHP OFFICIAL SITE
If you want a more complete answer on the error use the mysqli_error() function.
Good luck
I'm getting the following errors from php7 on a new Ubuntu server:
Notice: Trying to get property of non-object in ~/tLogServ.php on line 14
Warning: mysqli::query(): Couldn't fetch mysqli in ~/tLogServ.php on line 17
Fatal error: Uncaught Error: Call to a member function fetch_assoc() on null in ~/tLogServ.php:18
Stack trace:
0 {main}thrown in ~/tLogServ.php on line 18
Here's the tLogServ.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$request = json_decode( file_get_contents('php://input') );
$variable = $request->data;
// echo($variable);
$result = $conn->query("SELECT password FROM login where userID LIKE 'technician'");
$rows = $result->fetch_assoc();
$passHashed = $rows['password'];
if(password_verify($variable, $passHashed)){
$loginMess = "success";
}else{
$loginMess = "fail";
}
echo json_encode($loginMess);
$conn->close();
?>
and my connection script
<?php
DEFINE ('user', '$%^*(');
DEFINE ('pass', '^*&%*');
DEFINE ('host', '1*&^*&^');
DEFINE ('DB', '^*%*(&%^');
$conn = new mysqli(host, user, pass, DB) OR die('Fail Whale ' . mysqli_connect_error());
?>
The notice will go away with input, but I'm unsure about and the warning and the fatal error it causes.
This code works without issue on Ubuntu 14 with PHP5. I've uncommented
extension=php_mysqli.dll
in php.ini. This is obviously a compatibility issue, but I'm unsure if I need to re-write my code or if it's a matter of a simple setting that I can't find.
The issue was two stupid mistakes.
1)The user I referenced in my connection was not given proper rights.
and
2) 'Host' was defined as the IP of the localhost. For whatever reason this worked on Cat connection, but not on WiFI (both had static Ip's).
For others with the same/similar issue. Verify that your php.ini mysqli extension is enabled/uncommmented. As A.L was onto the issue was with mySQL settings not PHP/Ubuntu.
This is pretty similar to this question, but I need to capture output. It seems to be basically an issue with getting the correct syntax, but using the syntax suggested in the answer didn't work for me.
I have a php application that calls a stored procedure on a SQL Server database which I developed on my Windows laptop running xampp.
My stored procedure looks like this:
CREATE Procedure [dbo].[check_for_barcode] #Sample_Id nvarchar(20), #Barcode_In_table int OUTPUT
AS
SELECT #Barcode_In_table = count(*) FROM dbo.sample_info
WHERE sample_name = #Sample_Id
RETURN #Barcode_In_table
GO
The php code that works on Windows xampp looks like this:
// use a sample Id known to be present in database
$sample_yes = '11335577';
// use a sample Id known NOT to be in database
$sample_no = '0011223344';
$host = 'hostIP';
$db = 'db';
$user = 'user';
$password = 'pw';
try {
$conn = new PDO("sqlsrv:Server=$host;Database=$db", $user, $password);
$sql = "{CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)}";
$stmt = $conn->prepare($sql);
$stmt-> bindParam('barcode', $sample_yes, PDO::PARAM_STR);
$stmt->bindParam('isInDatabase', $isInDatabase, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 4);
$stmt->execute();
echo 'for sample id 11335577 $isInDatabase = '.$isInDatabase."\n";
$stmt-> bindParam(':barcode', $sample_no);
$stmt->execute();
echo 'for sample id 0011223344 $isInDatabase = '.$isInDatabase;
$stmt->closeCursor();
}
catch (Exception $e) {
echo $e->getMessage();
}
The output is:
for sample id 11335577 $isInDatabase = 1
for sample id 0011223344 $isInDatabase = 0
Since moving it to the Linux (Ubuntu 4.24) server where it will be deployed, it no longer works.
I installed the dblib drivers as described here.
I adapted the code to as required to use the dblib driver so that it would run without errors. This only involved changing the first two lines inside the try statement.
I have changed the first line to:
...
$conn = new PDO("dblib:host=$host;dbname=$db", $user, $password);
...
I have tried all of the following for the second line:
$sql = "{CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)}";
$sql = "{CALL dbo.check_for_barcode #Sample_Id=:barcode, #Barcode_In_table=:isInDatabase}";
$sql = "CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)";
$sql = "CALL dbo.check_for_barcode :barcode, :isInDatabase";
As well as all of the above using EXEC instead of CALL
The output in all cases is:
for sample id 11335577 $isInDatabase =
for sample id 0011223344 $isInDatabase =
If I add ...
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($result);
}
... this generates no output.
There are no error message at all, so I can't see where the problem is. I would welcome any suggestions on what to try next.
It's been a while since I had to do this, but I used ODBC to connect to SQL Server from PHP running on a linux box.
Drivers - http://msdn.microsoft.com/en-gb/data/ff657782.aspx
ODBC docs - http://www.php.net/manual/en/ref.pdo-odbc.connection.php
EDIT: The ODBC drivers above are for Windows, the Linux installation instructions and drivers can be found at www.php.net/pdo_odbc
I have a database in SQL Server, and I want to populate the XML document from my database, using PHP. But when I open the populate.php file I have the error below...
Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\populate.php on line 6
My php file is like below:
<?php
$server = "USER-PC\SQLEXPRESS";
$options = array("UID"=>"", "PWD"=>"", "Database"=>"RESTORANTET");
$conn = sqlsrv_connect($server, $options);
$sql = "SELECT Emri_Pjates, Cmimi, Pershkrimi FROM dbo.MENU FOR XML PATH";
$stmt = sqlsrv_query($conn, $sql);
sqlsrv_fetch($stmt);
$xml = sqlsrv_get_field($stmt, 0);
?>
Please can you help me? Why sqlsrv_connect() is unidentified? I am not sure about the username and password.. When I enter the SQL Server ,I enter with Windows Authentification, and I dont use any username or password...That is why I have leave UID amd PWD empty here..Am I wrong?
It seems that your php does not have the neccessary driver to connect to sql server.
download sql server driver for php here
I have a test db on godaddy, I have had a similar issue before but with the help of stackoverflow it was solved.. So I have actually used the same solution however now I am having a host of errors when I try to load my php file from the browser.
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/97/8603797/html/countryQuery.php on line 14
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/97/8603797/html/countryQuery.php on line 14
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/97/8603797/html/countryQuery.php on line 16
Warning: mysql_close(): no MySQL-Link resource supplied in /home/content/97/8603797/html/countryQuery.php on line 18
Database Output
I have read what the problem is and some of the things I have read suggest I am not connecting to the DB correctly.. but I am only doing what I have read/been told and dont really know what else I can do to solve the issue I am having...
this is my php code, if anyone has and suggestions or solutions I would greatly appreciate hearing from you.
<?
$hostname = 'mydatabasename.db.6666666.hostedresource.com';
$database = 'mydatabasename';
$username = 'myusername';
$password = 'secretsecret';
// establish a connection
$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$query="SELECT * FROM `Countries`";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$country=mysql_result($result);
echo "<b>$country<br>";
$i++;
}
?>
the out put should just be a list of the country names I have in the countrys table of my database.. which has values in it.
You are mixing PDO and mysql_* functions.
Try something like this:
$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM `Countries`";
$numRows=$dbh->exec($sql);
echo "There were $numRows selected with:<b> $sql </b><br><br>";
$db=null;
unset($db);
When it comes to getting data from the PDO connection, I normally do something like this using the FETCH_INTO option:
$stmt = $db->query($sql);
$obj = $stmt->setFetchMode(PDO::FETCH_INTO, new userStructure);
// I do normally have a class matching the row I wan:
foreach($stmt as $userStructure)
{
$someObject->year=$userStructure->year;
$someObject->month=$userStructure->month;
unset($stmt);
}
Make sure that you include the port as well when using a PDO connection. This is looking like you are doing this in a hosted environment. Check the installation guide regarding the port used.