Database connect in PHP 7 [duplicate] - php

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
but i do not understand what i am doing wrong and why it is not working ?
Seems like it connects with DB, but it wont update DB table.
My PHP code
<?php
$host = 'localhost';
$db_name = 'db_name';
$db_user = 'user';
$db_password = 'password';
$con = mysqli_connect($host, $db_user, $db_password, $db_name);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
function _VoteReward($custom)
{
$sql = "UPDATE `users` SET `gold` = `gold` + 50000 WHERE `id` = '".$custom."' ";
mysqli_query($con, $sql);
}
$custom = $_POST["custom"];
$key = $_POST["key"];
$result = false;
if (($custom > 0) && ($key == 'key'))
{
$result = true;
_VoteReward($custom);
}
mysqli_close($con);
?>

The above code does actually produce a connection to the database. However, the resulting connection does need to be checked for errors. Typically by the following:
if(!$con)
{ // creation of the connection object failed
die("connection object not created: ".mysqli_error($con));
}
if (mysqli_connect_errno())
{ // creation of the connection object has some other error
die("Connect failed: ".mysqli_connect_errno()." : ". mysqli_connect_error());
}

Related

error in parsing parameter using php [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
This code showing the output as :
string(59) "{"status":{"message":"error parsing
parameter","value":14}}"
but when instead of "file_get_contents" only "echo" is used, it shows correct url as output : http://ws.geonames.org/countryCodeJSON?lat=20.992&lng=73.314&username=****
what is going wrong here ?
<?php
$servername = "localhost";
$username = "*******";
$password = "*************";
$dbname = "id1116502_kk";
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//remove limit 1 is you want multiple data.
$sql = "SELECT degree_n, minute_n,degree_e, minute_e FROM coordinates ORDER BY id DESC limit 1 ";
$result = $conn->query($sql);
$deg_e = "";
$min_e = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$deg_n = $row["degree_n"];
$min_n = $row["minute_n"];
$deg_e = $row["degree_e"];
$min_e = $row["minute_e"];
$url = file_get_contents('http://ws.geonames.org/countryCodeJSON?lat=$deg_n.$min_n&lng=$deg_e.$min_e&username=****');
var_dump($url);
}
} else {
echo "0 results";
}
$conn->close();
?>
you are using single quotes in your file_get_contents
// change this to double quotes
$url = file_get_contents('http://ws.geonames.org/countryCodeJSON?lat=$deg_n.$min_n&lng=$deg_e.$min_e&username=krunal123');
for more info check this post

SQL database connection in PHP successful, but I can't query it [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$tablename = "mapcoords";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo "Failure";
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
echo "ok";
}
$conn->close();
?>
Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:
<?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 (lat, lng) FROM mapcoords";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["lat"]. " " . $row["lng"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
The problem is that this query:
SELECT (lat, lng) FROM mapcoords
returns the folloowing error:
[21000][1241] Operand should contain 1 column(s)
You have to change the query to
SELECT lat, lng FROM mapcoords

Truncate table mysql php providing error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am receiving this error "Parse error: syntax error, unexpected 'TABLE' (T_STRING)" when trying to truncate a table before more data is entered.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "members";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
TRUNCATE TABLE users;
{
header ("Location: order.php");
exit;
}
mysqli_close($conn);
?>
You can't just put MySQL queries in plain text in PHP. Write it as a string inside mysqli_query() along with your current open connection as shown below:
mysqli_query($conn, "TRUNCATE TABLE `users`");
Your entire code should be:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "members";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else {
mysqli_query($conn, "TRUNCATE TABLE `users`");
header ("Location: order.php");
exit;
}
mysqli_close($conn);
?>

Getting error: Object of class mysqli_result could not be converted to string [duplicate]

This question already has an answer here:
How to use mysqli_query() in PHP?
(1 answer)
Closed 6 years ago.
My code is very messy, but I am trying to make a forum on my website.
<?php
$conn = mysqli_connect("localhost", "root", "");
if($conn->connect_error){
die("Connection Failed");
}
mysqli_select_db($conn, 'forum');
$get= "select * from forum";
$runq = mysqli_query($conn, $get);
while($fetch_value=mysqli_fetch_array($runq)){
$get_usr_name=$fetch_value['username'];
$get_body = $fetch_value['info'];
}
$result = $conn->query($get);
if(is_null($get_body)){
}
else{
echo "<div id='inputform1'>";
for($row = 1; $row < $result->num_rows; $row++){
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "forum";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()){
die("database connection failed: ") .
mysqli_connect_errno() .
" (" . mysqli_connect_errno() . ")";
}
$result1 = mysqli_query($connection, "select info from forum where id='$row';");
$result = mysqli_query($connection, "select username from forum where id='$row';");
echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";
}
echo "</div>";
}
?>
I cannot seem to post the username and password; it keeps saying:
Object of class mysqli_result could not be converted to string
I have tried many different "tactics" but none of them seem to work. Can someone help me?
The returned value of a mysqli query is a result object. It's not just a set of rows; it also contains information on the query, success or failure, error messages, and other things. You need to use the result object's methods explicitly to access the rows returned by your query.
I think the problem is from this line
echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";
The $result and $result1 are not strings. You need to loop it up and then access the records.

No database selected help me solve this [duplicate]

This question already has answers here:
PHP "No Database Selected"
(2 answers)
Closed 8 years ago.
hey everyone what's wrong with this :( i get no database selected .. what seems to be the problem ? damn i can't get this right.
<?php
require_once('db.php');
function getLanguage() {
global $db;
global $conn;
$sql = "SELECT * FROM books.languages ORDER BY name ASC";
$db = mysql_connect($hostname, $username, $password);
$rs = mysql_query($sql, $db) or die(mysql_error());
$rows = mysql_fetch_assoc($rs);
$tot_rows = mysql_num_rows($rs);
if($tot_rows > 0){
?>
Problem is no database is selected (didn't I see that in your question ?)
Where is your call to mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )?
It should be after the connect.
$db = mysql_connect($hostname, $username, $password);
mysql_select_db("your_database_name");
$rs = mysql_query($sql, $db) or die(mysql_error());
(obviously you should have error checking too...)
Maybe something like the following will help? I am using MySQLi as MySQL is deprecated.
db.php
<?php
// CONNECT TO THE DATABASE
$DB_NAME = 'database';
$DB_HOST = 'host';
$DB_USER = 'username';
$DB_PASS = 'password';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
Then use the following to query your database.
<?php
require_once('db.php');
$sql = "SELECT * FROM tablename";
$rs = $mysqli->query($sql) or die($mysqli->error.__LINE__);
if($rs->num_rows > 0) {
while($row = $rs->fetch_assoc()) {
//Do Something
}
}
else {
echo 'NO RESULTS';
}
mysqli_close($mysqli);
?>

Categories