Get value from specific MySql row - php

On my website, a user's background URL is stored with their data in a database. I need to get this value and store it in a variable.
However, I am having trouble doing so, the code I have right now gives me an error but doesnt tell me what the error is.
My code:
<?php
error_reporting(E_ALL);
include 'config.php';
$pin = $_COOKIE["UID"];
// Create connection
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * from users where pin ='$pin'";
$result = $conn->query($query);
if ($conn->query($query) === TRUE) {
$bg_url = $row[bg_url];
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
echo $bg_url;
mysqli_close($conn);
?>

$query = "SELECT * from users where pin ='$pin'";
$result = $conn->query($query);
Actually yes, the John's answer is correct, you're not using the return values properly, but you are neither accesing the variables properly, for example, I don't see where $row comes from. Try this and change it for array mode if you need to. I'm not very used to mysqli_* API, because I use PDO mostly.
if ($result) {
while($row = $result->fetch_object()) {
$bg_url = $row->bg_url;
}
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
echo $bg_url;

Related

Why does mysqli() not recognize my database name?

I'm new to PHP and MySQL. While learning, I got this error that says my database is unkown.
I have already made this database.
and I have already made the table 'todos'
Here is my PHP
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$statement = $conn->prepare('select * from todos');
$statement->execute();
var_dump($statement->fetchAll());
require 'index.view.php';
the PHP file is named 'Index.test.php'
but when I try to access localhost/Index.test.php on my browser
it returns this
Could you tell me why I am getting the error? Appreciate the help!
try to use this select:
<?php
require 'functions.php';
$conn = new mysqli('localhost','root','', 'mytodo');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//select query
$sql = "SELECT * FROM todos";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
echo $row['your column name'];
}
//free result set
mysqli_free_result($result);
} else {
echo "no records found";
}
}
else {
echo "error: could not connect" . mysqli_error($conn);
}
require 'index.view.php';
?>

Select SQL query is not working in PHP

I am having trouble with an SQL query that I have inserted into a piece of PHP code to retrieve some data. The query itself works perfectly within SQL. I am using the following PHP script.
I have the following objectives:
Connect to the existing database. This part works well.
Get data from the column 'Brand' of the table 'Transport' in $sql. This part is not working at this stage. echo ($sql) returns SELECT Brand FROM Transport WHERE Type = 'car'
Could you please let me know if you see the solution to this issue and if the remaining part of the code is correct. This is my f_sqlConnect()
function f_sqlConnect() {
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!link) {
die('Could not connect: '.mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can not use'.DB_NAME.
': '.mysql_error());
}
}
/*This function cleans up the array to protect against injection attacks */
function f_clean($array) {
return array_map('mysql_real_escape_string', $array);
}
<?php
// Create connection
$link = f_sqlConnect();
// Getting data from the column Brand of the table Transport
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"]. "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
Here is the code, without seeing your f_sqlConnect(); mothod. This method should return connection string for DB in your case. But you can use following code this must work.
<?php
$servername = "Your_db_host";
$username = "your_db_username";
$password = "your_db_password";
$dbname = "your_DB_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Brand FROM Transport WHERE Type = 'car'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Brand: " . $row["Brand"];
}
} else {
echo "0 results";
}
$conn->close();
?>
NOTE: Object oriented way of mysqli, You can use procedural way too to connect and get data.

not able to show all records in my database

i am testing my database by executing some MySQLi statements
in this case : i want to display all the records of 2 specefic rows (name,score)
i checked how to do such thing in PHP , and i did it
problem is , the page is not showing anything at all , (blank empty page)
My code :
<?php
$servername = "sql3.freesqldatabase.com";
$username = "MY USERNAME";
$password = "MY PASSWORD";
$dbname = "MY DBNAME";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,score FROM Scores");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " " . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
and , i executed the same query in phpMyAdmin Control Panel , and it worked
What have i done wrong ?
This line
$sql = "SELECT name,score FROM Scores");
Should be
$sql = "SELECT name,score FROM Scores";
This syntax error will cause an error and your environment is likely suppressing errors/warnings.

My SQL Query does not work, but returns no error

I'm trying to use a UPDATE SQL query in a PHP script but the query always returns a null or empty error. I am not sure why this is happening. Here is my code so far:
$conn1 = mysql_connect($servername, $username, $password, $dbname);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
$query = "UPDATE Products SET FilePrice=".$ourprice." WHERE FileID=".$id;
$sql = mysql_query($query);
if ($sql === TRUE) {
echo "Price Modified Successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn1->error;
}
Every time I run the script it just echoes Error:
Does anybody know why this is happening (I do not get a connection error initially either)?
The mysql API does not offer OOP syntax so your code is incorrect and will not report an error (actually, if you had error reporting set to display all errors PHP would have thrown an error). Combine that and your use of four parameterrs in mysql_connect and it looks you used mysql functions after reading a mysqli tutorial.
$conn1 = mysql_connect($servername, $username, $password);
if (!$conn1 ) {
die("Connection failed: " . $conn1->connect_error);
}
$db_selected = mysql_select_db($dbname, $conn1 );
if (!$db_selected) {
die ("Can't use foo : " . mysql_error());
}
$query = "UPDATE Products SET FilePrice=".$ourprice." WHERE FileID=".$id;
$sql = mysql_query($query);
if ($sql === TRUE) {
echo "Price Modified Successfully";
} else {
echo "Error: " . $sql . "<br>" . mysql_error();
}
This doesn't fix your UPDATE issue but will tell you what error MySQL is reporting.
(Your error is probably due to $ourprice being empty so check that next).

I need help identifying why i cant query these $_get variables into sql

<?php
$kundenr = $_GET["kundenr"];
$kundenr = mysql_real_escape_string($kundenr);
$kundenavn = $_GET["kundenavn"];
$kundenavn = mysql_real_escape_string($_GET["kundenavn"]);
$servername = "random";
$username = "random";
$password = "random";
$dbname = "random";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO customerbase (kundenr, kundenavn) VALUES('$kundenr','$kundenavn')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
echo "<br>" . $sql;
echo "<br>" . $kundenr;
echo "<br>" . $kundenavn;
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
output of the above is as follows :
New record created successfully
INSERT INTO telefoncentral (kundenr, kundenavn) VALUES('','')
i cant for the love of god figure out why it wont put in the values
can anyone see the error? im going mildy crazy here
URL line it gets is new.php?kundenr=0236&kundenavn=Peter
mysql_real_escape_string() requires a mysql_connect() connection in order to work, since you're only contacting the database later in the script, no such connection will be available. You should only call this function in the place where you are inserting data.
Furthermore you're mixing the mysql_ extension with the mysqli_ extension, you'll want to use something like $conn->escape_string() in your code.
Ideally you should not use real_escape_string at all, but instead read up on what prepared statements are.

Categories