How To Get a Single Number From a Database in PHP [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I need to echo the number 1 or 0 whatever the database has in that cell. I cant seem to get it to work. What am I doing wrong people of the internet?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$bob = mysql_query($sql);
echo $bob;
?>

You can not use mysql and mysqli together. So you can try the following code (using mysqli only, since mysql is deprecated) :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testfp";
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT hookup FROM testfp WHERE name = 100100" ;
$result = $conn->query($sql);
if($result->num_rows>0)
{
while($row = $result->fetch_assoc()) {
echo $row["hookup"];
}
}
else
{
echo "No results found!";
}
?>

Related

How to use PHP SQL with where Clause? [duplicate]

This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I want to select an email from the DB which needs to return the related Account_ID
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "connected accounts details";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT acc_id FROM users
WHERE
email = $user_email";
$result = $conn->query($sql);
echo "$result";
$conn->close();
This code returns nothing, just blank.
While using the same connection/db data is being inserted in db successfully.
Try this code
// Php Sql Select From DB....
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "connected accounts details";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `acc_id` FROM `users` WHERE `email` = ?";
$result->$conn->prepare($sql);
$result->bind_param('s',$user_email);
$result->execute();
$data = $result->get_result();
// Fetch all
$data->fetch_all(MYSQLI_ASSOC);
print_r($data);
$mysqli -> close();
// #link http://php.net/manual/en/mysqli-result.fetch-all.php
$sql = "SELECT acc_id FROM users
WHERE
email LIKE '%$user_email%' ";
$result = mysqli_query($conn, $sql);
$row = $result->fetch_assoc();
$id = $row['acc_id'];
echo "$id";
$conn->close();
Got the wanted Record, trying this way.
Thanks everyone answering on 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

Echo a mysql column by php as an array [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
i want to fetch a mysql table named "my_table" column named "Email" contents as an array by php , so this is my code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysql_query("SELECT * FROM my_table");
if ($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[] = $row['Email'];
}
echo join($data, ',');
?>
but this code returns me this error :
No database selected
but i've selected my table and database ...
and i know this code have some problems as mixing mysql and mysqli content but i dont know how to fix it i just want that array echo , if this code need to be fixed just guid me ,
how to solve this problem ? thanks in advance
Thanks to #Martin
my problem has solved i just changed the code by this way :
<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "my_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}
$result = mysqli_query($conn, "SELECT * FROM my_table");
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$data = array();
while ($row = mysqli_fetch_array($result))
{
$data[] = $row['Email'];
}
echo join($data, ',')
?>
You are conencting to a database here called "my_table":
$dbname = "my_table";
And then, in your SQL statement, you try connecting to a table called the same:
$result = mysql_query("SELECT * FROM my_table");
Are you sure this is the correct name for your database?
On PHPMyAdmin you can click "Databases" to view the Database names and then, when clicking on the db, it will give you a list of tables:
Image file of getting database views from tables in PHPMyAdmin

Mysqli to variable in PHP

I've been a few days working on my project, but just stay stuck a specific part.
My problem is that i like to get a value from the database (mysqli)
But i always receive a 0. So, no value.
This is my code:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_idSQL = "SELECT user_id FROM ss_devices WHERE serialkey = '$serialkey'";
$user_idResult = $conn->query($user_idSQL);
$user_id = $user_idResult;
The script should see what the userid, it is in equality with the specified serial number.
But when i take a serial number like: FJRI433. I always get a 0. But in the database has this serial number user_id: 3.
I hope someone can help me out whith this problem.
Thanks.
You are executing the query but not fetching any results.
The manual has plenty of examples of using mysqli :
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
//Do stuff with the next row.
}
}
Try this perhaps...
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ( mysqli_connect_errno() ) {
die("Connection failed: " . mysqli_connect_error() );
}
$user_idSQL = "SELECT `user_id` FROM `ss_devices` WHERE `serialkey` = '$serialkey'";
if( $user_idResult = mysqli_query( $conn, $user_idSQL ) ) {
while ($row = mysqli_fetch_assoc($user_idResult)) {
echo $row['user_id'].'<br />';
}
}
mysqli_close( $conn );

why its not working to me [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
i want to get the backgroundColor of the Name headerBackgroud and its not printing anything, can you please help me?
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "DB#1";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT backgroundColor FROM background WHERE Name = 'headerBackground'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo $row['backgroundColor'];
}
You mix up mysqli and mysql api, so just use PDO uniformly as given:
$dbc = new PDO('mysql:host=localhost;dbname='.$database, $user, $password);
$sql = "SELECT backgroundColor FROM background WHERE Name = :n";
$stmt = $dbc->prepare($sql);
$stmt->bindParam(':n', "headerBackground");
$stmt->execute();
if($stmt->rowCount() > 0){
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$headerB = $data['headerBackground'];
}

Categories