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
Related
I'm building a mobile application that requires push notifications to be sent whenever a new post is added.
Here's the PHP notification script: (it should be added inside the loop so it will be used again for each result.
$to = (should get DEVICEID from the database).
$title = "A new request"
sendPush($to,$title,$message);
function sendPush($to,$title,$message){
//Sends noti.
}
So How do I place the above code in a while loop, and get the DEVICEID from the database and place it into $to ?
I suppose that you have an mysql connection set up and that you are using mysqli.
After you have created the connection to the database, you should have a variable holding your connection:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
So right now you are able to execute a SQL statement to your database. Your SQL statement should look similarily to this:
$sql = "SELECT DeviceID FROM Devices";
Now we can execute the statement we created to the database:
$result = mysqli_execute($con, $sql);
Now we get a result object. We can use this to get a while loop:
while ($row = mysqli_fetch_assoc($result)) {
$to = $row["DeviceID"];
// Rest of your code, this will be executed for every DeviceID in the database.
}
If you are using PDO you can read more about it here: http://php.net/manual/en/ref.pdo-mysql.php
But in general, the steps are the same.
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.
I'm trying to use jQuery-jTable to list data from a MS-Access database through PHP.
I want to change the sample provided by jTable.org : http://www.jtable.org/downloads/jTable-PHP-Samples.zip
<?php
try
{
//Open database connection
$db_connection = odbc_connect("Persist Security Info=False;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\websiagh\books\54.mdb", "ADODB.Connection", "password", SQL_CUR_USE_ODBC) or die('Cannot connect to 54');
//Getting records (listAction)
if($_GET["action"] == "list")
{
//Get records from database
$query = 'SELECT * FROM asnad WHERE (sanadno Between 10 AND 20 )';
$result = odbc_exec($db_connection , $query );
//Add all records to an array
$rows = array();
while( $row = odbc_fetch_array( $result ) )
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Records'] = $rows;
print json_encode($jTableResult);
}
...
The rest of the code is not changed.
The code ( query execution and fetching data using odbc ) works well when not using jTable. However when using it in the code as above , I get this error :
An error occured while communicating to the server.
But when I export the data from MS-Access to mysql, there is no problem at all.
For me your query execution and fetching data using odbc didn't work. This may be depending on the Window OS and the version of PHP.
The following however did work with jTables (credits to w3schools.com):
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database. Give the
database a Data Source Name (DSN).
Click OK.
After, to create a connection use the following (assuming your database is called 54.mdb):
$db_connection=odbc_connect('54','','');
if (!$db_connection) {
exit("Connection Failed: " . $db_connection);
}
The rest of the code remains as is.
Hope this helps.
I have a PHP file included but everything after the <?php include 'RandomFile.php' ?> gets thrown away, I have no clue why! I need help with this.
RandomFile.php has the contents of:
require 'cons.php';
mysql_connect($URL, $USER, $PASS, $DBN);
$strSQL = "SELECT * FROM song";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
if($rs === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $row['url'] . "<br />";
}
// Close the database connection
mysql_close();
So Basically after the <?php include 'randomfile.php' ?> is included all my html after that isn't showing up visually in my browser but if I go back and edit the file it is there???
I'm guessing that cons.php is the connection file for your database. You need to specify the full path to cons.php. From the PHP Manual for require:
require is identical to include except upon failure it will also
produce a fatal E_COMPILE_ERROR level error. In other words, it will
halt the script whereas include only emits a warning (E_WARNING) which
allows the script to continue.
So this example shows the file at the root.
require ($_SERVER['DOCUMENT_ROOT'].'/cons.php');
If it's below the DocumentRoot then you would do this:
require ($_SERVER['DOCUMENT_ROOT'].'/../cons.php');
On another note, replace all of the mysql_ functions with mysqli_. New versions of PHP will not include it as mysql_ has been deprecated.
Since you are trying a SQL query without selecting a database first, mysql fails with the error "No database selected"
The error is displayed with the code die(mysql_error());. die aborts further execution of the script. If you'd like it to continue you should just print it instead like this
if($rs === FALSE) {
print(mysql_error() . "\n"); // TODO: better error handling
}
else {
while($row = mysql_fetch_array($rs)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $row['url'] . "<br />";
}
// Close the database connection
mysql_close();
}
The key is this line:
die(mysql_error()); // TODO: better error handling
If there is an error connecting to the database (as you described in your comment: "no database selected"), the execution will "die" and no more of the page will be sent to the browser.
As the code comment says, you need better error handling, but you might conceivably temporarily change the die to an echo and then the execution will continue.
Are you missing the semicolon (;) in the include?
Btw, you could set
<?php error_reporting(E_ALL);
ini_set('display_errors', '1'); ?>
this will show you the errors in your script.
I am using wamp server 2.0 working with PHP and my database is Oracle 10g.
I am new to php and I am try to fetch data from database.
There are two columns in my table. I want show 1 column data.
After executing I get only blank page, without data from database (and yes, there is data in my database).
How can I fix this?
<?php
$c = oci_connect("system", "123", "localhost/XE");
if (!$c) {
echo "Unable to connect: " . var_dump( oci_error() );
die();
}
$s = oci_parse($c, "select col2 from tab1");
oci_execute($s, OCI_DEFAULT);
while ($row =oci_fetch($s)) {
echo $row['name']."<br>";
}
// Commit to save changes...
oci_commit($c);
// Logoff from Oracle...
oci_free_statement($s);
oci_close($c);
?>
oci_fetch copies a result into an internal buffer that you have to access e.g. using oci_result.
BTW: Had you set a higher value for error_reporting and turned on display_errors, then you would have noticed, that you tried to treat a boolean as an array when printing $row['name'].