https://developers.google.com/maps/documentation/javascript/mysql-to-maps
Features deprecated PHP for connecting to a MySQL database, i.e. using mysql instead of mysqli or pdo.
I tried following the tutorial to display markers with set names/lat/lng from a mySQL database, but found that the PHP was deprecated and simply replacing 'mysql' with 'mysqli' gave me a nearly empty XML document.
Here is my PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "pins_db";
$database = "root-pins_db";
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $dbname);
// Select all the rows in the markers table
$query = "SELECT * FROM pins_table WHERE 1";
$result = mysqli_query($query);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'name="' . $row['name'] . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
The resulting XML document only contains:
<markers/>
I'm running PHP 7.0.15 and MySQL 5.6.35 on MAMP.
The database info:
user/pw: root/root
database: pins_db
table: pins_table
The table:
1 id int(11)
2 datetime datetime
3 lat float(10,6)
4 lng float(10,6)
5 name text
Am I missing something here? I'm fairly new to PHP and very new to mySQL; I can't find an answer to this anywhere else online.
Turn on php error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/function.ini-set.php
Test for successful execution of the query execution.
$result = mysqli_query($connection,$query);
if(!$result) {
// error returned
die('error#002: '.mysqli_error($connection));
}
http://php.net/manual/en/mysqli.error.php
Remove the # (at sign) before the function call, because we do not want to silence php errors:
while ( $row = #mysqli_fetch_assoc($result) ) {
^
Also check for a successful connection
$connection = mysqli_connect('localhost', $username, $password);
if(!$connection) {
die('error connecting: ' . mysqli_connect_error());
}
http://php.net/manual/en/mysqli.connect-error.php
It's also possible that mysqli_select_db isn't working. For example if the database name is incorrect or doesn't exist.
http://php.net/manual/en/mysqli.select-db.php
Note that a database name could be supplied as a fourth argument on mysqli_connect.
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/
Related
I am coding a book store for a school project. We started last semester with html. This semester we are converting it to php for dynamic reasons. I modularized the code the best I can, but when I put the functional section of php in, it prevents all the following html in the php code from displaying. As far as I can tell, I have done everything correctly and can not find the issue.
This is the php call in my index.php
<aside class="lSideMenu">
<table>
<tr><td><h3>Categories</h3></td></tr>
<?php
include_once 'getGenres.php';
popGenres();
?>
<tr><td>Humor</td></tr>
</table>
</aside>
I left the table row after the call for testing and does not show, but when I look at the debug window the table and aside close tags are there. there is other code after that that does not populate as well and my css breaks.
This is the php funtions of getGenres.php
<?php
/**
* Created by PhpStorm.
* User: PoeDawg
* Date: 4/5/2017
* Time: 2:51 PM
*/
function db_connect(){
$host = 'localhost';
$uname = 'root';
$pass = 'root';
$link = new mysqli($host, $uname, $pass);
if (!$link) {
die('Could not connect: ' . mysqli_error($link));
}
return $link;
}
function popGenres(){
$link = db_connect();
$dbname = 'volga_db';
$db_selected = mysqli_select_db($link, $dbname);
if (!$db_selected) {
die('Could not connect to database: ' . mysqli_error($link));
}
$query = 'SELECT * FROM tblgenres';
$result = mysqli_query($link, $query);
if ( $result ) {
while ( $row = $result->fetch_assoc() ) {
echo "<tr><td>" . $row['genreName'] . "</td></tr>";
}
$result->close();
}
mysqli_close()($link);
}
It does what it is supposed to in that it populates the list of genres, it just prevents the content below the function call from loading in the browser. If i take the php call section out, the page works as it should. Any help would be greatly appreciated.
Your last line of code should be like below,
mysqli_close($link);
I've looked all over here. Please be patient as I am new to php and mysql.
I got WAMPP installed & seems to be working OK. I created a simple "test" database from phpMyAdmin and "firsttable" in that. I can do a simple connect using example from w3schools, but trying to select & display data I entered only throws back errors.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Connect
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT reference, firstname, lastname, room FROM firsttable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["reference"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "room:" . $row["room"]. "<br>";
}
} else {
echo "0 results";
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
First off, I get a parse error on line 17. The one that reads:
if ($result->num_rows > 0) {
The error says: Trying to get property of non-object.
I tried wrapping the whole php code in tags and saving it as html, but then it appeared that no row data was ever found.
I am able to use very simple code that connects successfully. I can confirm the database is in there, so is the table, and the contents I added to it.
Please, what am I doing wrong?
You need to specify the database when you connect:
$database = 'test';
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (test in this case). MySQL doesn't know which database your table resides in without you telling it.
In addition, you should always include error checking for your database connection (you have two of these, you don't need the last one) as well as any queries. Sans this, you can check your error logs for more information when something fails.
I'm trying to connect my PHP webpage with my MS SQL database. I've got this code from the internet, but have tried others. All seems to come back with a problem with "mssql_connect".
I have tried (I think) everything and can not find out why it won't work.
My code is:
<?php
$myServer = 'SQL5008.Smarterasp.net,1433';
$myUser = '*****';
$myPass = '*****';
$myDB = '*****';
//connection to the database
$dbhandle = mssql_connect($myServer, $myuser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT id ";
$query .= "FROM tblEmployees ";
$query .= "WHERE CompanyID=3";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
Why dont you try using PDO. I use it with SQL SERVER daily. You will need the php sqlsrv extenstion though.
// instantiate the pdo object
try {
$Server = "localhost";
$User = "username";
$Pass = "password";
$Database = "mydb";
$this->conn = new PDO("sqlsrv:Server=$Server;Database=$Database", $User, $Pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//allow for SQL query errors
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Check using phpinfo() if mssql extension is loaded. According to PHP Manual:
This extension is not available anymore on Windows with PHP 5.3 or later.
If you find out mssql is unloaded, try to connect using sqlsrv extension. Here you can find a few examples http://php.net/manual/ru/function.sqlsrv-connect.php
Anyway it's a good idea to post here an error message you get from PHP.
how can I get the ChatList names from the database and populate them in strings.xml file string array tag using php and mysql
<string-array name="chatListNames">
<item>Byamukama Robinhood</item>
<item>Test name</item>
<item>Ivan</item>
<item>Mohsin Afir</item>
<item>Test</item>
</string-array>
populate them in strings.xml file string array tag using php and mysql
strings.xml are for constant(read-only) strings so it's not possible to change at runtime
So instead of adding strings in strings.xml which want to change at runtime save in SharedPreferences.
how can I get the ChatList names from the database
Use web-services for getting ChatList names from server in application then update all values in SharedPreferences.
See following good tutorial for creating web-services in PHP and accessing from Android:
How to connect Android with PHP, MySQL
It is not possible in Android, you can use a database to do that. Strings.xml is can not be modified at run time.
Use the following code and change db name and access, run it in browser and copy paste the same to XML file.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
echo "<string-array name="chatListNames">";
while($row = $result->fetch_assoc()) {
echo "<item>" . $row["id"]. "</item><br>";
}
echo "</string-array>";
} else {
echo "0 results";
}
$conn->close();
?>
I'm trying to output the results of a simple query using a Google Cloud SQL with a mysqli connection. I've properly set up a Cloud SQL instance and imported a SQL database. However, when I run my app, it seems to connect to the database - no errors are triggered there - but the logs show the following:
PHP Fatal error: Wrong SQL: SELECT * FROM students Error: No database selected in /base/data/home/apps/s~db-php-001/1.371796924944999585/main.php on line 18
Here's my code:
$conn = new mysqli(null,"root","",null,null,"/cloudsql/db-php-001:db-instance-001");
// check connection
if($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
$sql='SELECT * FROM students';
$rs=$conn->query($sql);
if($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
$rows_returned = $rs->num_rows;
}
Obviously, I'm triggering that error, but I'm can't figure out why. There is definitely a table named students in the database.
Anyone have any ideas?
Thanks!! Joe
You've set your database name to null. A connection is made like so:
$mysqli = new mysqli("localhost", "user", "password", "database");
The mysqli constructor can take in the following parameters (in this order):
$mysqli = mysqli($hostname, $username, $password, $database, $port, $socket);
In your case, you've set the parameters to be:
$hostname = null; //Defaults to mysqli.default_host
$username = "root";
$password = "";
$database = null; //Defaults to ""
$port = null; //Defaults to mysqli.default_port
$socket = "/cloudsql/db-php-001:db-instance-001";
To clarify, you can pass null for the database name. In the query you'd need to use the fully qualified table name (<database>.Students in your case). Or you can use the mysqli_select_db() function to select the database to use.