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);
?>
Related
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.
I Cannot Check whether the username already exist in database. I gone through existing questions that were answered here. None of them solved my problem. When i executes, it displays "Cannot select username from table", which i given inside die block. Code Is given below.
<?php
$username = $_POST['user_name'];
$password = $_POST['pass_word'];
$host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "my_db";
//create connection
$conn = #new mysqli($host, $db_username, $db_password, $db_name);
if (isset($_POST["submit"]))
{
# code...
//check connection established or not
if ($conn->connect_error)
{
die("Not Connected to DB");
}
else
{
$query = "SELECT 'usernamedb' FROM 'registration' WHERE usernamedb='$username'";
$result = mysqli_query($conn, $query) or die('Cannot select username from table');
if (mysqli_num_rows($result)>0)
{
$msg.="This username already exist. try Another !!";
}
else
{
$insert = "INSERT INTO 'registration'('id', 'usernamedb', 'password') VALUES ([$username],[$password])";
$insert_result = mysqli_query($conn,$insert) or die('INSERTION ERROR');
}
}
$conn->close();
}
?>
Hope someone will answer me.
First of all you should not use those unescaped queries.
But regarding your question you have an SQL error on your queries. You quoted table name. "FROM 'registration'" should be "FROM registration".
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());
}
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!";
}
?>
I've tried searching for answers and used some tutorials, but nothing
has helped.
I can successfully establish a connection to my database.
But when I try to run this query, I get 500-Internal Server Error.
The code I am using is:
$stmt = $conn->prepare("SELECT * FROM people WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows):
$row = $stmt->fetch_assoc();
$id = $row['id'];
$position = $row['position'];
else:
die("User not found.");
endif;
How I used to do it using MySQL:
$sql = "SELECT * FROM people WHERE name = '$name'";
$result = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result) > 0):
$row = mysql_fetch_array($result);
$id = $row['id'];
$position = $row['position'];
else:
die("User not found.");
endif;
Any and all help will be appreciated.
EDIT: The following code is at the start of my file:
// Connection Details
$host = "";
$user = "";
$pass = "";
$db = "";
// Database Connection
$conn = new mysqli($host, $user, $pass, $db);
// check connection
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
Could you please tell me the best way to replicate the MySQL code I posted in MySQLi?