Cannot access database [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
Hi i need some help in coding php to connect database
my source code is
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name
// Connect to server and select databse.
`mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";`
But it display error
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\123\tryforum\main_forum.php on line 11
cannot select DB
How to solve it

You need to pass connection variable to mysqli_select_db.
See this link
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name
// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password") or die("cannot connect");
mysqli_select_db($con, "$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";`

mysqli_select_db()
needs two parameter first your database connection and second your
database name
$conn=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($conn,"$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysqli_query($conn,$sql);

Related

Fatal error: Uncaught Error: Call to undefined function mysql_connect() [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 3 years ago.
I am a beginner and also a diploma student...
please help me solve this error... I tried many online solution but it cant help ... I'm new to php and mysql...
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="slr"; // Database name
$tbl_name="software"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$soft_name=$_POST['soft_name'];
$installed_date=$_POST['installed_date'];
$expiry_date=$_POST['expiry_date'];
$product_key=$_POST['product_key'];
// Insert data into mysql
$sql="INSERT INTO $software(soft_name, installed_date, expiry_date, product_key)VALUES('$soft_name', '$installed_date', '$expiry_date', '$product_key')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='CreateData.php'>Back to main page</a>";
} else {
echo "ERROR";
}
// close connection
mysql_close();
?>
You should use mysqli_connect instead of mysql_connect which is deprecated since PHP 5.5.0 :
$link = mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($link, $db_name)or die("cannot select DB");
Try This:
Old way:
<?php
$link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('testdb', $link);
mysql_set_charset('UTF-8', $link);
?>
New way: all you gotta do is create a new PDO object. PDO's constructor takes at most 4 parameters, DSN, username, password, and an array of driver options.
A DSN is basically a string of options that tell PDO which driver to use, and the connection details... You can look up all the options here PDO MYSQL DSN
<?php
$db=new PDO('mysql:host=localhost;dbname=slr;charset=utf8mb4', 'root', '') or die("Could connect to Database");
?>
According to Here.

PHP issue with random mysql rows

I'm having a little problem with php, basically I want to get a random row from my mysql database, I am really new to php and mysql so please be kind and explain me what's going on. I've already granted all permissions on mysql, now I just have to figure out what's going on, i tried to put some echoes to debug but it seems like anything happens, there's just a blank page with nothing on it, this drives me crazy so I'd like to resolve it. Here's the code
<?php
echo "test";
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Select a random account
$min=1;
$row=mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'mine.accounts';"));
$max=$row["Auto_increment"];
$random_id=rand($min,$max);
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
echo $row["username"]. ":" . $row["password"]
?>
// --- UPDATE ---
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Select a random account
$row = mysql_query("SELECT username AND password FROM accounts order by RAND() LIMIT 1");
WHILE ($data = mysql_fetch_array($row))
ENDWHILE;
echo $row['username'] . " " . $row['password'];
?>
On this line, you forgot the closing parentheses.
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
Hence the single closing parentheses while you open two.
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'"));
And you'll have to use a while loop to make $row output anything, since fetch_assoc returns an associative array:
while($row = mysql_fetch_assoc(<...>){
$max = $row['Auto_increment'];
}
Also you might wanna look into Prepared Statements or PDO as mysql_* Functions are officially deprecated.

Why ampps is not selecting my db?

I'm using ampps
The following is my config code. I've not given username and password in it.
$db_name="naatesam_FAMILY"; // Database name
$tbl_name="user"; // Table name
// Connect to server and select database.
$conn=mysql_connect("localhost", "", "")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
The output it shows is:
cannot select DB
I think without username and password it should work....
If anyhow they're required then please tell me how to set username and password for phpMyAdmin in ampps.
Your code should be :
$db_name="naatesam_FAMILY"; // Database name
$tbl_name="user"; // Table name
// Connect to server and select database.
$conn=mysql_connect("localhost", "root", "mysql")or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
$conn=mysql_connect("localhost", "", "")or die("cannot connect");
change to
$conn=mysql_connect("localhost", "root", "mysql")or die("cannot connect");

Update SQL tables

I want to update a table on a specific row need some advice on my php statement
I use this statement to call the client's info
<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE member_msisdn='$query'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
This works fine echoĆ­ng the information I need and able to alter it.
<form name="form1" method="post" action="control_clientupdated.php">
This referes to my action php script
Problem I need assistance with is how do i notify my action script to use the same id I ran the query on to update that row.
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET member_name='$member_name',
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',
cofficenr='$cofficenr', cfax='$cfax', e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);
I have placed the WHERE statement on the end of the update
Let me just state it shows done, but it did not update the table at all
Firstly you need to store your ID into a hidden form element in your form.
<form method="post" action="control_clientupdated.php">
<input type="hidden" name="member_msisdn" value="<?=$query?>" />
...
</form>
This will allow you to passthrough the value from your first php script.
Then in your control_clientupdated.php you need to use $_POST to recover your value.
// Store the $_POST value for my query ID
$query = $_POST['member_msisdn'] ;
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET member_name='$member_name',
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',
cofficenr='$cofficenr', cfax='$cfax', e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);
This should be what you need - note that you cannot use $_GET to retrieve the variable passed by the form, as you are sending it with the method="post" attribute, you must use $_POST instead of $_GET

mysql_fetch_assoc() error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning when using mysql_fetch_assoc in PHP
i am having a problem with the following codes, i am new in encountering this error
here is the code
session_start();
$uname=$_SESSION['login'];
$host="localhost";
$username="root";
$password="";
$db_name="sampledb";
$tbl_name="tblsched";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "SELECT * FROM tblteacher WHERE teacherName=$uname";
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
$teacherid = $row['teacherID'];
it gives me a "mysql_fetch_assoc() expects parameter 1 to be resource, boolean" error, how do i deal with this error?? i have used this code already a few times in other files and it worked perfectly except now, i checked the names of the rows and it was correct
i already tried using other commands such as mysql_fetch_array, mysql_result, mysql_fetch_row and it gives the same error
You seem to be using a variable that is a string, you need to encapsulate it in quotes:
SELECT * FROM tblteacher WHERE teacherName='$uname'
On that note, I see that it is coming from a Session variable, I take it that it is already cleansed to make sure there are no possible injection attacks within it - yes?
Try
$sql = "SELECT * FROM tblteacher WHERE teacherName='$uname'";
The problem is in this line
$sql = "SELECT * FROM tblteacher WHERE teacherName=$uname";
change to
$sql = "SELECT * FROM tblteacher WHERE teacherName='$uname'";
the uname is string and it should be quoted using single or double quotes.
Try This // user index no
session_start();
$uname=$_SESSION['login'];
$host="localhost";
$username="root";
$password="";
$db_name="sampledb";
$tbl_name="tblsched";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "SELECT * FROM tblteacher WHERE teacherName=$uname";
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
**$teacherid = $row[0];**

Categories