I am getting an error as A link to the server could not be established in /var/www/Test.php on line 25 and mysql_select_db(): Access denied for user 'pass'#'localhost' (using password: NO) in /var/www/Test.php on line 25 when i trying to create a tables in a created Data base.
My code:
<?php
$con = mysqli_connect("localhost","root","pass");
connection to mysql server
//checking the connection
if(mysqli_connect_errno($con))
{
echo "Sorry!Failed to connect Mysql" . mysqli_connect_error();
}
$sql = "CREATE DATABASE viral_coff";
if(mysqli_query($con,$sql))
echo "database Created Successfully!";
else
echo "error in creating the database" . mysqli_error($con);
mysql_select_db('viral_coff') or die(mysql_error());;
$tab = ' CREATE TABLE retention( '.
' playing-date DATE ,' .
' that-day INT ,' .
' 1_day INT ,' .
' 3_days INT ,' .
' 7_days INT ,' .
' 31_days INT ,)' ;
if(mysqli_query($con,$tab))
echo "Created Tables";
else
echo "Error creating table" . mysqli_error($con);
mysqli_close($con)
?>
What am i doing wrong?
mysql_ and mysqli_ functions dont work well together. Choose one of it and work with them, not both.
You're mixing mysql_* functions with mysqli_* functions. Which you of course can't!
You need to use mysqli_select_db() instead of mysql_select_db()
Also learn to put some brackets on your if statements, thereby you can be 100% sure of what is inside and outside of the statement.
You are remove password.
<?php
define('Hostname','localhost');
define('Username','root');
define('Password','');
define('DB_Name','viral_coff');
$con=mysqli_connect(Hostname,Username,Password);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE ".DB_Name;
if (mysqli_query($con,$sql))
{
echo "Database ".DB_Name." created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
$con = mysqli_connect(Hostname,Username,Password,DB_Name);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql = "CREATE TABLE retention(playing_date DATE, that_day INT, 1_day INT , 3_days INT ,7_days INT , 31_days INT)";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table retention created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
Related
This is the code I'm using for deleting row from my DB:
<?php
$eid = $_GET['eid'];
$con = mysqli_connect("localhost", "root", "","project") or die("Connection failed");
echo "connection is done";
$query = "delete from exam where eid='$eid'";
if ($con->query($query)==TRUE)
{
echo " record deleted";
}
else
{
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
?>
The else statement is not getting executed. It displays "record deleted" for every value even if the value is not found in the database.
Why is this happening? how can I verify that my record has been deleted from my DB?
You can use mysqli.affected-rows.
Consider the following:
$query="delete from exam where eid='$eid'";
if ($con->query($query)==TRUE && $con->affected_rows > 0) {
echo " record deleted";
} else {
echo "Error: " . $query . "<br>" . $con->error;
}
Host updated the environment to PHP 7. Had to update db.php and added the following:
<?php
include('administrator/configuration.php');
//echo DATABASE;
//mysql_select_db(DATABASE, mysql_connect(HOSTNAME, DBUSER, DBPASS));
$con = mysqli_connect(HOSTNAME, DBUSER, DBPASS, DATABASE) or die("Error message...");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$con) {
"Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
printf ("Success: A proper connection to MySQL was made! The ???? database is great." . PHP_EOL);
printf("Host information: " . mysqli_get_host_info($con) . PHP_EOL);
echo "<p><hr />";
try {
//$query = sprintf("SELECT database() AS the_db");
$query = sprintf("SELECT COUNT(*) FROM albums");
/* return name of current default database */
if ($result = mysqli_query($con, $query)) {
$row = mysqli_fetch_row($result);
//printf("Default database is %s.\n", $row[0]);
printf($row[0]);
}
//throw new Exception();
} catch (Exception $e) {
print "Something went wrong /n" . $e;
} finally {
// do nothing
}
?>
As per How to change mysql to mysqli?
, I tried making the change mysql_query( -> mysqli_query($con, .
Navigating to ~/administrator/login.php results in a blank page. I edited ~/public_html/administrator/lib/connection.php - with the above change, nothing.
I'm guessing a WordPress upgrade to start then take it from there.
Any other suggestions?
I'm currently doing a computing project which requires me to use an id(which is a primary key in one table) as a secondary key for another table. I want to use sessions to keep track of the variable using commands like this
$id=$_SESSION['id'];
However I'm currently having the problem of getting id from the table it's the primary key in. This is the mysql statement I'm using.
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
$sql='INSERT INTO user(email,pass) VALUES ("'.$email.'","'.$pass.'")';
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql='SELECT id FROM user WHERE user(email)="'.$email'"'
I've done a bit of looking but haven't found any examples of how to do this. The information is being sent to the database and id's are being generated and I will add validation once it all works. However I can't retrieve the specific id's from the database. Also is there a specific variable I have to use (like $result) or do I just do $id=$sql after the query is completed?
Instead of running another query, the mysqli provides a way to get the primary key from the last insert using a property of the connection called insert_id like this
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user(email,pass) VALUES('$email','$pass'");
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
// get the id
$lastId = $conn->insert_id;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Try this :
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
$sql='INSERT INTO user(email,pass) VALUES ("'.$email.'","'.$pass.'")';
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$lastId = mysqli_insert_id($con);
I recently installed MAMP. But my I can't successfully execute MySQL queries from php. I can connect to MySQL from php, but all my queries come back as NULL. I don't get any error message or other message that would help me diagnose the problem.
Oddly, I can successfully query MySQL with phpMyAdmin and MySQLWorkbench.
Here is my code:
<?php
$link = mysqli_init();
$con = mysqli_real_connect($link, 'localhost', 'root', 'root', 'mrmk', 8889);
$db = 'mrmk';
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$sql = "SHOW TABLES FROM `$db`";
$result = mysqli_query($con, $sql);
echo "</br>" . "1. MySQL Error: " . mysqli_error();
echo "</br>" . "result: ";
var_dump($result);
$sql = "SHOW TABLES FROM $db";
$result = mysqli_query($con, $sql);
echo "</br></br>2. MySQL Error: " . mysqli_error();
echo "</br>" . "result: ";
var_dump($result);
$sql = "SELECT * FROM `transactionTable` WHERE `attorneyName` LIKE \'%howard%\'";
$result = mysqli_query($con, $sql);
echo "</br></br>3. MySQL Error: " . mysqli_error();
echo "</br>" . "result: ";
var_dump($result);
if (!$result) {
echo "</br></br>" . "4. MySQL Error: " . mysqli_error();
exit;
} else {
echo "we should have results.";
}
while ($row = mysqli_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysqli_free_result($result);
?>
When I access the page through localhost I get this:
Host information: Localhost via UNIX socket
1. MySQL Error:
result: NULL
2. MySQL Error:
result: NULL
3. MySQL Error:
result: NULL
4. MySQL Error:
When I execute the same queries from phpMyAdmin, they execute successfully and return non-NULL results.
I have two questions:
What code can I add to help diagnose this problem?
Do I have a error in my code that is causing me to get NULL results.
Have you enabled mysqli extension in PHP? Check apache error log. Look at this question for example. mysqli is disabled by default, you need to uncomment line
extension=php_mysqli
in php.ini file.
To answer question #1, you might find it helpful to echo your $sql variables, to make sure your queries are valid and what you intended them to be.
I am trying use mysqli functions but I get this error:
Fatal error: Call to undefined method mysqli::num_rows() in C:\AppServ\www\edu\files\header.php on line 19
I tried to go to php.ini and remove ; from extension=php_mysqli.dll
I found it already removed
I tried to restart appachi;
the connection file:
// db username
define("USERNAME","root");
// db password
define("PASSWORD","root");
// db servername
define("SERVERNAME","localhost");
// db name
define("NAME","edu");
//connect to db
$mysqli = new mysqli(SERVERNAME,USERNAME,PASSWORD,NAME);
if ($mysqli->connect_errno) {
echo $cannot_connect;
}
//select db encoding
$mysqli->set_charset('utf8');
calling function in a file:
$sql = $mysqli->query("SELECT VALUE FROM SITE_CONFIG WHERE CONF='KEYWORDS'");
if (!$sql) {
echo "Failed to run query: (" . $mysqli->errno . ") " . $mysqli->error;
}
if($mysqli->num_rows($sql) > 0){
while($rs = $sql->fetch_assoc()){
$keyw = $rs['VALUE'];
}
}else{
$keyw = $no_data;
}
note : I included connection.php
Try:
$sql = $mysqli->query("SELECT VALUE FROM SITE_CONFIG WHERE CONF='KEYWORDS'");
if (!$sql) {
echo "Failed to run query: (" . $mysqli->errno . ") " . $mysqli->error;
} else {
if($sql->num_rows > 0){// here must be $sql , not $mysqli
while($rs = $sql->fetch_assoc()){
echo $rs['VALUE'];
}
} else{
echo "No data";
}
}