I'm trying to host this php file on my website to connect to a MySQL database.
<?php
// Create connection
$con=mysqli_connect("localhost","username","pass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
And when I try to validate the code using http://writecodeonline.com/php/ it says
Fatal error: Call to undefined function mysqli_connect() on line 2
Searching around said I needed to change the php.ini file but I don't even have one hosted on my site. Is there something wrong with the code?
When I try to access it at www.mydomain.com/service.php it says file not found error... but it's definitely there. I'm working with this tutorial - http://codewithchris.com/iphone-app-connect-to-mysql-database/
You need to install the php-mysql dependency on your server.
yum install php-mysql -y
or your equivalent on your os.
Related
I am trying to fetch data from a table in oracle database using php 7. I am using OCIFetchInto but when i look to the result array, i find it empty, although it shouldn't be actually empty. knowing that the connection to the database is successful and the table in the database is not empty.
I have debugged the code, and i am getting Resource #4 from the execute function, I have changed the execute function but i still get the same result.
i also changed the data[0] to data['columnname'] but i still cannot retreive it.
Thanks in advance
The code below:
require_once("../include_tse/class_ora_db.php");
$oracle_db = new ora_database("abc");
$error="";
if ($oracle_db->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$Wip=$_REQUEST['rechts'];
echo $Wip;
$sql_query="SELECT Om_Wip_Entity_Name,Quantity,Country_Description FROM abc_print WHERE Om_Wip_Entity_Name= '$Wip'";
$cursor = $oracle_db->execute_sql($sql_query);
$counter=0;
while (OCIFetchInto ($cursor,$row))
{
echo "2222222";
$data[]=$row;
$Wip= $data['0'];
$Quantity= $data['1'];
$Country= $data['2'];
$counter ++;
}
echo "row.$Quantity";
As of PHP 5.4 the ocifetchinto alias has been deprecated (http://php.net/manual/en/function.ocifetchinto.php)
You should change it to something like...
while ($row = oci_fetch_assoc ($cursor))
{
$Wip= $row['Om_Wip_Entity_Name'];
$Quantity= $row['Quantity'];
$Country= $row['Country_Description'];
$data[]=$row;
$counter ++;
}
I'm a bit confused as to how your using the data, but this should give a better idea of how to get it working.
i´m pretty new to php and i have encountered a problem with no idea where it comes from.
I want to connect to an sql-db via php in order to access this via iOS. But the first step fails allready :(
This is a screenshot with login data from my provider:
Then i uploaded this php to the website:
<?php
// Create connection
$con=mysqli_connect("db559233526.db.1and1.com","dbo559233526","Correct Password","db559233526");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table ‚Cards‘
$sql = "SELECT * FROM Cards";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row=$result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
You con see the result here:
http://sektor3d.de/service.php
Now i wonder, why the last half of the code is shown as text? And why is the "break" at that point?
Thanks for help or any idea!
Take a look at your page source, your server seems to not be able to run PHP. If you're convinced it can, add this code to your .htaccess file:
AddType application/x-httpd-php .php
Check if your server support PHP
Create a file (in any text editor) and inside the file type:
<?php
phpinfo();
?>
Save it as info.php
Upload it to your server, and then, in your web browser, navigate to that file eg. http://sektor3d.de/info.php
This question already has answers here:
php warning: mysqli_close() expects parameter 1 to be mysqli
(2 answers)
Closed 2 years ago.
I want to apologize in advance if this has been answered. But I can not seem to find a solution that works for my problem.
I have been staring at my code for about 2 hours now and I can not seem to figure out why I am getting this error. Below is my PHP code. I am using a MYSQL DB that is hosted on a GoDaddy server.
// Create connection
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM location";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
I am getting my error at mysqli_close($result);
you can view via http://www.iqrleads.co/service.php any help will be greatly appreciated my mind is drawing a blank
$result is not a mysqli connection object in your code.
You can remove mysqli_close($result); from your code, as it's not needed.
mysqli_close($con); should suffice for you.
You can also use $con->close(); if you're so inclined.
See the reference for mysqli_close
Change
mysqli_close($result);
mysqli_close($con);
to
$con->close();
This question already has answers here:
php warning: mysqli_close() expects parameter 1 to be mysqli
(2 answers)
Closed 1 year ago.
I have this PHP code on a WAMP server with MySQL and this error appears in a dialog box when I visit the site. How can I fix this?
<?php
// Create connection
$con = mysqli_connect("localhost","userdb","userdb","apptestdb");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
?>
You don't need this line:
mysqli_close($result);
You only need to close the connection.
Can anyone see what the problem with my code is / where im going wrong?
I know i have the correct host,database,user and password.
This is the code in the php file, it should get all the details available on the players from my sql database, however if i go on the page it just gives me a white page. Im using go daddy as a host and my database is also on there.
Any ideas? thanks
<?php
$host = "abc12345"; //Your database host server
$db = "abc12345"; //Your database name
$user = "abc12345"; //Your database user
$pass = "abc12345"; //Your password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if (!$connection) {
die("Database server connection failed.");
} else {
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if (!$dbconnect) {
die("Unable to connect to the specified database!");
} else {
$query = "SELECT * FROM Player";
$resultset = mysql_query($query);
$records = array();
//Loop through all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset)) {
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
The script is all right, I checked it with a different query.
Assuming that the table Player is not empty, the error can be either with your raw sql query (as pointed by #Sharikov in comments), otherwise the error is with the way you have configured your godaddy server.
For debugging that, I would suggest inserting dummy print_r or echo statements before you execute the query, and going through your apache logs at /var/log/apache2/access.log.
Also make sure that you don't have any core php package missing on your server (like php5-mysql if you use mysql).