Why is it saying no database selected? - php

When trying to post data to the database it gives an error saying no database selected. But the content from the database is displaying on a table and select menu.
<?php
$con = mysqli_connect("localhost", "root", "","radian");
if(!$con)
{
exit("Couldn't connect: ".mysqli_connect_error());
}
mysqli_set_charset($con, "utf8");
$insert_data = "UPDATE enquiries
SET ResponseDate = '".$current_date."',
Response = '".$txtResponse."',
Enquiry_No = '".$_SESSION['ses_staff']
."' WHERE Enquiry_No = '".$txtStudentId."'";
$execute = mysql_query($insert_data) or die(mysql_error());
$output= '<h4 style="margin-left:1em;width:15em;color:red;"> Response successful!. </h4>';
}else{
$output= '<h4 style="margin-left:1em;width:15em;color:red;"> </h4>';
}

Your final code should be identical to:
<?php
$con = mysqli_connect("localhost", "root", "", "radian");
if (!$con) {
exit("Couldn't connect: " . mysqli_connect_error());
}
mysqli_set_charset($con, "utf8");
$insert_data = "UPDATE enquiries SET ResponseDate = '" . $current_date . "', Response = '" . $txtResponse . "',Enquiry_No = '" . $_SESSION['ses_staff'] . "' WHERE Enquiry_No = '" . $txtStudentId . "'";
$execute = mysqli_query($con, $insert_data) or die(mysqli_error($con));
$output = '<h4 style="margin-left:1em;
width:15em;
color:red;"> Response successful!. </h4>';
Changes made:
Remove all instances of mysql_* and replace with the correct mysqli_* function.
Remove the orphaned } else {.
Note: Officially mysql_* functions are deprecated. So no point using them. Use either mysqli_* or PDO.

Related

How do I get all values to echo with mysqli_fetch_assoc?

I'm pretty new to php, so don't really know how to do much, but from what I've looked up, this should echo all values from the two fields.
<?php
$con = mysqli_connect('localhost', 'root', 'root', 'unityaccess');
if(mysqli_connect_errno())
{
echo "1: Connection failed"; //error code 1 = connection failed
exit();
}
$username = $_POST["name"];
$idcheckquery = "SELECT id FROM users WHERE username = '" . $username . "';";
$idcheck = mysqli_query($con, $idcheckquery) or die("7: ID check query failed"); //error code 8 = couldn't get user's id
$existingid = mysqli_fetch_assoc($idcheck);
$userid = $existingid["id"];
$itemfindquery = "SELECT itemid, equipped FROM inventory WHERE userid = '" . $userid ."';";
$itemfind = mysqli_query($con, $itemfindquery) or die("9: Couldn't find items");
while($row = $mysqli_fetch_assoc($itemfind)){
echo $row["itemid"] . ", " . $row["equipped"] . " ";
}
?>
I expect this to, when it is called in unity, to print a list of all the values in each list, but instead it doesn't echo anything.
The mysqli_fetch_assoc() function is being used as a variable ($). Just remove the dollar sign and it will work.
while($row = mysqli_fetch_assoc($itemfind)){
echo $row["itemid"] . ", " . $row["equipped"] . " ";
}
Also, try to use prepared statements to fight against SQL injections.

How to use Insert query in PHP code

I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>

I have trouble connecting to mysql

I've created a database on az.pl I wanted to use old code which works perfectly on other website. I get this error message:
Could not connect to mysql
Here's my code:
$dbh = mysqli_connect("localhost","user","password", "db") or die ("could not connect to mysql");
I've also tried:
$c = mysql_connect("localhost", "user", "password");
mysql_select_db("db");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
Both with the same result, the error message.
I've searched the web but I haven't found exact problem.
Consider following the docs when debugging your connection issue:
http://php.net/manual/en/function.mysqli-connect.php
<?php
$link = mysqli_connect("localhost", "user", "password", "db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
// this will tell you why the connection failed
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I finally connected to the database. All I needed to do is to change isset to if in the following code:
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1";
$query = mysqli_query($link, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$body = $row["pagebody"];
}
I used MonkeyZeus code to connect to the database.Thank you MonkeyZeus.

PHP and MySql update

I'm newbie in PHP sorry. How can i update a user defined var in user defined table with user defined value in MySql? I cant get it work.
<?PHP
$table = $_POST['table'];
$id = $_POST['id'];
$key = $_POST['key'];
$value = $_POST['value'];
$con = mysql_connect("mysql.serversfree.com","u105645000***","mf***") or ("Cannot connect!" . mysql_error());
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("u10564500***" , $con) or die ("could not load the database" . mysql_error());
/*$mysql = mysql_query("INSERT INTO $table ($key) VALUES ('".$value."');");*/
$sql = "UPDATE $table Set ".$key."='".$value."'";
?>
Why your code is not working
You're not launching the query.
No where clause
Without specifying any conditions, every record of the coulmn $key will be updated with $value.
Solution
Use this code:
<?PHP
$con = mysqli_connect("mysql.serversfree.com","u105645000***","mf***","u10564500***");
$table = mysqli_real_escape_string($con,$_POST['table']);
$id = mysqli_real_escape_string($con,$_POST['id']);
$key = mysqli_real_escape_string($con,$_POST['key']);
$value = mysqli_real_escape_string($con,$_POST['value']);
if (!$con){die('Could not connect: ' . mysqli_error($con));}
$sql = mysqli_query($con,"UPDATE ".$table." Set ".$key."='".$value."'");
?>

MySQL: UPDATE database with JQuery sortable new value

I've got this:
foreach($_POST['pos'] as $value) {
$new_value = "UPDATE users SET regnr='" . $value . "'
WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
}
// Connect to database
$opendb = mysql_connect($dbhost, $dbuser, $dbpass) or die("Kunde inte ansluta till MySQL:<br>" . mysql_error());
mysql_select_db($dbname) or die("Kunde inte ansluta till databasen:<br>" . mysql_error());
mysql_query($new_value) or die(mysql_error());
// Close database
mysql_close($opendb);
Information:
$_POST['pos'] holds a value from the database in a hidden input. This value I have choosen to split with str_split($r['regnr'], 6); into a JQuery sortable list. If I type echo $value; in the foreach loop I've got the new value (not splitted, as I want) from the JQuery sortable list. I need all values from the list, and I get it with echo. But if I use $value variable to UPDATE the database that it came from, it just updates with the last value from the JQuery sortable list.
Can someone solve that? :D
Here is the solution:
$str = '';
foreach($_POST['pos'] as $value) {
$str = $str.$value;
}
// Connect to database
$opendb = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$new_value = "UPDATE users SET regnr='" . $str . "' WHERE username='" . mysql_real_escape_string($_COOKIE['username']) . "'";
mysql_query($new_value) or die(mysql_error());
// Close database
mysql_close($opendb);

Categories