I'm trying to fetch data from the database but I am getting this error.
Fatal error: Uncaught Error: Call to undefined function
mysql_select_db() in E:\xamp\htdocs\PoliceApp\News\fetch.php:10 Stack
trace: #0 {main} thrown in E:\xamp\htdocs\PoliceApp\News\fetch.php on
line 10
How can I make this right?
<?php
$username="root";
$password="namungoona";
$hostname = "localhost";
//connection string with database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "";
// connect with database
$selected = mysql_select_db("police",$dbhandle)
or die("Could not select examples");
//query fire
$result = mysql_query("select * from News;");
$json_response = array();
// fetch data in array format
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// Fetch data of Fname Column and store in array of row_array
$row_array['Headlines'] = $row['Headlines'];
$row_array['Details'] = $row['Details'];
$row_array['NewsPhoto'] = $row['NewsPhoto'];
//push the values in the array
array_push($json_response,$row_array);
}
//
echo json_encode($json_response);
?>
It should be mysqli_select_db($dbhandle, "police") and other mysql_* functions should be changed to their mysqli_* as well.
<?php
$username = "root";
$password = "namungoona";
$hostname = "localhost";
// enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// connection string with database
$dbhandle = mysqli_connect($hostname, $username, $password);
// connect with database
$selected = mysqli_select_db($dbhandle, "police");
// query fire
$result = mysqli_query($dbhandle, "select * from News;");
$json_response = array();
// fetch data in array format
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Fetch data of Fname Column and store in array of row_array
$row_array['Headlines'] = $row['Headlines'];
$row_array['Details'] = $row['Details'];
$row_array['NewsPhoto'] = $row['NewsPhoto'];
//push the values in the array
array_push($json_response, $row_array);
}
echo json_encode($json_response);
Related
I am trying to retrieve data from my database using PDO Fetch object and it says
Fatal error: Uncaught Error: Call to undefined method mysqli_result::execute()
what I'm doing wrong
This is what I have tried
<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "messages_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$getquery = $conn->query('select col_name from db where id = 2');
$getquery->execute();
$result = $getquery->fetch(PDO::FETCH_OBJ);
?>
<div><?= $result->col_name; ?></div>
Firstly, don't mix PDO and mysqli. Stick to one. Here's a PDO example. You first need to create a new PDO object. and connect to DB at the start
$servername = "localhost";
$dbusername = "root";
$dbpassword = "root";
$dbname = "dbname";
try{
$pdo = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,
$dbpassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
print "Error! Unable to connect: " . $e->getMessage() . "<br/>";
die();
}
$rtrv = "select col_name from db where id = 2"
$stmt = $pdo->prepare($rtrv);
//Execute.
$stmt->execute();
//Fetch.
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Do whatever you want after this
Trying to make json file from this php but its not working what im missing
// Initialize variable for database credentials
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'users';
$dblink = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
//Fetch 3 rows from actor table
$result = $dblink->query("SELECT * FROM profile");
//Initialize array variable
$dbdata = array();
//Fetch into associative array
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
//Print array in JSON format
echo json_encode($dbdata);
We are currently running into an issue when we try and get the username from a SQL database. This is our code for the query and the fetch_object()
<?php
$user = 'root';
$password = 'root';
$db = 'collabowrite';
$host = 'localhost';
$port = 3306;
$conn = new mysqli($host, $user, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $link = mysqli_init();
// $success = mysqli_real_connect(
// $link,
// $host,
// $user,
// $password,
// $db,
// $port
// );
//$sql='SELECT * FROM users WHERE username ='.$_GET['username'];
$result = $conn->query('SELECT * FROM users WHERE username ="'.$_POST['username']).'"';
if(!$result){
die($conn->error);
}
$userids = array();
while ($row = $result->fetch_object()){
$userids[] = $row->id;
}
echo $userids[0];
$conn->close();
?>
When we run this we get the error:
Fatal error: Uncaught Error: Call to a member function fetch_object() on string in /Users/brianleaf/Google Drive/htdocs/login.php:34 Stack trace: #0 {main} thrown in /Users/brianleaf/Google Drive/htdocs/login.php on line 34
We are new to PHP so any help would be appreciated.
It looks like you have the quote on the outside of that last ) on line 34 rather than on the inside.
Try, for example:
$sql='SELECT * FROM users WHERE username ="'.$_POST['username'].'"';
$result = $conn->query($sql);
or
$result = $conn->query('SELECT * FROM users WHERE username ="'.$_POST['username'].'"');
You've created a string. This is the way it should be:
$result = $conn->query('SELECT * FROM users WHERE username="' . $_POST['username'] . '"');
I'm in the process of building an iOS app that uses a webservice for data. The webservice consists of PHP, MySQL database.
I've successfully managed to JSON encode the data returned, but the code I am using only seems to encode 1 row.
I wanted to get some advice on how to encode multiple rows?
Thanks.
<?php
$username = "root";
$password = "*******";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//print "Connected to MySQL<br>";
$selected = mysql_select_db("newtest",$dbh)
or die("Could not select first_test");
$query = "SELECT * FROM MyGuests ";
$result=mysql_query($query);
echo json_encode(mysql_fetch_assoc($result));
?>
$query = "SELECT * FROM MyGuests ";
$result=mysql_query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print_r json_encode($rows);
I have webservice written in PHP that reads from the local database and output the result in JSON.
However, I am unable to output it into a JSONArray.
Here is the php script
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$response=array();
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("test",$dbhandle)
or die("Could not select test");
//execute the SQL query and return records
$result = mysql_query("SELECT name, country FROM android");
$response["infos"] = array();
while ($row = mysql_fetch_assoc($result)) {
$info = array();
$info["name"]=$row["name"];
$info["country"]=$row["country"];
print(json_encode($info));
}
//close the connection
mysql_close($dbhandle);
?>
This is the output from the webservice
{"name":"develop","country":"mru"}{"name":"fufu","country":"tutu"} {"name":"chikaka","country":"aceVentura"}
But I have been told that this is not in JSONArray.
What am I missing here?
Thank you
In your example you're echo'ing out multiple JSON strings because your output code is within a while loop. There should only be one output for the JSON string. The code below will give you a two dimensional array in JSON format.
$info = array();
while ($row = mysql_fetch_assoc($result))
{
$arr = array();
$arr["name"] = $row["name"];
$arr["country"] = $row["country"];
$info[] = $arr;
}
echo json_encode($info);